学习ffmpeg官方示例代码transcoding.c遇到的问题

来源:互联网 发布:表格怎么把相同的数据 编辑:程序博客网 时间:2024/05/17 09:19

编译测试遇到问题,首先我的编译命令:

export PKG_CONFIG_PATH=~/ffmpeg_build/lib/pkgconfig:$PKG_CONFIG_PATH//将库的路径添加到PKG_CONFIG_PATH中gcc transcoding.c -o transcoding `pkg-config --libs --cflags libavcodec libavutil libavfilter`

编译完成,运行命令:

./transcoding input.ts output.avi

issue 1 - [libx264 @ 0x7fe4ac851600] broken ffmpeg default settings detected

这个错误及其下面的信息告诉我们,在打开输出文件的视频编码器时出现了错误,这通常是由于编码器参数设置不当造成的。通过搜索,发现了这篇文章对这个问题解释的比较清楚:
http://blog.csdn.net/cffishappy/article/details/7680097

解决方法:

在transcoding.c中的open_output_file函数中,修改的部分如下(只增加了13-17行):

if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {                enc_ctx->height = dec_ctx->height;                enc_ctx->width = dec_ctx->width;                enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;                /* take first format from list of supported formats */                if (encoder->pix_fmts)                    enc_ctx->pix_fmt = encoder->pix_fmts[0];                else                    enc_ctx->pix_fmt = dec_ctx->pix_fmt;                /* video time_base can be set to whatever is handy and supported by encoder */                enc_ctx->time_base = dec_ctx->time_base;                enc_ctx->me_range = 16;                 enc_ctx->max_qdiff = 4;                enc_ctx->qmin = 10;                 enc_ctx->qmax = 51;                 enc_ctx->qcompress = 0.6;            } else {                enc_ctx->sample_rate = dec_ctx->sample_rate;                enc_ctx->channel_layout = dec_ctx->channel_layout;                enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);                /* take first format from list of supported formats */                enc_ctx->sample_fmt = encoder->sample_fmts[0];                enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};            }   

这次的视频编码可以打开并正常编码了,可是音频部分又出现了问题,关于这个问题的解答可以看这个文章:
http://www.tuicool.com/articles/NNNVv2z

issue2 - [mp4 @ 0x7fbe86803e00] Malformed AAC bitstream detected

测试中本人未遇到这个错误,记下来以便以后使用吧。stackoverflow中提供了一种方法,可以使程序正常运行,但处理结果中只有音频被保留,
其实,只需要调整一下代码顺序就可以了。
解决方法
还是在transcoding.c中的open_output_file函数中,只是把for循环尾部的那句代码:

if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)                enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; 

提前到for循环中整个if判断结构的前面就行了,因为我们在打开编码器前,没有设置编码器上下文中enc_ctx->flags这个参数,所以把这句提前了就解决问题了。如下:

 if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)                enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;            if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {                enc_ctx->height = dec_ctx->height;                enc_ctx->width = dec_ctx->width;                enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;                /* take first format from list of supported formats */                if (encoder->pix_fmts)                    enc_ctx->pix_fmt = encoder->pix_fmts[0];                else                    enc_ctx->pix_fmt = dec_ctx->pix_fmt;                /* video time_base can be set to whatever is handy and supported by encoder */                enc_ctx->time_base = dec_ctx->time_base;                enc_ctx->me_range = 16;                enc_ctx->max_qdiff = 4;                enc_ctx->qmin = 10;                enc_ctx->qmax = 51;                enc_ctx->qcompress = 0.6;            } else {                enc_ctx->sample_rate = dec_ctx->sample_rate;                enc_ctx->channel_layout = dec_ctx->channel_layout;                enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);                /* take first format from list of supported formats */                enc_ctx->sample_fmt = encoder->sample_fmts[0];                enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};            }

issue3 - 处理后的结果比原视频模糊

在经过以上两个问题的解决后,编译通过是没有问题了,不过你可能会不满意输出视频的质量,觉得它比原视频模糊了许多,这时我们可以调节#issue1中的代码:

enc_ctx->qmax = 51; 

把这个参数改小点,可以减小编码时的量化间隔,提高编码视频的质量,不过你可能因此加长整个编码的时间。
例如我将其改为:

 enc_ctx->qmax = 30;