In case of a necessary, you may use my script as well
import requests
import m3u8
import shutil
import os
def strip_end(text, suffix):
if not text.endswith(suffix):
return text
return text[:len(text)-len(suffix)]
def download_file(url):
local_filename = url.split('/')[-1]
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
with open(f"ts_files/{local_filename}", 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
return local_filename
m3u8_url = 'your m3u8 url goes here'
r = requests.get(m3u8_url)
m3u8_master = m3u8.loads(r.text)
# print(m3u8_master.data['segments'])
m3u8_file = m3u8_url.split('/')[-1]
url = strip_end(m3u8_url, m3u8_file)
url_copy = url
if not os.path.exists('ts_files'):
print('ts_file folder is not found, creating the folder.')
os.makedirs('ts_files')
# print statement can be deleted, they were placed prior to debugging purposes.
for seg in m3u8_master.data['segments']:
print(url)
url += seg['uri']
print(url)
print(f'downloading {seg["uri"]}')
# download_file(url) # comment out this line to download ts files.
url = url_copy
print(url)
print()
# you should comment out the rest part if you want to merge your multiple ts files to one ts file.
"""
cwd = os.getcwd() # Get the current working directory (cwd)
TS_DIR = 'ts_files'
with open('merged.ts', 'wb') as merged:
for ts_file in os.listdir(f'{cwd}/{TS_DIR}'):
with open(f'{cwd}/{TS_DIR}/{ts_file}', 'rb') as mergefile:
shutil.copyfileobj(mergefile, merged)
"""