如何一次性下载.m3u8

问题描述 投票:3回答:3

我在远程主机上有一个.m3u8文件,包含固定数量的chunk .ts文件名,而不是stream:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:11
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:9.736,
media_0.ts
#EXTINF:9.96,
media_1.ts
#EXTINF:10.0,
media_2.ts
#EXTINF:10.0,
media_3.ts
#EXTINF:10.0,
media_4.ts
#EXTINF:10.2,
media_5.ts
#EXTINF:10.0,

当我使用此命令时:

# ffmpeg -i "http://example.com/chunklist.m3u8" file.mp4

frame=  582 fps=9.4 q=28.0 size=    1536kB time=00:00:23.21 bitrate= 542.1kbits/s dup=2 drop=4 speed=0.375x

有用。但它逐帧视频,需要很长时间。 (播放视频几乎需要时间。)

但是因为所有.ts文件的路径都是已知的。 (http://example.com/media_0.tshttp://example.com/media_1.ts,...)必须有一种方法可以同时获取和合并它们。

但是如何直接在ffmpeg?!

EDIT (try a solution):

对于一个解决方案,我知道如何使用ffmpeg连接文件。

ffmpeg -i "concat:0.ts|1.ts|2.ts|3.ts|4.ts|5.ts" -c copy output.mp4

这个ffmpeg命令很棒,并且工作时间少于1秒!

因此,尝试使用此命令下载所有带CURL的.ts文件:

curl \
http://example.com/media_0.ts -o 0.ts \
http://example.com/media_1.ts -o 1.ts \
http://example.com/media_2.ts -o 2.ts \
http://example.com/media_3.ts -o 3.ts \
http://example.com/media_4.ts -o 4.ts \
http://example.com/media_5.ts -o 5.ts

但你可以看到结果:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  687k  100  687k    0     0  75108      0  0:00:09  0:00:09 --:--:-- 74111
100  652k  100  652k    0     0  59404      0  0:00:11  0:00:11 --:--:-- 53400
100  673k  100  673k    0     0  48675      0  0:00:14  0:00:14 --:--:-- 55781
100  657k  100  657k    0     0  63573      0  0:00:10  0:00:10 --:--:-- 62494
100  671k  100  671k    0     0  39019      0  0:00:17  0:00:17 --:--:-- 40863
100  692k  100  692k    0     0  63480      0  0:00:11  0:00:11 --:--:-- 80049

看,总下载时间是72秒,而所有部分的总持续时间是59秒!这个时间很长!

很抱歉,下载所有部件,然后结束,不是很好的解决方案。

EDIT 2

我尝试使用不同的URL在另一台服务器上的另一个.m3u8文件:

下载并连接在一起:

ffmpeg -i "concat:\
http://184.72.239.149/vod/smil:BigBuckBunny.smil/media_w442897525_b560000_0.ts|\
http://184.72.239.149/vod/smil:BigBuckBunny.smil/media_w442897525_b560000_1.ts|\
http://184.72.239.149/vod/smil:BigBuckBunny.smil/media_w442897525_b560000_2.ts|\
http://184.72.239.149/vod/smil:BigBuckBunny.smil/media_w442897525_b560000_3.ts|\
http://184.72.239.149/vod/smil:BigBuckBunny.smil/media_w442897525_b560000_4.ts|\
http://184.72.239.149/vod/smil:BigBuckBunny.smil/media_w442897525_b560000_5.ts\
" -c copy -y output.ts

使用input.txt URL文件的另一个命令。

ffmpeg -f "concat" -i "input.txt" -c copy -y output.ts

input.txt文件:

file 'http://184.72.239.149/vod/smil:BigBuckBunny.smil/media_w442897525_b560000_0.ts'
file 'http://184.72.239.149/vod/smil:BigBuckBunny.smil/media_w442897525_b560000_1.ts'
file 'http://184.72.239.149/vod/smil:BigBuckBunny.smil/media_w442897525_b560000_2.ts'
file 'http://184.72.239.149/vod/smil:BigBuckBunny.smil/media_w442897525_b560000_3.ts'
file 'http://184.72.239.149/vod/smil:BigBuckBunny.smil/media_w442897525_b560000_4.ts'
file 'http://184.72.239.149/vod/smil:BigBuckBunny.smil/media_w442897525_b560000_5.ts'

或者如果需要,可以使用此命令:

ffmpeg -f "concat" -safe "0" -protocol_whitelist "file,http,https,tcp,tls" -i "input.txt" -c copy -y output.ts

最后,为了下载速度很好,MAYBE我的服务器目标带宽有限。 :-(

ffmpeg concatenation m3u8
3个回答
4
投票

从m3u8播放列表中连接多个视频文件的正确方法是

ffmpeg -i "http://example.com/chunklist.m3u8" -codec copy file.mp4


  • m3u8播放列表可以在Web上或本地目录中 它包含相对于播放列表的文件路径列表
  • -codec copy避免编码(需要时间)
  • 容器类型事项: *.mp4很好,但是当从网络上获取播放列表时,多路复用似乎很慢 *.mkv*.ts对我来说效果最好

0
投票

你可以试试。

command: ffmpeg -y \ -v warning \ -loglevel debug \ -i "m3u8 url" \ -vcodec copy \ -c copy -f mpegts out.ts

将ts转换为mp4:

ffmpeg -i out.ts -acodec copy -vcodec copy out.mp4


0
投票

这里有一些python代码,你需要提供第一段的url和段数(来自.m3u8文件):

def dumpSegs(initUrl, n, path, append=False):
    """ downlaod and combine the .ts files
    given the first seg's url, the number of segments and
    the destination download path """
    with open(path, 'ab' if append else 'wb') as f:
        for i in range(1, n + 1):
            segurl = initUrl.replace('seg-1-', 'seg-{:d}-'.format(i))
            success = False
            while not success:
                try:
                    seg = requests.get(segurl, headers=HEADERS)
                    success = True
                except:
                    print('retrying...')
            f.write(seg.content)

Here的代码与更多的铃声和口哨相同

© www.soinside.com 2019 - 2024. All rights reserved.