使用 FFmpeg 的 Android 相机捕获 [英] Android Camera Capture using FFmpeg

查看:38
本文介绍了使用 FFmpeg 的 Android 相机捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 android 相机生成的预览帧并将 data[] 传递给 ffmpeg 输入管道以生成 flv 视频.我使用的命令是:

Am tryin' to take the preview frame generated by the android camera and pass the data[] to ffmpeg input pipe to generate a flv video. The command that I used was :

ffmpeg -f image2pipe -i pipe: -f flv -vcodec libx264 out.flv

我也尝试将输入格式强制为 yuv4mpegpiperawvideo 但没有成功...android-camera 生成的预览帧默认格式为NV21.调用 ffmpeg 的方式是通过 Process API 并将预览帧 data[] 写入进程' stdin ...onPreviewFrame() 定义如下:

I've also tried to force the input format to yuv4mpegpipe and rawvideo but with no success... The default format of the preview frame generated by android-camera is NV21. The way am invokin' ffmpeg is through the Process API and writing the preview frames data[] to the process' stdin... The onPreviewFrame() definition is as follows :

public void onPreviewFrame(byte[] data, Camera camera)
{   
    try
    {
        processIn.write(data);
    }
    catch(Exception e)
    {
        Log.e(TAG, FUNCTION + " : " + e.getMessage());
    }               
    camera.addCallbackBuffer(new byte[bufferSize]);
}

processIn 连接到 ffmpeg 进程 stdinbuffersize 根据为 提供的文档计算代码>addCallbackBuffer().有什么地方做错了吗...?

processIn is connected to the ffmpeg process stdin and buffersize is computed based on the documentation provided for addCallbackBuffer(). Is there something that am doin' wrong...?

谢谢...

推荐答案

Kinda 让它完美运行...似乎发生的错误与图像流的vcodec有关.似乎 ffmpeg 没有规定解码 NV21 格式的图像或图像流.因为必须将 NV21 格式的预览帧转换为 JPEG 并且图像必须实时流式传输到 ffmpeg 过程,因此转换必须On the Fly.将 On the Fly 转换为 JPEG 的最接近可靠的解决方案如下:

Kinda got it working perfectly... The mistake that seemed to be happenin' was related to the vcodec of the image stream. Seems that ffmpeg has no provision to decode NV21 format images or image stream. For that had to convert the NV21 format preview frame to JPEG and as the images had to streamed in real time to the ffmpeg process ,the conversion had to be On the Fly. The closest reliable solution for On the Fly conversion to JPEG was as follows :

public void onPreviewFrame(byte[] data, Camera camera)
{
        if(isFirstFrame)
    {
        Camera.Parameters cameraParam = camera.getParameters();
        Camera.Size previewSize = cameraParam.getPreviewSize();
        previewFormat = cameraParam.getPreviewFormat();
        frameWidth = previewSize.width;
        frameHeight = previewSize.height;
        frameRect = new Rect(0, 0, frameWidth, frameHeight);
        isFirstFrame = false;
    }

    previewImage = new YuvImage(data, previewFormat, frameWidth, frameHeight, null);

    if(previewImage.compressToJpeg(frameRect, 50, processIn))
        Log.d(TAG, "Data : " + data.length);

    previewImage = null;

    camera.addCallbackBuffer(new byte[bufferSize]);
}

使用的 ffmpeg 命令是:

ffmpeg -f image2pipe -vcodec mjpeg -i - -f flv -vcodec libx264 out.flv

这篇关于使用 FFmpeg 的 Android 相机捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆