Skip to content

Instantly share code, notes, and snippets.

@nagayev
Last active March 27, 2022 18:50
Show Gist options
  • Save nagayev/0b86de051d5c9ca2072cbf7f8ecf79fe to your computer and use it in GitHub Desktop.
Save nagayev/0b86de051d5c9ca2072cbf7f8ecf79fe to your computer and use it in GitHub Desktop.
Zen downloader
import os
import sys
def get_m3u8_url():
f=open('tmp.html')
i=0
found = False
needle='m3u8'
flen=os.path.getsize('tmp.html')
content=''
while i<flen:
content = f.read(1000)
if needle in content:
index = content.rindex(needle)
found = True
break
i+=1000
f.close()
if not found:
raise RuntimeError('Coudn\'t download the video')
pos=content.index('http')
m3u8=content.index('m3u8')
return content[pos:m3u8+4]
def main():
url = ''
if len(sys.argv)!=2:
if len(sys.argv)==1:
url = input('Enter the url: ')
else:
print('Invalid script usage!')
print('Usage: python downloader.py video_url')
exit(1)
else:
url = sys.argv[1]
print('Loading html document...')
os.system(f'curl {url} > tmp.html')
print('Done!')
m3u8_url = get_m3u8_url()
m3u8_url = m3u8_url[m3u8_url.rindex('http'):]
print(f'm3u8 url is {m3u8_url}')
print('Calling ffmpeg...')
os.system(f'ffmpeg -i {m3u8_url} -c copy -bsf:a aac_adtstoasc output.mp4')
print('Saved as output.mp4')
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment