我試圖捕獲相機輸出並使用libavcodec製作視頻。作爲一個例子,如何完成這個我已經使用ffmpeg muxing example。使用libavcodec編碼視頻時的極高比特率
問題是,4秒視頻的大小爲〜15mb,比特率爲〜30000 kb/s,雖然我已經將AVCodecContext上的比特率設置爲400000(我認爲這個值是以比特/秒爲單位的,而不是kb/s的)。
我也嘗試從命令行使用ffmpeg錄製視頻,它的碼率爲〜700 kb/s。
有沒有人有一個想法,爲什麼比特率不被保留,因此產生的文件是非常大的?我已經使用初始化編解碼器上下文中的代碼如下:
初始化部分:
avformat_alloc_output_context2(&m_formatContext, NULL, NULL, filename);
outputFormat = m_formatContext->oformat;
codec = avcodec_find_encoder(outputFormat->video_codec);
m_videoStream = avformat_new_stream(m_formatContext, codec);
m_videoStream->id = m_formatContext->nb_streams - 1;
codecContext = m_videoStream->codec;
codecContext->codec_id = outputFormat->video_codec;
codecContext->width = m_videoResolution.width();
codecContext->height = m_videoResolution.height();
int m_bitRate = 400000;
codecContext->bit_rate = m_bitRate;
codecContext->rc_min_rate = m_bitRate;
codecContext->rc_max_rate = m_bitRate;
codecContext->bit_rate_tolerance = 0;
codecContext->time_base.den = 20;
codecContext->time_base.num = 1;
codecContext->pix_fmt = AV_PIX_FMT_YUV422P;
if (m_formatContext->oformat->flags & AVFMT_GLOBALHEADER)
codecContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
/* open it */
ret = avcodec_open2(codecContext, codec, NULL);
avFrame = avcodec_alloc_frame();
ret = avpicture_alloc(&avPicture, codecContext->pix_fmt, codecContext->width, codecContext->height);
*((AVPicture *)avFrame) = avPicture;
av_dump_format(m_formatContext, 0, filename, 1);
if (!(outputFormat->flags & AVFMT_NOFILE)) {
ret = avio_open(&m_formatContext->pb, filename, AVIO_FLAG_WRITE);
}
ret = avformat_write_header(m_formatContext, NULL);
if (avFrame)
avFrame->pts = 0;