twitch-dl/twitchdl/commands.py

263 lines
7.7 KiB
Python
Raw Normal View History

2019-08-23 10:36:05 +00:00
import m3u8
import os
import pathlib
2018-01-25 10:09:20 +00:00
import re
2019-08-23 10:36:05 +00:00
import requests
import shutil
2018-01-25 10:09:20 +00:00
import subprocess
import tempfile
from concurrent.futures import ThreadPoolExecutor, as_completed
2019-08-23 10:36:05 +00:00
from datetime import datetime
2018-01-25 10:09:20 +00:00
from functools import partial
2019-08-23 10:36:05 +00:00
from urllib.parse import urlparse
2018-01-25 10:09:20 +00:00
from twitchdl import twitch
from twitchdl.download import download_file
from twitchdl.exceptions import ConsoleError
2018-01-25 10:09:20 +00:00
from twitchdl.output import print_out
from twitchdl.utils import slugify
2018-01-25 10:09:20 +00:00
def read_int(msg, min, max, default):
msg = msg + " [default {}]: ".format(default)
while True:
try:
val = input(msg)
if not val:
return default
if min <= int(val) <= max:
return int(val)
except ValueError:
pass
2018-01-26 20:50:55 +00:00
def format_size(bytes_):
if bytes_ < 1024:
return str(bytes_)
kilo = bytes_ / 1024
if kilo < 1024:
return "{:.1f}K".format(kilo)
mega = kilo / 1024
if mega < 1024:
return "{:.1f}M".format(mega)
return "{:.1f}G".format(mega / 1024)
def format_duration(total_seconds):
total_seconds = int(total_seconds)
hours = total_seconds // 3600
remainder = total_seconds % 3600
2018-01-25 10:09:20 +00:00
minutes = remainder // 60
2018-01-26 20:50:55 +00:00
seconds = total_seconds % 60
2018-01-25 10:09:20 +00:00
if hours:
2018-01-26 20:50:55 +00:00
return "{} h {} min".format(hours, minutes)
if minutes:
return "{} min {} sec".format(minutes, seconds)
2018-01-25 10:09:20 +00:00
2018-01-26 20:50:55 +00:00
return "{} sec".format(seconds)
2018-01-25 10:09:20 +00:00
def _print_video(video):
published_at = video['published_at'].replace('T', ' @ ').replace('Z', '')
2018-01-26 20:50:55 +00:00
length = format_duration(video['length'])
2018-01-25 10:09:20 +00:00
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))
2019-08-13 10:29:42 +00:00
print_out("<i>{}</i>".format(video["url"]))
2018-01-25 10:09:20 +00:00
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))
2018-01-25 10:09:20 +00:00
for video in videos['videos']:
_print_video(video)
def _select_quality(playlists):
2019-04-30 11:34:54 +00:00
print_out("\nAvailable qualities:")
2019-08-23 10:36:05 +00:00
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))
2018-01-25 10:09:20 +00:00
2019-08-23 10:36:05 +00:00
no = read_int("Choose quality", min=1, max=len(playlists) + 1, default=1)
2018-01-25 10:09:20 +00:00
2019-08-23 10:36:05 +00:00
return playlists[no - 1]
2018-01-25 10:09:20 +00:00
def _print_progress(futures):
counter = 1
total = len(futures)
2018-01-26 20:50:55 +00:00
total_size = 0
start_time = datetime.now()
2018-01-25 10:09:20 +00:00
for future in as_completed(futures):
size = future.result()
2018-01-26 20:50:55 +00:00
percentage = 100 * counter // total
total_size += size
2018-01-26 20:50:55 +00:00
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='')
2018-01-25 10:09:20 +00:00
counter += 1
def _download_files(base_url, directory, filenames, max_workers):
2019-08-23 10:36:05 +00:00
urls = [base_url + f for f in filenames]
paths = ["/".join([directory, f]) for f in filenames]
partials = (partial(download_file, url, path) for url, path in zip(urls, paths))
2018-01-25 10:09:20 +00:00
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = [executor.submit(fn) for fn in partials]
2018-01-25 10:09:20 +00:00
_print_progress(futures)
return paths
2018-01-25 10:09:20 +00:00
def _join_vods(directory, paths, target):
2018-01-25 10:09:20 +00:00
input_path = "{}/files.txt".format(directory)
with open(input_path, 'w') as f:
for path in paths:
f.write('file {}\n'.format(os.path.basename(path)))
2018-01-25 10:09:20 +00:00
result = subprocess.run([
"ffmpeg",
"-f", "concat",
"-i", input_path,
"-c", "copy",
target,
"-stats",
"-loglevel", "warning",
])
result.check_returncode()
def _video_target_filename(video, format):
match = re.search(r"^(\d{4})-(\d{2})-(\d{2})T", video['published_at'])
date = "".join(match.groups())
name = "_".join([
date,
video['_id'][1:],
video['channel']['name'],
slugify(video['title']),
])
return name + "." + format
2018-01-25 10:09:20 +00:00
2019-08-23 10:36:05 +00:00
def _parse_video_id(video_id):
"""This can be either a integer ID or an URL to the video on twitch."""
if re.search(r"^\d+$", video_id):
return int(video_id)
match = re.search(r"^https://www.twitch.tv/videos/(\d+)(\?.+)?$", video_id)
if match:
return int(match.group(1))
raise ConsoleError("Invalid video ID given, expected integer ID or Twitch URL")
2019-08-23 10:36:05 +00:00
def _get_files(playlist, start, end):
"""Extract files for download from playlist."""
vod_start = 0
for segment in playlist.segments:
vod_end = vod_start + segment.duration
# `vod_end > start` is used here becuase it's better to download a bit
# more than a bit less, similar for the end condition
start_condition = not start or vod_end > start
end_condition = not end or vod_start < end
if start_condition and end_condition:
yield segment.uri
vod_start = vod_end
def _crete_temp_dir(base_uri):
"""Create a temp dir to store downloads if it doesn't exist."""
path = urlparse(base_uri).path
directory = '{}/twitch-dl{}'.format(tempfile.gettempdir(), path)
pathlib.Path(directory).mkdir(parents=True, exist_ok=True)
return directory
def download(video_id, max_workers, format='mkv', start=None, end=None, keep=False, **kwargs):
2019-08-23 10:36:05 +00:00
video_id = _parse_video_id(video_id)
if start and end and end <= start:
raise ConsoleError("End time must be greater than start time")
2019-04-30 11:34:54 +00:00
print_out("Looking up video...")
2018-01-25 10:09:20 +00:00
video = twitch.get_video(video_id)
2019-04-30 11:34:54 +00:00
print_out("Found: <blue>{}</blue> by <yellow>{}</yellow>".format(
video['title'], video['channel']['display_name']))
print_out("Fetching access token...")
2018-01-25 10:09:20 +00:00
access_token = twitch.get_access_token(video_id)
2019-08-23 10:36:05 +00:00
# TODO: save playlists for debugging purposes
2019-04-30 11:34:54 +00:00
print_out("Fetching playlists...")
2018-01-25 10:09:20 +00:00
playlists = twitch.get_playlists(video_id, access_token)
2019-08-23 10:36:05 +00:00
playlists = m3u8.loads(playlists)
selected = _select_quality(playlists.playlists)
2018-01-25 10:09:20 +00:00
2019-04-30 11:34:54 +00:00
print_out("\nFetching playlist...")
2019-08-23 10:36:05 +00:00
response = requests.get(selected.uri)
response.raise_for_status()
playlist = m3u8.loads(response.text)
2019-08-23 10:36:05 +00:00
base_uri = re.sub("/[^/]+$", "/", selected.uri)
target_dir = _crete_temp_dir(base_uri)
filenames = list(_get_files(playlist, start, end))
2018-01-25 10:09:20 +00:00
2019-08-23 10:36:05 +00:00
print_out("\nDownloading {} VODs using {} workers to {}".format(
len(filenames), max_workers, target_dir))
_download_files(base_uri, target_dir, filenames, max_workers)
2018-01-25 10:09:20 +00:00
2019-04-30 11:34:54 +00:00
print_out("\n\nJoining files...")
target = _video_target_filename(video, format)
2019-08-23 10:36:05 +00:00
_join_vods(target_dir, filenames, target)
2018-01-25 10:09:20 +00:00
if keep:
2019-08-23 10:36:05 +00:00
print_out("\nTemporary files not deleted: {}".format(target_dir))
else:
2019-08-23 10:36:05 +00:00
print_out("\nDeleting temporary files...")
shutil.rmtree(target_dir)
2018-01-25 10:09:20 +00:00
print_out("Downloaded: {}".format(target))