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
网友评论