FFmpeg / libavcodec:使用x264进行编码会忽略比特率设置

时间:2016-08-16 14:44:24

标签: c++ video ffmpeg libavcodec

我正在尝试使用FFmpeg库编写视频。到目前为止,我可以使用.avi扩展名成功编码视频,但是当我使用.mp4扩展名时,应用程序会完全忽略我指定的比特率选项。

这是我用来指定编码设置的代码片段:

//define video stream
AVOutputFormat* outFmt = nullptr;
outFmt = av_guess_format(NULL, m_pcFilename.c_str(), NULL);
avformat_alloc_output_context2(&m_pcOC, outFmt, NULL, NULL);
AVFormatContext* m_pcOC;
AVStream* m_pcVideoSt = avformat_new_stream(m_pcOC, NULL);
AVCodec* codec = nullptr;
codec = avcodec_find_encoder(codecID);
avcodec_get_context_defaults3(m_pcVideoSt->codec, codec);
//set some parameters
double dBitrate = std::stod(bitrate);
m_pcVideoSt->codec->codec_id = codecID;
m_pcVideoSt->codec->codec_type = AVMEDIA_TYPE_VIDEO;
m_pcVideoSt->codec->bit_rate = dBitrate;
m_pcVideoSt->codec->bit_rate_tolerance = 20000;
m_pcVideoSt->codec->width = m_iOutCols;
m_pcVideoSt->codec->height = m_iOutRows;
m_pcVideoSt->codec->time_base.den = static_cast<int>(dFps);
m_pcVideoSt->codec->time_base.num = 1;
if (m_pcOC->oformat->flags & AVFMT_GLOBALHEADER)
{
  m_pcVideoSt->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
/* open the codec */
AVDictionary* pcOpts = nullptr;
int res1 = av_dict_set(&pcOpts, "b", bitrate.c_str(), 0);
int res = avcodec_open2(m_pcVideoSt->codec, codec, &pcOpts);

这是我在创建.avi文件时得到的输出

ffprobe version 2.8.6 Copyright (c) 2007-2016 the FFmpeg developers
built with gcc 5.3.0 (GCC)
configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-lzma --enable-decklink --enable-zlib
libavutil      54. 31.100 / 54. 31.100
libavcodec     56. 60.100 / 56. 60.100
libavformat    56. 40.101 / 56. 40.101
libavdevice    56.  4.100 / 56.  4.100
libavfilter     5. 40.101 /  5. 40.101
libswscale      3.  1.101 /  3.  1.101
libswresample   1.  2.101 /  1.  2.101
libpostproc    53.  3.100 / 53.  3.100
Input #0, avi, from 'testGrey2.avi':
Metadata:
encoder         : Lavf56.40.101
Duration: 00:01:20.00, start: 0.000000, **bitrate: 433 kb/s**
Stream #0:0: Video: **mpeg4 (Advanced Simple Profile)** (FMP4 / 0x34504D46), yuv420p, 720x576 [SAR 1:1 DAR 5:4], 428 kb/s, 25 fps, 25 tbr, 25 tbn, 25 tbc

这是我在创建.mp4文件时得到的

ffprobe version 2.8.6 Copyright (c) 2007-2016 the FFmpeg developers
built with gcc 5.3.0 (GCC)
configuration: --disable-static --enable-shared --enable-gpl --enable-    version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-    libxavs --enable-libxvid --enable-lzma --enable-decklink --enable-zlib
libavutil      54. 31.100 / 54. 31.100
libavcodec     56. 60.100 / 56. 60.100
libavformat    56. 40.101 / 56. 40.101
libavdevice    56.  4.100 / 56.  4.100
libavfilter     5. 40.101 /  5. 40.101
libswscale      3.  1.101 /  3.  1.101
libswresample   1.  2.101 /  1.  2.101
libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'testGrey2.mp4':
Metadata:
major_brand     : isom
minor_version   : 512
compatible_brands: isomiso2avc1mp41
encoder         : Lavf56.40.101
Duration: 00:01:20.00, start: 0.000000, **bitrate: 1542 kb/s**
Stream #0:0(und): **Video: h264 (High)** (avc1 / 0x31637661), yuv420p, 720x576, 1540 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
  handler_name    : VideoHandler

关于为什么会发生这种情况的任何想法?指定输出视频比特率的首选方法是什么?

0 个答案:

没有答案