I am trying to encode a video from raw YUV to MPEG-2 using the ffmpeg API.
My problem is that the API-generated file is approx. 1.7 times bigger than the equivalent files generated by ffmpeg itself.
I use the quantization parameter (via qmin
and qmax
) instead o开发者_高级运维f the bitrate.
The API-version is basically:
//...
pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
pCodecCtx->qmin = 3;
pCodecCtx->qmax = 3;
pCodecCtx->time_base.num = 1;
pCodecCtx->time_base.den = 30;
avcodec_open(pCodecCtx, avcodec_find_encoder(CODEC_ID_MPEG2VIDEO));
//...
while(/*...*/) {
avcodec_encode_video(pCodecCtx, pOutbuf, outbufSize, pPicture);
//..
}
//...
For ffmpeg itself, I use the command:
ffmpeg -s 352x288 -r 30 -i foreman_352x288.yuv -f mpeg2video -vcodec mpeg2video -r 30 -pix_fmt yuv420p -qmin 3 -qmax 3 foreman.m2v
Why does the API-generate file achieve a bitrate of 5212 kb/s and the file generated by ffmpeg for the same qp
a bitrate of 3047 kb/s??
(Even more puzzling is that the smaller ffmpeg version has a slightly higher PSNR, 40.49 dB vs. 40.02 dB).
Are there any other relevant parameters that I missed? Does the ffmpeg actually respect the quantization parameter?
When using the ffmpeg API, the picture type (I-frame, P-frame, etc.) needs to be set manually for each frame. By default, ffmpeg will make every frame an I-frame.
The solution is to set the picture type before encoding a frame (here for a GOP size of 12):
//...
while(/*...*/) {
if(pCodecCtx->frame_number % 12)
pPicture->pict_type = AV_PICTURE_TYPE_P;
avcodec_encode_video(pCodecCtx, pOutbuf, outbufSize, pPicture);
//...
}
//...
Note that setting pCodecCtx->gop_size
before the encoding does not help.
精彩评论