學習ffmpeg官方示例代碼transcoding.c遇到的問題

編譯測試遇到問題,首先我的編譯命令:

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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章