下载 m3u8 的 ffmpeg 命令:
1
2
$ mkdir mydir
$ ffmpeg -i http://xxx.com/hls/record.m3u8 -c copy mydir/out.mp4
如果完整的 ts 流,中间没有中断过。那么在 mydir 目录直接就生成 out.mp4 了。
但是我这里不幸出现错误:
1
2
[mp4 @ 0x1104490] Application provided duration: 8431004738 / timestamp: 8537426612 is out of range for mov/mp4 format
[mp4 @ 0x1104490] pts has no value
这样只好自己手工下载 m3u8 文件中的 ts 文件然后再合并。
首先可以通过下面命令生成一个 ts 文件 dowload 列表:
1
2
3
4
5
6
7
8
9
10
11
12
13
$ curl -s http://xxx.com/hls/record.m3u8 | grep '.ts' | xargs -n 1 -I{} echo 'http://xxx.com/hls/{}' > download-list.txt
$ cat download-list.txt
http://xxx.com/hls/20191012/085950_49.ts
http://xxx.com/hls/20191012/090001_50.ts
http://xxx.com/hls/20191012/090013_51.ts
http://xxx.com/hls/20191012/090025_52.ts
http://xxx.com/hls/20191012/090037_53.ts
http://xxx.com/hls/20191012/090049_54.ts
http://xxx.com/hls/20191012/090101_55.ts
http://xxx.com/hls/20191012/090113_56.ts
http://xxx.com/hls/20191012/090125_57.ts
http://xxx.com/hls/20191012/090137_58.ts
download-list.txt 文件生成后,使用 wget 命令下载 ts 文件到 mydir 目录,命令如下
1
2
3
4
$ wget -i download-list.txt -P mydir
$ ls mydir
085950_49.ts 090001_50.ts 090013_51.ts 090025_52.ts 090037_53.ts
090049_54.ts 090101_55.ts 090113_56.ts 090125_57.ts 090137_58.ts
文件下载完成后,可以生成合并列表了。
1
2
3
4
5
6
7
8
9
10
11
12
$ grep -oE '[^\/]+.ts' download-list.txt | xargs -n 1 -I{} echo "file 'mydir/{}'" > concat-list.txt
$ cat concat-list.txt
file 'mydir/085950_49.ts'
file 'mydir/090001_50.ts'
file 'mydir/090013_51.ts'
file 'mydir/090025_52.ts'
file 'mydir/090037_53.ts'
file 'mydir/090049_54.ts'
file 'mydir/090101_55.ts'
file 'mydir/090113_56.ts'
file 'mydir/090125_57.ts'
file 'mydir/090137_58.ts'
文件格式是固定的。格式为:file ‘文件路径’。如果是多个 mp4 文件合并也是一样的格式。如 file ‘mydir/1.mp4’
列表有了,最后主角该登场了(其实就一行命令,废了这么多话)
1
$ ffmpeg -f concat -i concat-list.txt -c copy mydir/out.mp4