ffmpeg关于avformat_write_header问题

来源:互联网 发布:主播的网络热门歌曲 编辑:程序博客网 时间:2024/06/06 08:32

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

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();}}//添加音频信息到输出contextif(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;  } }//添加视频信息到输出contextif (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





0 0