Skip to content

Instantly share code, notes, and snippets.

@aont
Created July 10, 2019 06:16
Show Gist options
  • Select an option

  • Save aont/103501f1415e46a4980b9275567cee83 to your computer and use it in GitHub Desktop.

Select an option

Save aont/103501f1415e46a4980b9275567cee83 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Usage:
# $ ./m3u8_mod.py $M3U8URL | ffmpeg -allowed_extensions ALL -protocol_whitelist pipe,file,http,https,tcp,tls,crypto -i - -movflags faststart -c copy out.mp4
import sys
import requests
import re
if __name__ == u'__main__':
m3u8_url = sys.argv[1]
# sys.stderr.write("[info] m3u8: %s\n" % m3u8_url)
question_idx = m3u8_url.rfind('?')
if question_idx != -1:
query = m3u8_url[question_idx:]
else:
query = ""
question_idx = len(m3u8_url)
slash_idx = m3u8_url.rfind('/', 0, question_idx)
if slash_idx == -1:
raise Exception("unexpected")
else:
base_url = m3u8_url[:slash_idx+1]
sess = requests.session()
result = sess.get(m3u8_url)
for line in result.text.split('\n'):
if len(line)==0:
pass
elif line[0] != '#':
print(base_url + line + query)
else:
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment