Skip to content

Instantly share code, notes, and snippets.

@goncalomb
Last active October 17, 2022 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goncalomb/eda8a47d199c35eee9fc7426ef5782be to your computer and use it in GitHub Desktop.
Save goncalomb/eda8a47d199c35eee9fc7426ef5782be to your computer and use it in GitHub Desktop.
Another YouTube downloader script.
#!/bin/bash
# Copyright (c) 2021 Gonçalo Baltazar <me@goncalomb.com>
# MIT License
# pip3 install --upgrade youtube-dl
# ffmpeg -y -loglevel repeat+info -i file:video.xyz -i file:audio.xyz -c copy -map 0:v:0 -map 1:a:0 file:out.mkv
set -eo pipefail
cd -- "$(dirname -- "$0")"
VERSION=2.0
now() {
date +%s.%N
}
ytdl() {
yt-dlp \
--no-continue \
--write-description \
--write-info-json \
--write-annotations \
--write-thumbnail \
--write-all-thumbnails \
--all-subs \
--write-sub \
--write-pages \
--playlist-reverse \
-o '%(id)s.%(ext)s' \
-f bestvideo+bestaudio/best \
--keep-video \
-i \
-v \
"$@"
}
dl_single() {
SVC=$1
ARC_NAME=$2
ID=$3
URL=$4
CMD=$5
if [ -f "data/archive.txt" ] && grep -F "$ARC_NAME" "data/archive.txt" > /dev/null; then
echo "$ID already in the archive"
return
fi
START=$(now)
RUN_DIR="data/$SVC/single/$ID.$START"
mkdir -p "$RUN_DIR"
(
cd "$RUN_DIR"
end() {
now > end
echo end
}
trap end EXIT
echo "$VERSION" > version
"$CMD" --download-archive "../../../archive.txt" "$URL" 2>&1 | tee -i log.txt
)
}
youtube_single() {
dl_single youtube "youtube $1" "$1" "https://www.youtube.com/watch?v=$1" ytdl
}
twitch_single() {
dl_single twitch "twitchvod v$1" "$1" "https://www.twitch.tv/videos/$1" ytdl
}
if [ "$1" == "youtube-single" ]; then
youtube_single "$2"
elif [ "$1" == "twitch-single" ]; then
twitch_single "$2"
else
echo "youtube-single,twitch-single?"
exit 1
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment