FFMPEG设置网络摄像头编码器c++

FFMPEG Set Webcam Encoder C++

本文关键字:编码器 c++ 摄像头 网络 设置 FFMPEG      更新时间:2023-10-16

我试图在windows中使用FFMPEG C API捕获网络摄像头流。我可以使用以下命令行选项做我想做的事情:

ffmpeg -f dshow -vcodec mjpeg -s 1280x720 -framerate 30 -i video=HX-A1:audio="Microphone (HX-A1)" outputFile.mpg

我已经开始与转码。c的例子,并得到了它的工作,以记录一个虚拟的网络摄像头,如屏幕捕捉记录器。然而,我需要为我的真实网络摄像头设置编码器选项,因为它默认为160x120原始视频,我更喜欢更高的分辨率。我正在尝试以下设置相机编码器选项,但它似乎没有做任何事情。

AVDictionary *opt = NULL;
av_dict_set(&opt, "vcodec", "mjpeg", 0);
av_dict_set(&opt, "s", "1280x720", 0);
av_dict_set(&opt, "framerate", "30", 0);
if ((ret = avformat_open_input(&ifmt_ctx, filename, inputFormat, &opt)) < 0) {
    av_log(NULL, AV_LOG_ERROR, "Cannot open input filen");
    return ret;
}

是否有另一种方法来设置输入选项,以告诉相机使用什么编解码器在我的命令行示例中完成?

解决了,我必须先初始化我的AVFormatContext,然后打开MJPEG解码器,并在打开之前设置AVFormatContext的视频解码器和编解码器ID。

AVDictionary* dictionary = NULL;
av_dict_set(&dictionary, "video_size", "1280x720", NULL);
av_dict_set(&dictionary, "framerate", "30", NULL);
ifmt_ctx = avformat_alloc_context();
ifmt_ctx->video_codec_id = AV_CODEC_ID_MJPEG;
av_format_set_video_codec(ifmt_ctx, opened_mjpeg_codec);
avformat_open_input(&ifmt_ctx, filename, inputFormat, &dictionary);