Skip to content

Instantly share code, notes, and snippets.

@jae-jae
Last active May 21, 2019 01:58
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 jae-jae/10be327fa01b1cc0cfe2f22ce3a2905b to your computer and use it in GitHub Desktop.
Save jae-jae/10be327fa01b1cc0cfe2f22ce3a2905b to your computer and use it in GitHub Desktop.
download m3u8
import PySimpleGUI as sg
import requests
import os
import uuid
from threading import Thread
def downloadM3u8(url, window):
window.FindElement('mark').Update('Downloading ...')
_getVideo(url, window)
window.FindElement('mark').Update('Done')
def _getVideo(url, window):
if url.endswith('/'):
url = url[:-1]
baseUrl = url.rsplit('/', 1)[0]
resp = requests.get(url)
lines = resp.text.split('\n')
if not lines or lines[0] != '#EXTM3U':
return
filename = '{}.mp4'.format(uuid.uuid1())
if os.path.exists(filename):
os.remove(filename)
count = len(lines)
base = 100 / count
with open(file=filename, mode='wb+') as fp:
for i in range(count):
window.FindElement('progbar').UpdateBar(base * (i + 1))
line = lines[i]
if line.endswith('.ts'):
seg = baseUrl + '/' + line
resp = requests.get(seg)
if resp.status_code == 200:
fp.write(resp.content)
layout = [
[sg.Text('Input m3u8 url here:')],
[sg.Multiline('', key='url', size=(80, 10))],
[sg.ProgressBar(100, size=(30, 20), key='progbar'), sg.Text('Downloading ...', key='mark')],
[sg.Submit(), sg.Cancel()],
]
window = sg.Window('Download m3u8').Layout(layout)
while True:
event, values = window.Read()
if event is None or event == 'Cancel':
break
url = values['url'].strip()
if url:
Thread(target=downloadM3u8, args=(url, window)).start()
else:
window.FindElement('mark').Update('No url found')
continue
window.Close()
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
pysimplegui = "*"
requests = "*"
uuid = "*"
[requires]
python_version = "3.7"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment