0

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 ?

Mark
  • 39
  • 1
  • 4
wiils
  • 143
  • 2
  • 12
  • Have you already tried something? Please post the code of what you have already tried? – Yashovardhan99 Aug 17 '18 at 17:58
  • @Yashovardhan I am really confused and stuck now. First thing is downloading these files and other is converting them to a single package as `.MP4` i didn't found any libraries that can do this work. – wiils Aug 18 '18 at 08:09
  • @Yashovardhan this is what i've found for FFMPEG for android https://github.com/writingminds/ffmpeg-android-java not updated maintained. – wiils Aug 18 '18 at 08:52
  • @Yashovardhan and the FFMPEG from the link is having a serious problem that it increases the size of app by 21mb !! and please let me know if you any code sample to download these chunks and convert them into my app. – wiils Aug 18 '18 at 17:50

2 Answers2

0

Python library - m3u8-dl

pip install m3u8-dl

m3u8-dl --help

kbrahma
  • 1
  • 1
  • 1
0

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! ==================")
jeppoo1
  • 650
  • 1
  • 10
  • 23