Skip to content

Instantly share code, notes, and snippets.

@Emory-M
Last active April 8, 2023 05:38
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Emory-M/4a3cb1787bc73c02285fd2fabf470858 to your computer and use it in GitHub Desktop.
Save Emory-M/4a3cb1787bc73c02285fd2fabf470858 to your computer and use it in GitHub Desktop.
Download m3u8 and merge ts file with python urllib2
# coding: utf8
import sys, urllib2
import socks
from sockshandler import SocksiPyHandler
from urlparse import urlparse
if len(sys.argv) < 2:
print 'Usage: python %s [url]' % sys.argv[0]
exit()
def parseurl():
url = sys.argv[1]
urlgroup = urlparse(url)
host = urlgroup.scheme + '://' + urlgroup.hostname
file = urlgroup.path.split('/')[-1].split('.')[0] + '.ts'
opener = urllib2.build_opener(SocksiPyHandler(socks.SOCKS5, '127.0.0.1'))
response = opener.open(url)
print 'm3u8 done!'
count = 0
line = response.readline().strip()
while line:
if not line.startswith('#') and line != '':
ts = opener.open(host+line).read()
tsfile = open(file, 'ab')
tsfile.write(ts)
count += 1
print 'part %s done!' % count
line = response.readline().strip()
print 'file: ' + file
if __name__ == "__main__":
try:
parseurl()
except KeyboardInterrupt:
print ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment