'Downloading .ts files

I want to download .ts streams from server and store them locally in android phone. i am able to retrieve the .m3u8 file but don't know how to actually download these chunks.

Why i am asking this question because these .ts chunks are 1950 in numbers and don't know how to download these at once and convert to a single file.

this is how the .m3u8 looks like :

#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 

and this is how the file which says the story of the chunks look like :

#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 

The download url is something like https://abcd.com/path1/path2/master_Layer*.ts

now i need to know how to download these chunks at once and convert them to a single file package such as .mp4 ?



Solution 1:[1]

Python library - m3u8-dl

pip install m3u8-dl

m3u8-dl --help

Solution 2:[2]

Source: Download TS files from video stream Try this:

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! ==================")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kbrahma
Solution 2 jeppoo1