尝试在C ++中的ffmpeg中设置MJPEG编码器 [英] Trying to setup MJPEG encoder in ffmpeg in C++
问题描述
您好
我想让MJPG编码正常工作,我有AV_CODEC_ID_MPEG1VIDEO正常工作,但当我切换到AV_CODEC_ID_MJPEG时出现错误-22
代码在最后一部分失败,无法打开编解码器。
任何帮助都将不胜感激这是代码:
Hi There
I am trying to get MJPG encoding working, I have AV_CODEC_ID_MPEG1VIDEO working, but when I switched to AV_CODEC_ID_MJPEG I get an error -22
The code fails on the last part with "could not open codec".
Any help would be greatly appreciated!
This is the code:
AVCodecID codec_id = AV_CODEC_ID_MJPEG; //AV_CODEC_ID_MPEG1VIDEO;
AVCodec* codec = avcodec_find_encoder(codec_id);
if (!codec)
{
printf("Codec not found\n");
return -1;
}
// Initialize codec.
AVCodecContext* codecContext = avcodec_alloc_context3(codec);
codecContext->codec_id = codec_id;
codecContext->codec_type = AVMEDIA_TYPE_VIDEO;
codecContext->pix_fmt = PIX_FMT_YUV422P;
codecContext->bit_rate = 400000;
codecContext->width = 320;
codecContext->height = 240;
codecContext->time_base.num = 1;
codecContext->time_base.den = 25;
int retval;
// Open the codec.
if ((retval = avcodec_open2(codecContext, codec, NULL)) < 0)
{
printf("could not open codec\n");
return -2;
}
推荐答案
尝试使用PIX_FMT_YUV J 422P而不是PIX_FMT_YUV422P。
我发现在这个 [ ^ ]论坛问题。这个问题和答案是从2012年10月开始的,所以事情本来可以改变。
如果这不能解决你的问题,我注意到<$ c $中的以下评论c> pixfmt.h :
Try using PIX_FMT_YUVJ422P instead of PIX_FMT_YUV422P.
I found that in this[^] forum question. That question and answer is from Oct. 2012, so things could have changed.
If that does not solve your problem, I noticed the following comment inpixfmt.h
:
AV_PIX_FMT_YUVJ422P, ///< planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_range
所以你可能只需要指定颜色范围以使其工作。不幸的是,我不知道正确的方法(我只使用了MJPEG解码器,而不是编码器)。
作为一方 - 请注意,我相信您应该将您的命名约定从 PIX_FMT_xxxx
更改为较新的 AV_PIX_FMT_xxxx
。但是,它们在各自的枚举中确实具有相同的价值,因此这不是您的问题。
Soren Madsen
So you may just have to specify the color range to make it work. Unfortunately, I don't know the right way to do that off-hand (I have only used the MJPEG decoder, not the encoder).
As a side-note, I believe you should change your naming convention from PIX_FMT_xxxx
to the newer AV_PIX_FMT_xxxx
. However, they do have the same value in their respective enums, so that is not your problem.
Soren Madsen
这篇关于尝试在C ++中的ffmpeg中设置MJPEG编码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!