Get the TS Stream Video URL, which might look something like the following
- http://example.com/stream/ABC.mp4/seg-1.ts
- http://example.com/stream/ABC.mp4/seg-2.ts
- http://example.com/stream/ABC.mp4/seg-3.ts
Notice the URLs are similar except a running rumber, which usually represent the segment/chunk/part number in sequence.
Below is a python script which download all the segment/chunk/part. The script use grequests to allow faster simultenous download.
import osimport requestsimport grequests# name of the videoname = 'ABC'# .ts URL of the video, replace segment number with {0}source_url = 'http://example.com/stream/ABC.mp4/seg-{0}.ts'if not os.path.exists(name): os.makedirs(name)keys = []urls = []for x in range(1,2000): filename = '{0}/{1:04d}.ts'.format(name, x) if os.path.isfile(filename): print 'skip', x continue urls.append(source_url.format(x)) keys.append(x) # allow 6 simultenous download if len(urls) == 6: rs = (grequests.get(u) for u in urls) results = grequests.map(rs) for key, _result in enumerate(results): filename = '{0}/{1:03d}.ts'.format(name, keys[key]) print urls[key], _result.status_code if _result.status_code == 200: with open(filename, 'wb') as f: f.write(_result.content) else: # if error assume no more segment, or you can run this script again to make sure it's a permanent error exit() urls = [] keys = []
After all the .ts
files are downloaded, run the following command to merge all the .ts
files.
cat ABC/*.ts > ABC.ts
Install ffmpeg.
sudo add-apt-repository ppa:mc3man/trusty-mediasudo apt-get updatesudo apt-get install ffmpeg
Run the following ffmpeg
command to convert .ts
to .mp4
.
ffmpeg -i ABC.ts -bsf:a aac_adtstoasc -acodec copy -vcodec copy ABC.mp4