Download YouTube videos with a shell script
- February 6th, 2010
- Posted in IT . Random stuffs
- Write comment
In my last post I wrote about extracting audio from in-streaming-format videos, that people often may get from content streaming services like eg. YouTube. As said, those videos may be downloaded in several ways that may also be different depending on the hosting service. A pretty effective way is using some plugin for your browser that shows the URL of the source the flash/html5 player download the video from, and allow you to get the content from that. Anyway, just for fun, here is a small shell script that can be used to download videos contents from youtube.
#!/bin/bash # ################################################################ # --- YouTube Downloader ------------------------------------- # # --- http://www.n0on3.net ----------------------------------- # ################################################################ # which wget &>/dev/null; if [ $? -ne 0 ];then echo "> Please install wget =)!"; exit; fi if [ $# -ne 1 ];then read -p "> Enter Youtube video ID: " VID else VID=$1; fi read -p "> Do you want me to try to get the HD version ? [y|N]: " HD case $HD in Y|y|yes) FMT=22;; N|n|*)FMT=18;; esac OK=1; PAGE=`tempfile` wget -q -O $PAGE "http://www.youtube.com/watch?v=$VID" SIG=`cat $PAGE | egrep -o "\"t\": \"[^\"]+\"" | \ sed 's/\"//g' | awk '{print $2}'` URL="http://www.youtube.com/get_video?fmt=$FMT&video_id=$VID&t=$SIG" TITLE=`cat $PAGE | grep VIDEO_TITLE | awk '{$1="";print $0}' ` TITLE=$(echo $TITLE | sed 's/.\(.*\)../\1/' | sed 's/\\//g') read -p "> Is the title \" $TITLE \" right for output file ? [Y|n]: " TOK case $TOK in n|N|no) read -p "> Enter filename: " TITLE ;; Y|y|yes|*) ;; esac; echo "> Saving file to $TITLE.mp4 ... " wget -q -O "$TITLE.mp4" $URL &>/dev/null if [ $? -ne 0 ];then echo "> Download failed!"; OK=0; rm -fr "$TITLE.mp4"; if [ $FMT == 22 ];then echo "> Maybe the HD version isn't available.." FMT=18; echo "> Downloading the non-HD version ..." URL="http://www.youtube.com/get_video?fmt=$FMT&video_id=$VID&t=$SIG"; wget -q -O "$TITLE.mp4" $URL &>/dev/null; if [ $? -ne 0 ];then echo "> Download failed!"; rm -fr "$TITLE.mp4"; else OK=1; fi; fi fi if [ $OK -eq 1 ]; then echo "> Successfully Downloaded to $TITLE.mp4 !" fi; #
To be said, youtube term of service, at point 6.C, states using YouTube you accept not to access the contents with any technology but the YouTube website player or other stuff explicitly authorized by YouTube. ( That means, yeah, you should really not use this u.u” ).



Keep up the good work, I like your writing.
sadly your solution is not working anymore. seems like youtube changed the layout of the links.
maybe its possible to fix it with a resolution selector aswell?
@vany No time to dedicate at the moment
But if you can post some details about the new ‘layout of the links’, it could be really quick and my pleasure to fix it
There’s a new script for linux to search and watch youtube’s video on linux shell, called YouShell.
http://code.google.com/p/youshell/
@Rafael Soares
Interesting