def videos(args): game_ids = _get_game_ids(args.game) print_out("<dim>Loading videos...</dim>") generator = twitch.channel_videos_generator( args.channel_name, args.limit, args.sort, args.type, game_ids=game_ids) first = 1 for videos, has_more in generator: count = len(videos["edges"]) if "edges" in videos else 0 total = videos["totalCount"] last = first + count - 1 print_out("-" * 80) print_out("<yellow>Showing videos {}-{} of {}</yellow>".format(first, last, total)) for video in videos["edges"]: print_video(video["node"]) if not args.pager: print_out( "\n<dim>There are more videos. " "Increase the --limit or use --pager to see the rest.</dim>" ) break if not has_more or not _continue(): break first += count else: print_out("<yellow>No videos found</yellow>")
def _print_progress(futures): downloaded_count = 0 downloaded_size = 0 max_msg_size = 0 start_time = datetime.now() total_count = len(futures) for future in as_completed(futures): size = future.result() downloaded_count += 1 downloaded_size += size percentage = 100 * downloaded_count // total_count est_total_size = int(total_count * downloaded_size / downloaded_count) duration = (datetime.now() - start_time).seconds speed = downloaded_size // duration if duration else 0 remaining = (total_count - downloaded_count) * duration / downloaded_count msg = " ".join([ "Downloaded VOD {}/{}".format(downloaded_count, total_count), "({}%)".format(percentage), "<cyan>{}</cyan>".format(format_size(downloaded_size)), "of <cyan>~{}</cyan>".format(format_size(est_total_size)), "at <cyan>{}/s</cyan>".format(format_size(speed)) if speed > 0 else "", "remaining <cyan>~{}</cyan>".format(format_duration(remaining)) if speed > 0 else "", ]) max_msg_size = max(len(msg), max_msg_size) print_out("\r" + msg.ljust(max_msg_size), end="")
def _download_clip(slug, args): print_out("<dim>Looking up clip...</dim>") clip = twitch.get_clip(slug) if not clip: raise ConsoleError("Clip '{}' not found".format(slug)) print_out( "Found: <green>{}</green> by <yellow>{}</yellow>, playing <blue>{}</blue> ({})" .format(clip["title"], clip["broadcaster"]["displayName"], clip["game"]["name"], utils.format_duration(clip["durationSeconds"]))) url = _get_clip_url(clip, args) print_out("<dim>Selected URL: {}</dim>".format(url)) url_path = urlparse(url).path extension = Path(url_path).suffix filename = "{}_{}{}".format(clip["broadcaster"]["login"], utils.slugify(clip["title"]), extension) print_out("Downloading clip...") download_file(url, filename) print_out("Downloaded: {}".format(filename))
def _join_vods(playlist_path, target): command = [ "ffmpeg", "-i", playlist_path, "-c", "copy", "-safe", "0", "-f", "concat", "-segment_time_metadata", "1", "-af", "aselect=concatdec_select,aresample=async=1", target, "-stats", "-y", "-loglevel", "warning", ] print_out("<dim>{}</dim>".format(" ".join(command))) result = subprocess.run(command) if result.returncode != 0: raise ConsoleError("Joining files failed")
def _join_vods(playlist_path, target, overwrite, video): command = [ "ffmpeg", "-i", playlist_path, "-c", "copy", "-metadata", "artist={}".format(video["creator"]["displayName"]), "-metadata", "title={}".format(video["title"]), "-metadata", "encoded_by=twitch-dl", "-stats", "-loglevel", "warning", target, ] if overwrite: command.append("-y") print_out("<dim>{}</dim>".format(" ".join(command))) result = subprocess.run(command) if result.returncode != 0: raise ConsoleError("Joining files failed")
def videos(args): game_ids = _get_game_ids(args.game) print_out("<dim>Loading videos...</dim>") generator = twitch.channel_videos_generator(args.channel_name, args.limit, args.sort, args.type, game_ids=game_ids) first = 1 for videos, has_more in generator: count = len(videos["edges"]) if "edges" in videos else 0 total = videos["totalCount"] last = first + count - 1 for video in videos["edges"]: print_video(video["node"]) if not has_more: break first += count else: print_out("<yellow>No videos found</yellow>")
def _select_playlist_interactive(playlists): print_out("\nAvailable qualities:") for n, (name, resolution, uri) in enumerate(playlists): print_out("{}) {} [{}]".format(n + 1, name, resolution)) no = utils.read_int("Choose quality", min=1, max=len(playlists) + 1, default=1) _, _, uri = playlists[no - 1] return uri
def _select_quality(playlists): print_out("\nAvailable qualities:") for n, p in enumerate(playlists): name = p.media[0].name if p.media else "" resolution = "x".join(str(r) for r in p.stream_info.resolution) print_out("{}) {} [{}]".format(n + 1, name, resolution)) no = read_int("Choose quality", min=1, max=len(playlists) + 1, default=1) return playlists[no - 1]
def _continue(): print_out("\nThere are more videos. " "Press <green><b>Enter</green> to continue, " "<yellow><b>Ctrl+C</yellow> to break.") try: input() except KeyboardInterrupt: return False return True
def _print_video(video): published_at = video['published_at'].replace('T', ' @ ').replace('Z', '') length = format_duration(video['length']) name = video['channel']['display_name'] print_out("\n<bold>{}</bold>".format(video['_id'][1:])) print_out("<green>{}</green>".format(video["title"])) print_out("<cyan>{}</cyan> playing <cyan>{}</cyan>".format( name, video['game'])) print_out("Published <cyan>{}</cyan> Length: <cyan>{}</cyan> ".format( published_at, length)) print_out("<i>{}</i>".format(video["url"]))
def _select_quality(playlists): print_out("\nAvailable qualities:") for no, v in playlists.items(): print_out("{}) {}".format(no, v[0])) keys = list(playlists.keys()) no = read_int("Choose quality", min=min(keys), max=max(keys), default=keys[0]) return playlists[no]
def _get_game_ids(names): if not names: return [] game_ids = [] for name in names: print_out("<dim>Looking up game '{}'...</dim>".format(name)) game_id = twitch.get_game_id(name) if not game_id: raise ConsoleError("Game '{}' not found".format(name)) game_ids.append(int(game_id)) return game_ids
def _clips_download(args): generator = twitch.channel_clips_generator(args.channel_name, args.period, 100) for clips, _ in generator: for clip in clips["edges"]: clip = clip["node"] url = clip["videoQualities"][0]["sourceURL"] target = _clip_target_filename(clip) if path.exists(target): print_out( "Already downloaded: <green>{}</green>".format(target)) else: print_out("Downloading: <yellow>{}</yellow>".format(target)) download_file(url, target)
def _download_clip(slug, args): print_out("<dim>Looking up clip...</dim>") clip = twitch.get_clip(slug) print_out("Found: <green>{}</green> by <yellow>{}</yellow>, playing <blue>{}</blue> ({})".format( clip["title"], clip["broadcaster"]["displayName"], clip["game"]["name"], utils.format_duration(clip["durationSeconds"]) )) print_out("\nAvailable qualities:") qualities = clip["videoQualities"] for n, q in enumerate(qualities): print_out("{}) {} [{} fps]".format(n + 1, q["quality"], q["frameRate"])) no = utils.read_int("Choose quality", min=1, max=len(qualities), default=1) selected_quality = qualities[no - 1] url = selected_quality["sourceURL"] url_path = urlparse(url).path extension = Path(url_path).suffix filename = "{}_{}{}".format( clip["broadcaster"]["login"], utils.slugify(clip["title"]), extension ) print("Downloading clip...") download_file(url, filename) print("Downloaded: {}".format(filename))
def clip_info(clip): print_out() print_clip(clip) print_out() print_out("Download links:") for q in clip["videoQualities"]: print_out("<b>{quality}p{frameRate}</b> {sourceURL}".format(**q))
def get_clip_authenticated_url(slug, quality): print_out("<dim>Fetching access token...</dim>") access_token = twitch.get_clip_access_token(slug) if not access_token: raise ConsoleError("Access token not found for slug '{}'".format(slug)) url = _get_clip_url(access_token, quality) query = urlencode({ "sig": access_token["playbackAccessToken"]["signature"], "token": access_token["playbackAccessToken"]["value"], }) return "{}?{}".format(url, query)
def video_info(video, playlists): print_out() print_video(video) print_out() print_out("Playlists:") for p in m3u8.loads(playlists).playlists: print_out("<b>{}</b> {}".format(p.stream_info.video, p.uri))
def _download_video(video_id, max_workers, format='mp4', start=None, end=None, keep=False, **kwargs): if start and end and end <= start: raise ConsoleError("End time must be greater than start time") _log(video_id, "Recherche la video ") video = twitch.get_video(video_id) _log(video_id, "Informations sur {}".format(video['title'])) print_out("Trouvé: <blue>{}</blue> by <yellow>{}</yellow>".format( video['title'], video['channel']['display_name'])) access_token = twitch.get_access_token(video_id) _log(video_id, "Obtention de la liste des fichiers...") playlists = twitch.get_playlists(video_id, access_token) parsed = m3u8.loads(playlists) selected = _select_quality(parsed.playlists) # print_out("\nListe...") response = requests.get(selected.uri) response.raise_for_status() playlist = m3u8.loads(response.text) base_uri = re.sub("/[^/]+$", "/", selected.uri) target_dir = _crete_temp_dir(base_uri) filenames = list(_get_files(playlist, start, end)) # Save playlists for debugging purposes with open(target_dir + "playlists.m3u8", "w") as f: f.write(playlists) with open(target_dir + "playlist.m3u8", "w") as f: f.write(response.text) # print_out("\nTélécharge {} VODs avec {} threads dans {}".format( # len(filenames), max_workers, target_dir)) file_paths = download_files(video_id, base_uri, target_dir, filenames, max_workers) target = _video_target_filename(video, format) print_out("\nCible: {}".format(target)) _join_vods(target_dir, file_paths, "videos/Download/{}".format(target)) if keep: print_out("\nTemporary files not deleted: {}".format(target_dir)) else: # print_out("\nSupprime le fichier temporaire...") shutil.rmtree(target_dir) print_out("Fichier téléchargé: {}".format(target)) _log(video_id, "Terminé {}".format(target))
def videos(channel_name, limit, offset, sort, **kwargs): user = twitch.get_user(channel_name) if not user: raise ConsoleError("Utilisateur {} non trouvé.".format(channel_name)) videos = twitch.get_channel_videos(user["id"], limit, offset, sort) count = len(videos['videos']) if not count: print_out("Pas de vidéos") return first = offset + 1 last = offset + len(videos['videos']) total = videos["_total"] for video in videos['videos']: print_video(video)
def _print_progress(futures): counter = 1 total = len(futures) total_size = 0 start_time = datetime.now() for future in as_completed(futures): size = future.result() percentage = 100 * counter // total total_size += size duration = (datetime.now() - start_time).seconds speed = total_size // duration if duration else 0 remaining = (total - counter) * duration / counter msg = "Downloaded VOD {}/{} ({}%) total <cyan>{}B</cyan> at <cyan>{}B/s</cyan> remaining <cyan>{}</cyan>".format( counter, total, percentage, format_size(total_size), format_size(speed), format_duration(remaining)) print_out("\r" + msg.ljust(80), end='') counter += 1
def _join_vods(playlist_path, target, overwrite): command = [ "ffmpeg", "-i", playlist_path, "-c", "copy", target, "-stats", "-loglevel", "warning", ] if overwrite: command.append("-y") print_out("<dim>{}</dim>".format(" ".join(command))) result = subprocess.run(command) if result.returncode != 0: raise ConsoleError("Joining files failed")
def _clips_download(args): downloaded_count = 0 generator = twitch.channel_clips_generator(args.channel_name, args.period, 100) for clips, _ in generator: for clip in clips["edges"]: clip = clip["node"] url = get_clip_authenticated_url(clip["slug"], "source") target = _clip_target_filename(clip) if path.exists(target): print_out( "Already downloaded: <green>{}</green>".format(target)) else: print_out("Downloading: <yellow>{}</yellow>".format(target)) download_file(url, target) downloaded_count += 1 if args.limit and downloaded_count >= args.limit: return
def _get_clip_url(clip, args): qualities = clip["videoQualities"] # Quality given as an argument if args.quality: selected_quality = args.quality.rstrip( "p") # allow 720p as well as 720 for q in qualities: if q["quality"] == selected_quality: return q["sourceURL"] available = ", ".join([str(q["quality"]) for q in qualities]) msg = "Quality '{}' not found. Available qualities are: {}".format( args.quality, available) raise ConsoleError(msg) # Ask user to select quality print_out("\nAvailable qualities:") for n, q in enumerate(qualities): print_out("{}) {} [{} fps]".format(n + 1, q["quality"], q["frameRate"])) print_out() no = utils.read_int("Choose quality", min=1, max=len(qualities), default=1) selected_quality = qualities[no - 1] return selected_quality["sourceURL"]
def _download_clip(slug, args): print_out("<dim>Looking up clip...</dim>") clip = twitch.get_clip(slug) if not clip: raise ConsoleError("Clip '{}' not found".format(slug)) print_out( "Found: <green>{}</green> by <yellow>{}</yellow>, playing <blue>{}</blue> ({})" .format(clip["title"], clip["broadcaster"]["displayName"], clip["game"]["name"], utils.format_duration(clip["durationSeconds"]))) url = _get_clip_url(clip, args) print_out("<dim>Selected URL: {}</dim>".format(url)) target = _clip_target_filename(clip) print_out("Downloading clip...") download_file(url, target) print_out("Downloaded: {}".format(target))
def clips(args): if args.json: return _clips_json(args) if args.download: return _clips_download(args) print_out("<dim>Loading clips...</dim>") generator = twitch.channel_clips_generator(args.channel_name, args.period, args.limit) first = 1 for clips, has_more in generator: count = len(clips["edges"]) if "edges" in clips else 0 last = first + count - 1 print_out("-" * 80) print_out("<yellow>Showing clips {}-{} of ??</yellow>".format( first, last)) for clip in clips["edges"]: print_clip(clip["node"]) if not args.pager: print_out( "\n<dim>There are more clips. " "Increase the --limit or use --pager to see the rest.</dim>") break if not has_more or not _continue(): break first += count else: print_out("<yellow>No clips found</yellow>")
def videos(channel_name, limit, offset, sort, **kwargs): print_out("Looking up user...") user = twitch.get_user(channel_name) if not user: raise ConsoleError("User {} not found.".format(channel_name)) print_out("Loading videos...") videos = twitch.get_channel_videos(user["id"], limit, offset, sort) count = len(videos['videos']) if not count: print_out("No videos found") return first = offset + 1 last = offset + len(videos['videos']) total = videos["_total"] print_out("<yellow>Showing videos {}-{} of {}</yellow>".format( first, last, total)) for video in videos['videos']: _print_video(video)
def _download_video(video_id, args): if args.start and args.end and args.end <= args.start: raise ConsoleError("End time must be greater than start time") if os.path.isfile(str(Path.home()) + "/.twitchdownloads/" + video_id): print("File already downloaded") return print_out("<dim>Looking up video...</dim>") video = twitch.get_video(video_id) save_json_video(video_id, video, _video_target_filename(video, "json")) print_out("Found: <blue>{}</blue> by <yellow>{}</yellow>".format( video['title'], video['channel']['display_name'])) print_out("<dim>Fetching access token...</dim>") access_token = twitch.get_access_token(video_id) print_out("<dim>Fetching playlists...</dim>") playlists_m3u8 = twitch.get_playlists(video_id, access_token) playlists = list(_parse_playlists(playlists_m3u8)) playlist_uri = (_get_playlist_by_name(playlists, args.quality) if args.quality else _select_playlist_interactive(playlists)) print_out("<dim>Fetching playlist...</dim>") response = requests.get(playlist_uri) response.raise_for_status() playlist = m3u8.loads(response.text) base_uri = re.sub("/[^/]+$", "/", playlist_uri) target_dir = _crete_temp_dir(base_uri) vod_paths = _get_vod_paths(playlist, args.start, args.end) # Save playlists for debugging purposes with open(path.join(target_dir, "playlists.m3u8"), "w") as f: f.write(playlists_m3u8) with open(path.join(target_dir, "playlist.m3u8"), "w") as f: f.write(response.text) print_out("\nDownloading {} VODs using {} workers to {}".format( len(vod_paths), args.max_workers, target_dir)) path_map = download_files(base_uri, target_dir, vod_paths, args.max_workers) # Make a modified playlist which references downloaded VODs # Keep only the downloaded segments and skip the rest org_segments = playlist.segments.copy() playlist.segments.clear() for segment in org_segments: if segment.uri in path_map: segment.uri = path_map[segment.uri] playlist.segments.append(segment) playlist_path = path.join(target_dir, "playlist_downloaded.m3u8") playlist.dump(playlist_path) print_out("\n\nJoining files...") target = _video_target_filename(video, args.format) _join_vods(playlist_path, target) if args.keep: print_out( "\n<dim>Temporary files not deleted: {}</dim>".format(target_dir)) else: print_out("\n<dim>Deleting temporary files...</dim>") shutil.rmtree(target_dir) print_out("\nDownloaded: <green>{}</green>".format(target))
def _download_video(video_id, args): if args.start and args.end and args.end <= args.start: raise ConsoleError("End time must be greater than start time") print_out("<dim>Looking up video...</dim>") video = twitch.get_video(video_id) if not video: raise ConsoleError("Video {} not found".format(video_id)) print_out("Found: <blue>{}</blue> by <yellow>{}</yellow>".format( video['title'], video['creator']['displayName'])) target = _video_target_filename(video, args) print_out("Output: <blue>{}</blue>".format(target)) if not args.overwrite and path.exists(target): response = input("File exists. Overwrite? [Y/n]: ") if response.lower().strip() not in ["", "y"]: raise ConsoleError("Aborted") args.overwrite = True print_out("<dim>Fetching access token...</dim>") access_token = twitch.get_access_token(video_id) print_out("<dim>Fetching playlists...</dim>") playlists_m3u8 = twitch.get_playlists(video_id, access_token) playlists = list(_parse_playlists(playlists_m3u8)) playlist_uri = (_get_playlist_by_name(playlists, args.quality) if args.quality else _select_playlist_interactive(playlists)) print_out("<dim>Fetching playlist...</dim>") response = requests.get(playlist_uri) response.raise_for_status() playlist = m3u8.loads(response.text) base_uri = re.sub("/[^/]+$", "/", playlist_uri) target_dir = _crete_temp_dir(base_uri) vod_paths = _get_vod_paths(playlist, args.start, args.end) # Save playlists for debugging purposes with open(path.join(target_dir, "playlists.m3u8"), "w") as f: f.write(playlists_m3u8) with open(path.join(target_dir, "playlist.m3u8"), "w") as f: f.write(response.text) print_out("\nDownloading {} VODs using {} workers to {}".format( len(vod_paths), args.max_workers, target_dir)) path_map = download_files(base_uri, target_dir, vod_paths, args.max_workers) # Make a modified playlist which references downloaded VODs # Keep only the downloaded segments and skip the rest org_segments = playlist.segments.copy() playlist.segments.clear() for segment in org_segments: if segment.uri in path_map: segment.uri = path_map[segment.uri] playlist.segments.append(segment) playlist_path = path.join(target_dir, "playlist_downloaded.m3u8") playlist.dump(playlist_path) if args.no_join: print_out("\n\n<dim>Skipping joining files...</dim>") print_out("VODs downloaded to:\n<blue>{}</blue>".format(target_dir)) return print_out("\n\nJoining files...") _join_vods(playlist_path, target, args.overwrite, video) if args.keep: print_out( "\n<dim>Temporary files not deleted: {}</dim>".format(target_dir)) else: print_out("\n<dim>Deleting temporary files...</dim>") shutil.rmtree(target_dir) print_out("\nDownloaded: <green>{}</green>".format(target))
def download(video_id, max_workers, format='mkv', start=None, end=None, **kwargs): video_id = parse_video_id(video_id) if start and end and end <= start: raise ConsoleError("End time must be greater than start time") print_out("Looking up video...") video = twitch.get_video(video_id) print_out("Found: <blue>{}</blue> by <yellow>{}</yellow>".format( video['title'], video['channel']['display_name'])) print_out("Fetching access token...") access_token = twitch.get_access_token(video_id) print_out("Fetching playlists...") playlists = twitch.get_playlists(video_id, access_token) quality, playlist_url = _select_quality(playlists) print_out("\nFetching playlist...") base_url, filenames = twitch.get_playlist_urls(playlist_url, start, end) if not filenames: raise ConsoleError("No vods matched, check your start and end times") # Create a temp dir to store downloads if it doesn't exist directory = '{}/twitch-dl/{}/{}'.format(tempfile.gettempdir(), video_id, quality) pathlib.Path(directory).mkdir(parents=True, exist_ok=True) print_out("Download dir: {}".format(directory)) print_out("Downloading {} VODs using {} workers...".format( len(filenames), max_workers)) paths = _download_files(base_url, directory, filenames, max_workers) print_out("\n\nJoining files...") target = _video_target_filename(video, format) _join_vods(directory, paths, target) print_out("\nDeleting vods...") for path in paths: os.unlink(path) print_out("\nDownloaded: {}".format(target))
def download(video_id, max_workers, format='mkv', start=None, end=None, keep=False, **kwargs): video_id = _parse_video_id(video_id) if start and end and end <= start: raise ConsoleError("End time must be greater than start time") print_out("Looking up video...") video = twitch.get_video(video_id) print_out("Found: <blue>{}</blue> by <yellow>{}</yellow>".format( video['title'], video['channel']['display_name'])) print_out("Fetching access token...") access_token = twitch.get_access_token(video_id) print_out("Fetching playlists...") playlists = twitch.get_playlists(video_id, access_token) parsed = m3u8.loads(playlists) selected = _select_quality(parsed.playlists) print_out("\nFetching playlist...") response = requests.get(selected.uri) response.raise_for_status() playlist = m3u8.loads(response.text) base_uri = re.sub("/[^/]+$", "/", selected.uri) target_dir = _crete_temp_dir(base_uri) filenames = list(_get_files(playlist, start, end)) # Save playlists for debugging purposes with open(target_dir + "playlists.m3u8", "w") as f: f.write(playlists) with open(target_dir + "playlist.m3u8", "w") as f: f.write(response.text) print_out("\nDownloading {} VODs using {} workers to {}".format( len(filenames), max_workers, target_dir)) _download_files(base_uri, target_dir, filenames, max_workers) print_out("\n\nJoining files...") target = _video_target_filename(video, format) _join_vods(target_dir, filenames, target) if keep: print_out("\nTemporary files not deleted: {}".format(target_dir)) else: print_out("\nDeleting temporary files...") shutil.rmtree(target_dir) print_out("Downloaded: {}".format(target))