ffmpeg # -f concat -i mylist.txt

ffmpeg -f concat 可将文件连接起来。
这种一般要求文件具有一致的参数,比如音频具有相同的采样率 声道数 和 位深等。

ffmpeg -f concat -safe 0 -i mylist.txt -acodec aac -b:a 128K -f mp4 -movflags faststart -y out.mp4

其中mylist.txt 是一个文件列表,依次存储输入文件:

file /home/1.m4a
file /home/2.m4a
file /home/3.m4a

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.

用法举例

目前有三个文件0.mp4 1.mp4 2.mp4
信息如下:

$ ffprobe -v quiet -show_entries stream=duration,start_time,codec_name 0.mp4
[STREAM]
codec_name=h264
start_time=0.000000
duration=106.560000
[/STREAM]
[STREAM]
codec_name=ac3
start_time=0.000000
duration=106.527979
[SIDE_DATA]
[/SIDE_DATA]
[/STREAM]
$ ffprobe -v quiet -show_entries stream=duration,start_time,codec_name 1.mp4
[STREAM]
codec_name=h264
start_time=0.000000
duration=7.760000
[/STREAM]
[STREAM]
codec_name=ac3
start_time=0.000000
duration=7.711979
[SIDE_DATA]
[/SIDE_DATA]
[/STREAM] 
$ ffprobe -v quiet -show_entries stream=duration,start_time,codec_name 2.mp4
[STREAM]
codec_name=h264
start_time=0.000000
duration=46.480000
[/STREAM]
[STREAM]
codec_name=ac3
start_time=0.015000
duration=46.432000
[SIDE_DATA]
[/SIDE_DATA]
[/STREAM]

106.560000 + 7.760000 + 46.480000 = 160.8

建立mylist.txt如下

file 0.mp4
file 1.mp4
file 2.mp4

执行命令:

ffmpeg -f concat -safe 0 -i mylist.txt -c copy -f mp4 concat.mp4

查看生成的信息:

$ ffprobe -v quiet -show_entries stream=duration,start_time,codec_name concat.mp4
[STREAM]
codec_name=h264
start_time=0.000000
duration=160.800000
[/STREAM]
[STREAM]
codec_name=ac3
start_time=0.000000
duration=160.767000
[SIDE_DATA]
[/SIDE_DATA]
[/STREAM]

和理论计算是相符合的。

也可以只读每个片段中的一部分来进行合并,用inpoint和outpoint参数来指定。
建立mylist.txt文件:

file 0.mp4
inpoint  00:00:00.000
outpoint 00:00:05.000

file 1.mp4
inpoint  00:00:00.000
outpoint 00:00:05.000

file 2.mp4
inpoint  00:00:00.000
outpoint 00:00:05.000

每个片段只去前5秒,然后执行命令。
查看生成的文件:

$ ffprobe -v quiet -show_entries stream=duration,start_time,codec_name concat.mp4
[STREAM]
codec_name=h264
start_time=0.000000
duration=15.000000
[/STREAM]
[STREAM]
codec_name=ac3
start_time=0.000000
duration=14.975000
[SIDE_DATA]
[/SIDE_DATA]
[/STREAM]

需要注意的是这种inpoint的方式,可能会往前多获取一些数据(如果指定的时间点不是I帧的话)。

This directive works best with intra frame codecs, because for non-intra frame ones you will usually get extra packets before the actual In point and the decoded content will most likely contain frames before In point too.

References:

https://ffmpeg.org/ffmpeg-all.html
https://trac.ffmpeg.org/wiki/Concatenate
https://ffmpeg.org/ffmpeg-formats.html#concat

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,126评论 0 10
  • 外面下着雨,隔着玻璃窗都能感受到空气的清新与凉爽,淅淅沥沥的雨,自由,随意,而风,很温柔,轻轻的摇曳着,似乎让人忘...
    大仙沫沫啊阅读 1,521评论 1 1
  • 雪休冬暮路难行。矮民房。冷风光。半老爷娘,孙长绕膝忙。冰没堂前行战战,才扫洒,几仓皇。 尘除污去待春阳,力不强,日...
    流水有声阅读 3,192评论 9 6
  • 出埃及记: 第一章 主题: 神厚待神仆 …出埃及记第一章记以色列民族在雅各与约瑟时代下进入埃及,直到后来有“有不认...
    義胜洁阅读 25,475评论 1 1
  • 今天我和两个姐姐商量好今天去妈妈家,两个弟弟在家等着我们。我昨天住在孩子姑姑家,等着孩子爸爸来接!姐姐都早早来到妈...
    史响庆阅读 936评论 0 0