使用ffmpeg下載HLS(m3u8)合併生成單個MP4視頻,多mp4視頻合併也是同理

下載m3u8的ffmpeg命令:

$ mkdir mydir
$ ffmpeg -i http://xxx.com/hls/record.m3u8 -c copy mydir/out.mp4

如果完整的ts流,中間沒有中斷過。那麼在mydir目錄直接就生成out.mp4了。

但是我這裏不幸出現錯誤:

[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列表:

$ 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目錄,命令如下

$ 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

文件下載完成後,可以生成合並列表了。

$ 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' 

列表有了,最後主角該登場了(其實就一行命令,廢了這麼多話)

$ ffmpeg -f concat -i concat-list.txt -c copy mydir/out.mp4

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章