Skip to content

Instantly share code, notes, and snippets.

@SophieShears
Last active July 15, 2025 17:28
Show Gist options
  • Save SophieShears/b241f0bd5a66edb63c578fe7cf106183 to your computer and use it in GitHub Desktop.
Save SophieShears/b241f0bd5a66edb63c578fe7cf106183 to your computer and use it in GitHub Desktop.
Download m3u8 links as mp4 and will raise error if the download process hangs.
# pip install m3u8_To_MP4
# Code to download m3u8 links as mp4 files
import m3u8_To_MP4
import os
import signal
def handler(signum, frame):
raise Exception('Function timed out')
def dl_m3u8(url, directory, filename):
# Note directory should be the filepath to save the file ex: '/home/username/bin'
# Directory can be a relative or absolute path
# Filename should end with an .mp4 ex: 'test.mp4'
try:
m3u8_To_MP4.multithread_download(
url,
mp4_file_dir=directory,
mp4_file_name=filename
)
except Exception as e:
print(e)
# Test it works
# Create directory for testing
directory = 'test_output'
if not os.path.isdir(directory):
os.mkdir(directory)
url = 'https://stream.mux.com/PjVO02ZBOj01Fxmf15ifIQPZAlIHv02GodT.m3u8'
filepath = 'output.mp4'
# Register the signal function handler
signal.signal(signal.SIGALRM, handler)
# Define a timeout for the download
timeout = 300 # How long to wait until timeout the function if it hangs.
signal.alarm(timeout)
dl_m3u8(url, directory, filepath)
# Should create a 'test_output' directory in the active directory
# Then produce an mp4 called 'output.mp4' there
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment