Skip to content

Instantly share code, notes, and snippets.

@bbaster
Last active February 12, 2025 14:01
Show Gist options
  • Save bbaster/3b7164ad6577708e105dd583960f6cba to your computer and use it in GitHub Desktop.
Save bbaster/3b7164ad6577708e105dd583960f6cba to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#A script for playing YouTube playlists
#Recommended mpv plugins: https://github.com/hoyon/mpv-mpris, https://github.com/cniw/mpv-discordRPC
trap "echo Exiting...; kill -s KILL 0; exit" SIGINT SIGTERM
trap "exit" SIGKILL
browser=$(xdg-mime query default application/x-extension-html)
browser=${browser%%.desktop}
browser=${browser:-firefox}
if ! [ -e headers.txt ]; then touch headers.txt; fi
while read -r header; do
[[ "$header" =~ ^([A-Za-z-]+)\:\ (.+) ]]
variable=${BASH_REMATCH[1]}
value=${BASH_REMATCH[2]}
if [ -n "$variable" ] && ! [[ "$variable" =~ (Host|Accept-Encoding) ]]; then headers="$headers --add-header $variable:\"$value\""; fi
done < headers.txt #Right click on a network request and click "Copy Request Headers"
echo "Input YouTube playlist link: "
read -r link
[[ "$link" =~ ^https?\:\/\/(www|m)\.youtube\.com\/watch\?v\=[A-Za-z0-9_-]+\&list\=([A-Za-z0-9_-]+) ]]
if [ -n "${BASH_REMATCH[2]}" ]; then
id="${BASH_REMATCH[2]}"
else
echo "Error: Invalid link!"
exit 1
fi
if ! [ -s "$id.json" ]; then
echo "Fetching playlist JSON from YouTube..."
eval yt-dlp -j --flat-playlist --cookies-from-browser "$browser" -- "\"$link\"" > "$id.json"
fi
echo "Parsing JSON..."
jq -r '.webpage_url' "$id.json" > "$id.txt"
if [ "$?" -ne 0 ]; then
echo "Error: Invalid JSON!"
rm "$id.json"
exit 1
fi
i=0
while read -r link; do
links[i]="$link"
i=$((i+1))
done < "$id.txt"
#Workaround for a HTTP 403 issue - don't use browser cookies, 16.01.2025
echo Running yt-dlp --embed-metadata --embed-thumbnail --merge-output-format mkv --sponsorblock-remove sponsor,intro,music_offtopic "$headers" -o "\"%(id)s.%(ext)s\"" -- "\"${links[0]}\""
eval yt-dlp --embed-metadata --embed-thumbnail --merge-output-format mkv --sponsorblock-remove sponsor,intro,music_offtopic "$headers" -o "\"%(id)s.%(ext)s\"" -- "\"${links[0]}\""
for ((i=1; i<=${#links[@]}; i++)); do
#https://www.youtube.com/watch?v=DvyCbevQbtI
[[ "${links[i-1]}" =~ ^https?\:\/\/(www|m)\.youtube\.com\/watch\?v\=([A-Za-z0-9_-]+)\&* ]]
id="${BASH_REMATCH[2]}"
echo Running yt-dlp --embed-metadata --embed-thumbnail --merge-output-format mkv --sponsorblock-remove sponsor,intro,music_offtopic "$headers" -o "\"%(id)s.%(ext)s\"" -- "\"${links[i]}\""
eval yt-dlp --embed-metadata --embed-thumbnail --merge-output-format mkv --sponsorblock-remove sponsor,intro,music_offtopic "$headers" -o "\"%(id)s.%(ext)s\"" -- "\"${links[i]}\"" && \
echo "yt-dlp finished!" &
while ! file=$(find -maxdepth 1 -name "$id.*" -not -name '*.ytdl' -not -name '*.part' -size +0c); do
sleep 0.1
echo "[$(date '+%H:%M:%S')] Waiting for $id.*..."
done
[[ "$file" =~ ^(\./)?[A-Za-z0-9_-]+\.(.*) ]]
ext="${BASH_REMATCH[2]}"
mpv --window-minimized=yes -- "$id.$ext"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment