ijkplayer-ffmpeg之concat

參考ffmpeg的concat官方文檔

concat顧名思義,就是把多段視頻拼接播放,可以用于連續劇的連續播放。

configure配置:

--enable-demuxer=concat
--enable-protocol=concat

ijkplayer對應修改

export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-protocol=concat"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-demuxer=concat"

總得來說,只需要3步

  • 建個文件, 命名為1.ffconcat
  • 播放器地址:類似file:///sdcard/1.ffconcat
  • 如果你的ffmpeg版本低于3.0左右, 可能要改ffmpeg的源碼

根據文檔精神,需要新建一個文件,命名為 1.ffconcat (當然,也可以1.ffcat)

A list file with the suffix ".ffcat" or ".ffconcat"  will auto-select this format.

1.ffconcat內容:

ffconcat version 1.0
file '/sdcard/1.mp4'
duration 3.0
file '/sdcard/2.mp4'
duration 3.0
file '/sdcard/3.mp4'
duration 3.0

url路徑這樣寫:
file:///sdcard/1.ffconcat

這樣還是有問題,因為safe參數的原因:

safe
If set to 1, reject unsafe file paths. A file path is considered safe if it does not contain a protocol specification and is relative and all components only contain characters from the portable character set (letters, digits, period, underscore and hyphen) and have no period at the beginning of a component.

If set to 0, any file name is accepted.

The default is 1.

-1 is equivalent to 1 if the format was automatically probed and 0 otherwise.

很明顯,safe默認是1.
看代碼libavformat/concatdec.c

static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
                    unsigned *nb_files_alloc)
{
    ConcatContext *cat = avf->priv_data;
    ConcatFile *file;
    char *url = NULL;
    const char *proto;
    size_t url_len, proto_len;
    int ret;

    if (cat->safe > 0 && !safe_filename(filename)) {
        av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
        //FAIL(AVERROR(EPERM)); //gongjia mask
    }

safe_filename這個函數被判斷返回0了,簡單做法是把FAIL這行屏蔽掉,簡單粗暴!

編譯驗證ffmpeg3.1,播放沒問題。(ffmpeg3.1及以下都有這問題,以上的沒驗證過)

有一點要注意:要加上duration,要不然播放的時候總的時長和進度會有問題
參考2.ffconcat

ffconcat version 1.0
file http://v1.play.fangyantianxia.cn/630ef5659bd52ff1.m3u8
duration 48.8
file http://v1.play.fangyantianxia.cn/e1e92bc7782670d6.m3u8
duration 255.7

官方解釋是這樣:

duration dur

Duration of the file. This information can be specified from the file; specifying it here may be more efficient or help if the information from the file is not available or accurate.
If the duration is set for all files, then it is possible to seek in the whole concatenated video.

說的很清楚:加上duration 是防止文件時長給錯,如果所有文件都加上,可以在合并的視頻中seek。

這樣就OK了,測試了seek也是ok的

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容