下载.ts文件

时间:2018-08-17 14:37:53

标签: java android-studio mp4 avi m3u8

我想从服务器下载.ts流,并将其本地存储在android手机中。 我能够检索.m3u8文件,但不知道如何实际下载这些块。

为什么我要问这个问题,因为这些.ts块的数量为1950,却不知道如何立即下载这些块并将其转换为单个文件。

.m3u8的外观如下:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="subtitles",NAME="English",AUTOSELECT=YES,DEFAULT=YES,LANGUAGE="en",URI="master_subtitle.m3u8"
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-STREAM-INF:BANDWIDTH=96632,AVERAGE-BANDWIDTH=82657,CODECS="avc1.42c015,mp4a.40.5",RESOLUTION=250x140,SUBTITLES="subtitles",FRAME-RATE=10.000
master_Layer1.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=167696,AVERAGE-BANDWIDTH=141886,CODECS="avc1.42c015,mp4a.40.5",RESOLUTION=320x180,SUBTITLES="subtitles",FRAME-RATE=15.000
master_Layer2.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=329376,AVERAGE-BANDWIDTH=280906,CODECS="avc1.42c015,mp4a.40.2",RESOLUTION=320x180,SUBTITLES="subtitles",FRAME-RATE=25.000
master_Layer3.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=598968,AVERAGE-BANDWIDTH=505629,CODECS="avc1.42c01e,mp4a.40.2",RESOLUTION=416x234,SUBTITLES="subtitles",FRAME-RATE=25.000
master_Layer4.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=1102432,AVERAGE-BANDWIDTH=914396,CODECS="avc1.42c01e,mp4a.40.2",RESOLUTION=640x360,SUBTITLES="subtitles",FRAME-RATE=25.000
master_Layer5.m3u8 

这就是说明块的故事的文件的样子:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:4
#EXT-X-MEDIA-SEQUENCE:1
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:4.000,
master_Layer1_00001.ts
#EXTINF:4.000,
master_Layer1_00002.ts
#EXTINF:4.000,
master_Layer1_00003.ts
#EXTINF:4.000,
master_Layer1_00004.ts
#EXTINF:4.000,
master_Layer1_00005.ts
#EXTINF:4.000,
master_Layer1_00006.ts
#EXTINF:4.000,
master_Layer1_00007.ts
#EXTINF:4.000,
master_Layer1_00008.ts
#EXTINF:4.000,
master_Layer1_00009.ts
#EXTINF:4.000,
master_Layer1_00010.ts
...
#EXTINF:0.600,
master_Layer1_02150.ts
#EXT-X-ENDLIST 

下载网址类似于 https://abcd.com/path1/path2/master_Layer*.ts

现在我需要知道如何立即下载这些块并将它们转换为单个文件包,例如.mp4吗?

2 个答案:

答案 0 :(得分:0)

Python库- m3u8-dl

pip安装m3u8-dl

m3u8-dl-帮助

答案 1 :(得分:0)

来源:Download TS files from video stream 试试这个:

import urllib.request
import os
import shutil

# Paste the address for one (any) video chunk ending with .ts
my_lessons = [
# paste your https://YOUR_SITE.com/A_VIDEO_CHUNK.ts address here
]

lesson_dir = "my_vids"
try:
    shutil.rmtree(lesson_dir)
except:
    print("ok")

os.makedirs(lesson_dir)
os.chdir(lesson_dir)

for lesson, dwn_link in enumerate(my_lessons):
    print("downloading lesson  %d.. " % (lesson), dwn_link)
    file_name = '%04d.mp4' % lesson
    f = open(file_name, 'ab')
    for x in range(0, 1200):
        try:
            rsp = urllib.request.urlopen(dwn_link + "_%04d.ts" % (x) )
        except:
            break
        file_name = '%d.mp4' % lesson
        print("downloading  %d.ts" % (x))
        f.write(rsp.read())
    f.close()

print ("================== Done - Find your video at", lesson_dir, "- Good luck! ==================")