From 90ce7c09aa0c62699ba845f1fe90cfb746483469 Mon Sep 17 00:00:00 2001 From: Michael Campagnaro Date: Mon, 28 Mar 2022 13:06:02 -0400 Subject: [PATCH] Use Streamlink to download twitch vods/live streams (faster than yt-dlp) --- aliases | 54 +++++++++++++------ ...-video-portion => compress-video-and-trim} | 0 2 files changed, 39 insertions(+), 15 deletions(-) rename bin/{compress-video-portion => compress-video-and-trim} (100%) diff --git a/aliases b/aliases index ba67f48..c50b3bd 100644 --- a/aliases +++ b/aliases @@ -353,7 +353,7 @@ make_vid_dir_and_cd_into() { return 0 } -# Download YouTube videos +# Download YouTube videos. Note that yt-dlp downloads a lot faster than streamlink. dl_youtube_vid() { local format="$1" local shortname="$2" @@ -514,15 +514,16 @@ dl_twitch_chat() { cd .. } -# Download Twitch videos. -# If you want to download subcriber-only vids then first extract your twitch -# cookies to a file (can use cookies.txt addon from Lennon Hill) and then pass it as an option, +# Download Twitch videos, both VODs and live streams. Pass a Twitch account URL to download a live stream. +# The live stream filename will not contain the stream title, so you'll need to modify it afterwards. +# +# If you want to download subcriber-only vids then first extract your Twitch +# cookies to a file (can use cookies.txt add-on from Lennon Hill) and then pass it as an option, # using the full path to the cookies file, e.g. # `tw-1080p60 --cookies /c//twitch_cookies.txt` # -# To extract a portion of a video, you have to first download the entire file and then -# pass these args to ffmpeg: `-ss HH:MM:SS : start time to take`, `-to HH:MM:SS : end time`. -# Or use the script `compress-video-portion` +# To extract a portion of a video, you have to first download the entire file and then use the +# `trim-video` or `compress-video-and-trim` scripts. # dl_twitch_vid() { local format="$1" @@ -538,15 +539,34 @@ dl_twitch_vid() { return fi + # We use yt-dlp to get the filename and then use streamlink to download it (the latter is a lot faster). + # It's a two step process because streamlink cannot pass the formatted filename to ffmpeg. + # We fallback to yt-dlp when it's a subscriber VOD because we don't have an easy way to access it with streamlink. + + local subscriber_vod=0 + local split_opts=($opts) + if [[ ${split_opts[0]} == "--cookies" ]]; then + subscriber_vod=1 + printf "${BOLD}Subscriber VOD. Will use yt-dlp to download.${NORMAL}\n" + fi + if [[ $compress -eq 0 ]]; then printf "${BOLD}Downloading Twitch vid with no compression.${NORMAL}\n" else printf "${BOLD}Downloading Twitch vid with compression.${NORMAL}\n" fi + + local yt_dlp_format="" + local streamlink_format="" + if [[ $format == "" ]]; then # Twitch only supplies pre-merged mp4s so we can ask for the best format and not worry about anything else. printf "${BOLD}No format given; using best available.${NORMAL}\n" - format="b" + yt_dlp_format="b" + streamlink_format="best" + else + yt_dlp_format="$format" + streamlink_format="$format" fi if [[ $make_folder == "1" ]]; then @@ -565,10 +585,16 @@ dl_twitch_vid() { # Download Twitch chat transcript actually_dl_twitch_chat $url "$(yt-dlp.exe --get-filename -o "$name_format" $opts $url)" - # Download the video. - local filename=$(yt-dlp.exe --get-filename -f $format -o "$name_format.%(ext)s" $opts $url) + # Get the video filename. + local filename=$(yt-dlp.exe --get-filename -o "$name_format.%(ext)s" $opts $url) + + # Download + if [[ $subscriber_vod -eq 0 ]]; then + local cmd="streamlink.exe --twitch-low-latency --twitch-disable-ads --twitch-disable-hosting --force --force-progress $opts $url $streamlink_format -O | ffmpeg -i pipe:0 -c copy \"$filename\"" + else + local cmd="yt-dlp.exe -f $yt_dlp_format -o \"$filename\" $opts $url" + fi - local cmd="yt-dlp.exe -f $format -o \"$filename\" $opts $url" eval $cmd # Need to eval in order to preserve the quotes wrapping the filename format string. error=$? @@ -737,8 +763,6 @@ alias yt-audio='yt-dlp.exe -f "140"' alias yt-and-hflip='dl_youtube_vid_and_hflip "137+140"' # 1080p # Twitch Vid DL -alias tw-src='dl_twitch_vid "Source" $SHORTNAME_OFF $COMPRESSION_OFF' - alias tw='dl_twitch_vid "" $SHORTNAME_OFF $COMPRESSION_OFF' alias tw-compressed='dl_twitch_vid "" $SHORTNAME_OFF $COMPRESSION_ON' alias tw-shortname='dl_twitch_vid "" $SHORTNAME_ON $COMPRESSION_OFF' @@ -763,8 +787,8 @@ alias tw-1080p50-shortname-compressed='dl_twitch_vid "1080p50" $SHORTNAME_ON $CO alias tw-720='dl_twitch_vid "720p" $SHORTNAME_OFF $COMPRESSION_OFF' alias tw-720-compressed='dl_twitch_vid "720p" $SHORTNAME_OFF $COMPRESSION_ON' alias tw-720-shortname='dl_twitch_vid "720p" $SHORTNAME_ON $COMPRESSION_OFF' -alias tw-720-60='dl_twitch_vid "720p60" $SHORTNAME_OFF $COMPRESSION_OFF' -alias tw-720-60-shortname='dl_twitch_vid "720p60" $SHORTNAME_ON $COMPRESSION_OFF' +alias tw-720p60='dl_twitch_vid "720p60" $SHORTNAME_OFF $COMPRESSION_OFF' +alias tw-720p60-shortname='dl_twitch_vid "720p60" $SHORTNAME_ON $COMPRESSION_OFF' alias tw-4k='dl_twitch_vid "2160p" $SHORTNAME_OFF $COMPRESSION_OFF' alias tw-4k-compressed='dl_twitch_vid "2160p" $SHORTNAME_OFF $COMPRESSION_ON' diff --git a/bin/compress-video-portion b/bin/compress-video-and-trim similarity index 100% rename from bin/compress-video-portion rename to bin/compress-video-and-trim