2013-10-18 77 views
4

我試圖捕獲相機輸出並使用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; 

回答

2

因爲每個編碼器都有自己的個人資料和您提供的比特率是一個提示。如果你的比特率是一個有效值(不是太小也不是太大),編解碼器將只在他的配置文件列表中選擇一個支持的比特率。編解碼器的「能力」也可能會影響比特率,但這是我所知道的。

編解碼器配置文件定義至少之間的相關性:

  • 幀大小(寬度,heigth)
  • 比特率
  • 像素格式
  • 幀速率

我仍在努力找到一種方法來使用api從編解碼器獲取比特率,但是您可以通過在比特率之前給出一個非常低的比特率來查找其配置文件編解碼器。

與代碼

codecContext->bit_rate = 1; 
avcodec_open2(codecContext, codec, NULL); 

FFmpeg的編解碼器將登錄投訴以及以上所列的可接受的元組的列表。

注意:只能使用不需要外部庫的編解碼器