Skip to content

Instantly share code, notes, and snippets.

@bbayles
Last active October 13, 2022 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbayles/d4df156ecbd4a162f279ed2f8465124e to your computer and use it in GitHub Desktop.
Save bbayles/d4df156ecbd4a162f279ed2f8465124e to your computer and use it in GitHub Desktop.
"""Quick and dirty HLS transport stream downloader.
Give it a URI that points to a media playlist (one that contains transport stream URIs,
not a multivariant playlist that contains other playlist URIs).
It will read the playlist, download all the segments, and write them to your chosen
output location.
A single output file is created. This works because MPEG Transport Stream files
can be concatenated.
"""
from argparse import ArgumentParser
from concurrent.futures import ThreadPoolExecutor
from sys import argv
from m3u8 import loads as m3u8_loads
from requests import Session
def main(args=None):
args = args or argv[1:]
argument_parser = ArgumentParser(description='HLS transport stream downloader')
argument_parser.add_argument('playlist_uri', type=str, help='Playlist location')
argument_parser.add_argument(
'output_path', type=str, help='Output transport stream location'
)
argument_parser.add_argument(
'--threads', type=int, default=8, help='Number of concurrent downloads'
)
parsed_args = argument_parser.parse_args()
playlist_uri = parsed_args.playlist_uri
max_workers = parsed_args.threads
output_path = parsed_args.output_path
pool = ThreadPoolExecutor(max_workers=max_workers)
session = Session()
outfile = open(output_path, 'wb')
with pool, session, outfile:
playlist_text = session.get(playlist_uri).text
parsed_playlist = m3u8_loads(playlist_text, uri=playlist_uri)
all_uris = (s.absolute_uri for s in parsed_playlist.segments)
for data in pool.map(lambda uri: session.get(uri).content, all_uris):
outfile.write(data)
if __name__ == '__main__':
main()
@bbayles
Copy link
Author

bbayles commented Oct 13, 2022

You'll need to run python -m pip install m3u8 requests to download the required libraries for this script. Then run it with python ./hls_ts_download.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment