Skip to content

Instantly share code, notes, and snippets.

@johnmccabe
Last active January 26, 2016 21:37
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 johnmccabe/714abb85af1e691e58a5 to your computer and use it in GitHub Desktop.
Save johnmccabe/714abb85af1e691e58a5 to your computer and use it in GitHub Desktop.
Bash function(s) to download Youtube videos using Docker
docker-toolbox() {
DOCKER_VM=$(docker-machine ip default)
DOCKER_HOST=tcp://${DOCKER_VM}:2376
DOCKER_CERT_PATH=~/.docker/machine/machines/default
DOCKER_TLS_VERIFY=1
export DOCKER_VM DOCKER_HOST DOCKER_CERT_PATH DOCKER_TLS_VERIFY
if [ ! "$1" = "-s" ]; then
echo "Using Docker Host: ${DOCKER_HOST}"
fi
}

Add the bash functions to your profile, either directly in ~/.bash_profile or preferably by loading the snippets from ~/.bash_profile.d - for example add the following to your .bash_profile:

# Run snippets
for script in ~/.bash_profile.d/*.sh ; do
        if [ -r $script ] ; then
                . $script
        fi
done

Source your updated profile and you can then download source vids from Youtube as follows:

Download By default it outputs to ./video.mp4

youtube-dl https://www.youtube.com/watch?v=WPsu4OH9hsk

Download to specific file You can download to a specific location/filename

youtube-dl https://www.youtube.com/watch?v=WPsu4OH9hsk ~/brooklyn-gource.mp4
# from - http://stackoverflow.com/a/7126780
dir_resolve()
{
cd "$1" 2>/dev/null || return $? # cd to desired directory; if fail, quell any error messages but return exit status
echo "`pwd -P`" # output full, link-resolved path
}
###
# Author: @johnmccabe
# Date: 16/1/16
#
# Bash function to download Youtube videos using jbergknoff/youtube-dl
# Docker image.
#
# Requires:
# utils.fn.sh#dir_resolve
# docker.fn.sh#docker-toolbox
###
youtube-dl(){
video_url=$1
output=$2
if [ $# -eq 0 ]; then
echo "usage: youtube-dl <video url> [<output file>]"
return 0
fi
if [ -z "$output" ]; then
output_dir="."
abs_output_dir=$(pwd)
output_file="video.mp4"
else
if [ -d "$output" ]; then
echo "Supplied output file is a directory. Exiting..."
return 1
fi
if [ -e "$output" ]; then
echo "Supplied output file already exists. Exiting..."
return 1
fi
output_dir=`dirname ${output}`
# Prompt to create output directory if not found
if [ ! -d "${output_dir}" ] ; then
read -p "Output directory does not exist. Do you want to create it? [yN]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "creating dir [${output_dir}] ..."
mkdir -p ${output_dir}
else
echo "exiting..."
return 0
fi
fi
# use utils#dir_resolve to get absolute path
abs_output_dir=`dir_resolve ${output_dir}`
output_file=`basename ${output}`
fi
# Try to setup docker environment variables
docker-toolbox
echo "Downloading ${video_url} to ${output_dir}/${output_file} ..."
docker run -it --rm -v "${abs_output_dir}:/src" jbergknoff/youtube-dl \
-f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]' \
-o /src/${output_file} \
${video_url}
echo
echo "Video downloaded to ${output_dir}/${output_file}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment