Skip to content

Instantly share code, notes, and snippets.

@triangletodd
Last active January 5, 2017 20:28
Show Gist options
  • Select an option

  • Save triangletodd/0189174ee83994f22db44f26cfaacc48 to your computer and use it in GitHub Desktop.

Select an option

Save triangletodd/0189174ee83994f22db44f26cfaacc48 to your computer and use it in GitHub Desktop.
Download and play youtube videos in mplayer with a single command.
#!/usr/bin/env bash
set -e
url="$1"
temp_dir=$(mktemp -d /tmp/yt.XXXXXXXXXXXXX)
validate_url() {
if [ -z $url ]; then
echo 'Usage: yt [url]'
exit 0
elif [[ ! $url =~ ^http(s)?://(www\.)?youtube\.com.*$ ]]; then
echo 'Please use a working youtube URL'
exit 0
fi
}
validate_programs() {
for i in $@; do
if [ ! -n $(which -s "$i") ]; then
echo "You need "$i" in your PATH to continue."
exit 0
fi
done
}
fetch_video() {
filename=$(youtube-dl -q --restrict-filenames --get-filename -o "${temp_dir}/%(id)s.%(ext)s" "$url")
youtube-dl -q --restrict-filenames -o "$filename" "$url"
}
play_video() {
mplayer -quiet -noborder -ontop -x 256 -y 144 "$filename"
}
main() {
validate_url
validate_programs youtube-dl mplayer
fetch_video
play_video
}
cleanup() {
rm -rf "$temp_dir"
}
trap cleanup EXIT
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment