Skip to content

Instantly share code, notes, and snippets.

@SophieShears
Created July 15, 2025 18:43
Show Gist options
  • Save SophieShears/384299f4442a6d3cbb690f3a074abf76 to your computer and use it in GitHub Desktop.
Save SophieShears/384299f4442a6d3cbb690f3a074abf76 to your computer and use it in GitHub Desktop.
file to complile as binary for m3u8 download with hang timeout error handling
# pip install m3u8_To_MP4
# Code to download m3u8 links as mp4 files
import signal
import sys
import m3u8_To_MP4
def handler(signum, frame):
raise Exception('Function timed out while attempting to download!')
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)
if __name__ == '__main__':
url = sys.argv[1]
fp = sys.argv[2]
directory = '/'.join(fp.split('/')[:-1])
fp = fp.split('/')[-1]
# Register the signal function handler
signal.signal(signal.SIGALRM, handler)
# Define a timeout for the download
timeout = 300 # How long to wait to exit the function if it hangs.
signal.alarm(timeout)
dl_m3u8(url, directory, fp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment