Skip to content

Instantly share code, notes, and snippets.

@eyecatchup
Created June 30, 2017 06:02
Show Gist options
  • Save eyecatchup/b8fd82e0c03fa8c25424840c98e7e18f to your computer and use it in GitHub Desktop.
Save eyecatchup/b8fd82e0c03fa8c25424840c98e7e18f to your computer and use it in GitHub Desktop.
playlist2mp3.sh - Small, useful shell wrapper for `youtube-dl`
#!/usr/bin/env sh
YTDL_UNIVERSAL_PATH="Python27/Scripts/youtube-dl"
help() {
echo ""
echo "playlist2mp3 - A small, useful youtube-dl wrapper."
echo ""
echo "Download a YouTube playlist and for each video,"
echo "extract its audio stream & save it to an mp3 file."
echo ""
echo " Usage: playlist2mp3.sh [OPTIONS] YTPLAYLIST_ID"
exit 1
}
no_args() {
echo ""
echo -e "[ERROR] Invalid argument count. Requires a playlist id, at least."
help
}
no_platform() {
echo -e "[ERROR] Failed detecting shell envorinment. Aborting.."
exit 1
}
no_prefix() {
echo -e "[ERROR] Failed setting a path prefix. Aborting.."
exit 1
}
detect_platform() {
case "$1" in
C:*) echo "win";; # is it a windows cmd?
/cygdrive*) echo "cyg";; # or maybe a cygwin mintty?
/c/*) echo "git";; # or is it a common bash shell?
*) no_platform;;
esac
}
set_path_prefix() {
case "$1" in
win) echo "C:";;
cyg) echo "/cygdrive/c";;
git) echo "/c";;
*) no_prefix;;
esac
}
# Check arguments
if [ $# == 0 ]; then
no_args
fi;
echo ""
echo "Checking system.."
# Detect platform context
PLTFRM=`detect_platform "${0}"`
echo -e " Shell context: $PLTFRM"
# Set platform-specific path vars
PATH_PREFIX=`set_path_prefix $PLTFRM`
YTDL_BIN="$PATH_PREFIX/$YTDL_UNIVERSAL_PATH"
echo -e " Binary path: $YTDL_BIN"
YT_URL="https://www.youtube.com/playlist?list=$1"
echo "Okay. All set."
echo -e " Starting (bulk) download & conversion now.."
echo ""
# Start (bulk) download and conversion
$YTDL_BIN --format mp4 --merge-output-format mp4 \
--extract-audio --audio-format mp3 --audio-quality 0 \
--yes-playlist -o "%(autonumber)s-%(title)s.%(ext)s" \
--add-metadata --restrict-filenames --no-overwrites \
--prefer-ffmpeg --hls-prefer-ffmpeg \
--ignore-errors --geo-bypass \
$YT_URL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment