Skip to content

Instantly share code, notes, and snippets.

@aavezel
Created November 26, 2019 06:15
Show Gist options
  • Save aavezel/37b6df499779438c8edac93e70672949 to your computer and use it in GitHub Desktop.
Save aavezel/37b6df499779438c8edac93e70672949 to your computer and use it in GitHub Desktop.
Download and clue video from kinokong
#!/usr/bin/env python36
# -*- coding: utf-8 -*-
"""
Download m3u8 from kinokong
[!] need ffmpeg installed from system
"""
import sys
import tempfile
import sh
import m3u8
from progressbar import Bar, ProgressBar, SimpleProgress, AdaptiveETA
class tmp_environment:
def __init__(self):
self.tmp_dir = tempfile.TemporaryDirectory()
def __enter__(self):
self.return_dir = sh.pwd().next().strip()
sh.cd(self.tmp_dir.name)
def __exit__(self, exc_type, exc_value, tb):
sh.cd(self.return_dir)
self.tmp_dir.cleanup()
def choise(lst, desc):
print("==== "+desc+" ====")
while True:
for idx, item in enumerate(lst):
print("{idx}: {item}".format(idx=idx, item=item))
try:
option = int(input("select option: "))
except ValueError as ex:
option = -1
if 0 <= option < len(lst):
return option
print("Option selection error. Try again.")
def choice_list(master):
if len(master.playlists) == 1:
playlist = master.playlists[0]
else:
playlist_format = "{program_id} - bandwidth:{bandwidth}, resolution: {resolution}, codecs: {codecs}"
choise_list = [playlist_format.format(**p.stream_info.__dict__) for p in master.playlists]
idx = choise(choise_list, "Select video")
playlist = master.playlists[idx]
medias = playlist.media
if len(medias) == 1:
media = medias[0]
else:
media_format = "{name} ({language}) autoselect: {autoselect}"
choise_list = [media_format.format(**m.__dict__) for m in playlist.media if m.type == "AUDIO"]
idx = choise(choise_list, "Select audio")
media = playlist.media[idx]
return (playlist, media)
def download_segment(desc, info, result):
playlist = m3u8.load(info.absolute_uri)
widgets = ['%s: ' % (desc), SimpleProgress(), ' ', Bar(), ' ', AdaptiveETA()]
pbar = ProgressBar(widgets=widgets, maxval=len(playlist.files)).start()
for i, segment in enumerate(playlist.segments):
sh.wget(segment.absolute_uri)
pbar.update(i)
pbar.finish()
sh.ffmpeg("-i", "concat:" + "|".join(playlist.files), "-c:v", "copy", "-c:a", "copy", result)
for file in playlist.files:
sh.rm("-f", file)
print("%s: OK" % (desc))
return result
def main(episode, url):
return_dir = sh.pwd().next().strip()
with tmp_environment() as env:
master = m3u8.load(url)
(playlist, media) = choice_list(master)
video = download_segment("Video download", playlist, "video.mp4")
audio = download_segment("Audio download", media, "audio.mp4")
episode_name = episode + ".mp4"
sh.ffmpeg("-i", video, "-i", audio, "-c:v", "copy", "-c:a", "copy", episode_name)
sh.mv(episode_name, return_dir)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: download.py episode m3u8_uri")
exit(1)
episode = sys.argv[1]
url = sys.argv[2]
main(episode, url)
m3u8==0.5.1
sh==1.12.14
progressbar2==3.47.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment