ffmpeg关于avformat_write_header问题

最近做转码遇到一些问题,先贴个代码在说明:

int init_mux()
{
	int i = 0;
	/* allocate the output media context */
	avformat_alloc_output_context2(&ocodec, NULL,NULL, OUTPUTURL);
	if (!ocodec) 
	{
		return getchar();
	}
	AVOutputFormat* ofmt = NULL;
	ofmt = ocodec->oformat;

	/* open the output file, if needed */
	if (!(ofmt->flags & AVFMT_NOFILE))
	{
		if (avio_open(&ocodec->pb, OUTPUTURL, AVIO_FLAG_WRITE) < 0)
		{
			printf("Could not open '%s'\n", OUTPUTURL);
			return getchar();
		}
	}

	//添加音频信息到输出context
	if(audio_stream_idx != -1)//如果存在音频
	{
		ofmt->audio_codec = audio_codecID;
		//如果是音频需要编解码
		if(audio_codecID != icodec->streams[audio_stream_idx]->codec->codec_id ||
			1 != icodec->streams[audio_stream_idx]->codec->sample_fmt)
		{
			oaudio_st = add_out_stream2(ocodec, AVMEDIA_TYPE_AUDIO,&audio_codec);
		}
		else
		{
			oaudio_st = add_out_stream(ocodec, AVMEDIA_TYPE_AUDIO);
		}
		if ((strstr(ocodec->oformat->name, "flv") != NULL) || 
			(strstr(ocodec->oformat->name, "mp4") != NULL) || 
			(strstr(ocodec->oformat->name, "mov") != NULL) ||
			(strstr(ocodec->oformat->name, "3gp") != NULL))    
		{
			if (oaudio_st->codec->codec_id == AV_CODEC_ID_AAC) 
			{
				vbsf_aac_adtstoasc =  av_bitstream_filter_init("aac_adtstoasc");  
			}
		}
		if(vbsf_aac_adtstoasc == NULL)  
		{  
			return -1;  
		} 
	}
	//添加视频信息到输出context
	if (video_stream_idx != -1)//如果存在视频
	{
		ofmt->video_codec = video_codecID;
		//如果是视频需要编解码
		if(bit_rate != icodec->streams[video_stream_idx]->codec->bit_rate ||
			m_dwWidth != icodec->streams[video_stream_idx]->codec->width ||
			m_dwHeight != icodec->streams[video_stream_idx]->codec->height ||
			video_codecID != icodec->streams[video_stream_idx]->codec->codec_id || 
			m_dbFrameRate != av_q2d(icodec->streams[video_stream_idx]->r_frame_rate))
		{
			ovideo_st = add_out_stream2(ocodec, AVMEDIA_TYPE_VIDEO,&video_codec);
		}
		else
		{
			ovideo_st = add_out_stream(ocodec,AVMEDIA_TYPE_VIDEO);
		}
	}

	av_dump_format(ocodec, 0, OUTPUTURL, 1);

	if (video_stream_idx != -1)//如果存在视频
	{
		//如果是视频需要编解码
		if(bit_rate != icodec->streams[video_stream_idx]->codec->bit_rate ||
			m_dwWidth != icodec->streams[video_stream_idx]->codec->width ||
			m_dwHeight != icodec->streams[video_stream_idx]->codec->height ||
			video_codecID != icodec->streams[video_stream_idx]->codec->codec_id || 
			m_dbFrameRate != av_q2d(icodec->streams[video_stream_idx]->r_frame_rate))
		{
			//解码初始化
			init_decode(VIDEO_ID);
			//编码初始化
			init_code(VIDEO_ID);
		}
	}
	//如果是音频需要编解码
	if(audio_stream_idx != -1)//如果存在音频
	{
		if(audio_codecID != icodec->streams[audio_stream_idx]->codec->codec_id ||
			1 != icodec->streams[audio_stream_idx]->codec->sample_fmt)
		{
			//解码初始化
			init_decode(AUDIO_ID);
			//编码初始化
			init_code(AUDIO_ID);
		}
		if (avformat_write_header(ocodec, NULL))
		{
			printf("Call avformat_write_header function failed.\n");
			return 0;
		}
	}
	return 1;
}
上述代码中如果需要做编解码,重采样等操作时,需要先打开编码器,再写入avformat_write_header(如果直接av_interleaved_write_frame不做任何其他操作忽略本条博文),否则会出现例如:

这个位置本身设定的是48000结果出来的是64000等等的一系列问题。

特此记录,深层问题:

交流请加QQ群:62054820
QQ:379969650





  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值