ffmpeg关于avformat_write_header问题
2015-07-21 19:02
411 查看
最近做转码遇到一些问题,先贴个代码在说明:
这个位置本身设定的是48000结果出来的是64000等等的一系列问题。
特此记录,深层问题:
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
相关文章推荐
- dup , dup2 , fcntl锁
- VIM
- 那些成功学和鸡汤文不会告诉你的
- 闭包
- hdoj1020
- window8.1编译hadoop2 eclipse插件,并远程调试hadoop程序
- 【瞎搞】HDU 5288 OO’s Sequence
- Java 二分法查找算法
- HTML5 链接记录
- 单电机板机模型,f22
- 单电机板机模型
- 用java 写 merge sort
- 不要62 (暴力打表)
- 目录条目”SASHELP.EMCREDIT.IGN.SOURCE”不存在
- java 二叉树的查找方法
- Rescue-优先队列
- 十六进制转换为十进制
- 2015 Multi-University Training Contest 1
- 用Quartz 2D画小黄人
- 统计元音