Skip to content

Instantly share code, notes, and snippets.

@ErebusBat
Created March 19, 2015 15:52
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 ErebusBat/6e695fb17bb1443f365e to your computer and use it in GitHub Desktop.
Save ErebusBat/6e695fb17bb1443f365e to your computer and use it in GitHub Desktop.
ytcurl - youtuble-dl wrapper script
#!/usr/bin/env zsh
set -e
# set -x # trace
################################################################################
# ytcurl - youtuble-dl wrapper script
#
# This is a simple wrapper script that I throw in my path to aid in downloading
# stuff from youtube on the fly. I threw it together very quick one day so it
# will probably not be exactly what you need, but it is here for reference
################################################################################
################################################################################
# OPTIONS
################################################################################
# The following variables are blank by default
# YT_URL=
# YT_USERNAME=
# START_AT=
# MAX_DOWNLOADS=
# Variables with defaults, but can be EXPORTed by calling script
if [ -z $SLEEP ]; then SLEEP=3; fi
if [ -z $AUDIO_FORMAT ]; then AUDIO_FORMAT=m4a; fi
### URL: Set / check
if [ ! -z $1 ]; then
# This is wrapped in a if block so that $YT_URL can be exported/inherited by
# our parent
if [ ! -z $YT_URL ]; then
echo "*** WARNING: You have a \$YT_URL set, but also specified one on the command"
echo " line. Using the command line version."
echo " "
echo " Old \$YT_URL: $YT_URL"
echo " New \$YT_URL: $1"
fi
YT_URL=$1
fi
if [ -z $YT_URL ]; then
echo "*** ERROR: Specify \$YT_URL or as first argument!"
exit 1
fi
################################################################################
# OPTIONS
################################################################################
# Static Options
OPTS=(
--write-info-json
--console-title
--output="./%(title)s.%(ext)s"
)
# Misc Optional
if [ ! -z $SLEEP ]; then
if [ $SLEEP -gt 0 ]; then OPTS+=(--sleep-interval=$SLEEP); fi
fi
if [ ! -z $START_AT ]; then OPTS+=(--playlist-start=$START_AT); fi
if [ ! -z $MAX_DOWNLOADS ]; then OPTS+=(--max-downloads=$MAX_DOWNLOADS); fi
# Authentication
if [ ! -z $YT_USERNAME ]; then
OPTS+=(--username=$YT_USERNAME)
# 2FA
echo -n "Enter Two Factor code for $YT_USERNAME: "
read TWOFACTOR
OPTS+=(--twofactor=$TWOFACTOR)
fi
# Audio Extraction
if [ ! -z $AUDIO_FORMAT ]; then
OPTS+=(
--extract-audio
--audio-format=$AUDIO_FORMAT
--keep-video
--add-metadata
)
fi
################################################################################
# Main Execution
################################################################################
# Display what is to be executed
echo "youtube-dl \\"
for o in $OPTS; do
echo " $o \\"
done
echo " $YT_URL"
# URL has to be the last thing, added after the output aboveo so that
# the terminating \ are correct
OPTS+=($YT_URL)
# DO IT!
youtube-dl ${(Q)OPTS}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment