ijkplayer-ffmpeg之concat

作者: 南风无影 | 来源:发表于2016-10-12 11:47 被阅读3232次

参考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的

相关文章

网友评论

  • engebei:有demo吗?大神
  • 36aa678b018e:您好 可以加我个QQ吗 多段方面我还有个问题想问您 200863405
  • 36aa678b018e:请问安卓端如何生成这个.concat文件去交给播放器
    南风无影:@像雨又像风丶自己生成
  • 嫌疑人zx:你好,我想请教你一个问题,就是一集电视剧,后台分成6段装在数组里面发给我,只有视频链接,并没有给我每个链接的时长,我是不是要让他返回给我每个链接的时长呀,我看了很多博客,里面除了链接还有时长,或者说,有其他方式获得每个链接的时长吗,谢谢
    南风无影: @嫌疑人zx 嗯
    嫌疑人zx:@Gongjia 就是说,时长要后台传给我,对吗
    南风无影:@嫌疑人zx 可以拿到每段的时长,ffmpeg有接口 就是dutation
  • de8ef940e01c:--enable-demuxer=concat
    --enable-protocol=concat 请问这个具体更改哪个文件?
    南风无影:module.sh
  • cfe1c1fef66e:大神 ijk相关项目 播放在线视频 每个视频到[80%]必现[重播]现象 请教这是上层逻辑问题还是IJK源码需要改动 谢谢啦:kissing_heart:
    cfe1c1fef66e:@Gongjia 嗯 我查一下 谢谢
    南风无影:@ceycochen 源的问题,底层可以修复
  • 迷途小书童nb:请问下要生成合并后的文件该咋弄呢?
    南风无影:@Adley ffmpeg有对应的命令,你如果要代码实现的话,也能实现
    迷途小书童nb:@Holden 额,我的需求是想将多个mp4文件合并成一个新的mp4,不需要播放。
    c7f5d4c0ed81:直接把生成文件的本地地址 当作url传给播放器就行了
  • 648cf77e9cc7:大虾
    我最近项目也遇到了需要合并的问题
    能加个你的联系方式吗?有很多不明白的地方恳请你指导一下
    QQ:49256440
    南风无影:@敲码道士 好的,已加你了

本文标题:ijkplayer-ffmpeg之concat

本文链接:https://www.haomeiwen.com/subject/jukiyttx.html