2013-10-18 82 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的编解码器将登录投诉以及以上所列的可接受的元组的列表。

注意:只能使用不需要外部库的编解码器