Improve transcribe api and add alias to transcribe twitch stream downloads

This commit is contained in:
2023-06-04 11:57:32 -04:00
parent 8ff9c338b5
commit e5e8f309e5
8 changed files with 157 additions and 103 deletions

130
aliases
View File

@@ -278,6 +278,8 @@ custom_grep() {
local include_list=("$@")
local include_arg=""
if [[ $include_list != "" ]]; then
# We're looping like this instead of for var in "$@", because that way of looping is affecting
# my shell environment. Very strange!
for i in "${include_list[@]}"; do
include_arg+="--include=\*${i} "
done
@@ -511,18 +513,8 @@ download_twitch_chat() {
fi
}
# 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 <url> --cookies /c/<cookie_path>/twitch_cookies.txt`
#
# 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.
#
download_twitch_vid() {
# Copy pasta of download_twitch_vid with a final pass to transcribe the audio using whisper.cpp
download_twitch_vid_and_transcribe() {
local format="$1"
local shortname="$2"
local compress="$3"
@@ -533,7 +525,7 @@ download_twitch_vid() {
if [[ $url == "" ]]; then
error "Usage: $0 <make folder?> <url> <optional args>"
return
exit 1
fi
# We use yt-dlp to get the filename and then use streamlink to download it (the latter is a lot faster).
@@ -569,7 +561,7 @@ download_twitch_vid() {
if [[ $make_folder == "1" ]]; then
make_vid_dir_and_cd_into $url "" $opts
if [[ $? -ne 0 ]]; then
return
exit 1
fi
fi
@@ -607,6 +599,113 @@ download_twitch_vid() {
fi
else
error "Error: Failed to download '$url'"
exit 1
fi
transcribe-video "$filename" jon base small
if [[ $make_folder == "1" ]]; then
cd ..
fi
}
# 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 <url> --cookies /c/<cookie_path>/twitch_cookies.txt`
#
# 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.
#
download_twitch_vid() {
local format="$1"
local shortname="$2"
local compress="$3"
local make_folder="$4"
local url="$5"
shift 5
local opts="$@"
if [[ $url == "" ]]; then
error "Usage: $0 <make folder?> <url> <optional args>"
exit 1
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"
yt_dlp_format="b"
streamlink_format="best"
else
yt_dlp_format="$format"
streamlink_format="$format"
fi
if [[ $make_folder == "1" ]]; then
make_vid_dir_and_cd_into $url "" $opts
if [[ $? -ne 0 ]]; then
exit 1
fi
fi
if [[ $shortname -eq 0 ]]; then
local name_format="%(upload_date>%Y-%m-%d)s-%(title)s-tw-%(id)s"
else
local name_format="%(upload_date>%Y-%m-%d)s-shortname-tw-%(id)s"
fi
# Download Twitch chat transcript
actually_download_twitch_chat $url "$(yt-dlp.exe --get-filename -o "$name_format" $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
eval $cmd # Need to eval in order to preserve the quotes wrapping the filename format string.
error=$?
if [[ $error -eq 0 ]]; then
if [[ $compress -eq 1 ]]; then
local temp_name="temp_${RANDOM}"
# 0=cpu, 1=gpu
compress-video "$filename" "$temp_name" 0
extension="${filename##*.}"
mv "$filename" "orig_$filename"
mv $temp_name.$extension "$filename"
printf "${BOLD}Make sure to delete the original video file${NORMAL}\n"
fi
else
error "Error: Failed to download '$url'"
exit 1
fi
if [[ $make_folder == "1" ]]; then
@@ -815,6 +914,7 @@ alias yt-and-hflip='download_youtube_vid_and_hflip "137+140"' # 1080p
# Twitch Vid DL
alias tw='download_twitch_vid "" $SHORTNAME_OFF $COMPRESSION_OFF'
alias twt='download_twitch_vid_and_transcribe "" $SHORTNAME_OFF $COMPRESSION_OFF'
alias tw-compressed='download_twitch_vid "" $SHORTNAME_OFF $COMPRESSION_ON'
alias tw-shortname='download_twitch_vid "" $SHORTNAME_ON $COMPRESSION_OFF'
alias tw-shortname-compressed='download_twitch_vid "" $SHORTNAME_ON $COMPRESSION_ON'
@@ -856,7 +956,7 @@ alias vimeo-compressed='download_vimeo_vid "Original" $SHORTNAME_OFF $COMPRESSIO
alias ig-download-and-hflip='download_instagram_vid_and_hflip '
# Twitter Vid DL
alias twt='download_twitter_vid "" '
alias twitter='download_twitter_vid "" '
# Misc
alias download-mp4='download_mp4'