How To Download YouTube Videos In Linux

I know there are probably tons of ways to download a copy of a YouTube video but this is the method I prefer and I thought I'd share. The basic idea is that we have a simple shell script that extracts the Flash video information from YouTube and uses ffmpeg to convert it into an XviD AVI file with MP3 audio.

First of all, this will only work on a UNIX-like system that is capable of running shell scripts. Linux, BSDs and Mac OS X should work. Windows might work with

Cygwin or the like. You will also need to have ffmpeg available with XviD and MP3 support (for final rendering), as well as the formats used by Flash videos (all of these should be built-in already but some Linux distros – like Ubuntu – don't include MP3 for legal reasons).

The script will require the URL of the video and there's two ways to get it. The first is from your browser's location bar – the thing you type www.chnorton.com.au into every day. The second way is from the YouTube page itself, the URL should be located to the right of the video, as shown below:

YouTube: URL Field Highlighted

The script that we are going to use looks like this (lines marked with » are wrapped and should be entered as one line in your text editor):

#!/bin/bash<br /> base_url="http://youtube.com/get_video.php?"<br /> url=$1<br /> video_name=$2</p> <p>wget ${url} -O /tmp/y1<br /> uf=${base_url}`grep player2.swf /tmp/y1 | cut -d? -f2 | cut -d\" -f1`<br /> wget "${uf}" -O /tmp/y.flv</p> <p>ffmpeg -i /tmp/y.flv -ab 56 -ar 22050 -acodec mp3 &raquo;<br /> &nbsp;&nbsp;&nbsp;&nbsp;-s 320x240 -vcodec mpeg4 -qmin 1 -qmax 5 "${video_name}.avi"</p> <p>rm /tmp/y.flv<br /> rm /tmp/y1<br /> exit

If you're familiar with bash coding then this should be fairly easy to work out. What the script is doing is:

  1. Specify the interpreter for this script (bash in this case).
  2. Set the base URL to retrieve videos.
  3. Get the URL for the specific video we want and the name of the new video file from the command line options.
  4. Download the HTML page for the URL.
  5. Extract the URL for the actual video by looking for "player2.swf" in the HTML.
  6. Download this video.
  7. Use ffmpeg to convert the file into a 320×240 MPEG4 (XviD) video using variable bit rate, muxed with 22kHz, 56 bit MP3 audio.
  8. Clean up.

Put the above code into a file, you can name it whatever you like. I'm going to call mine dlyt for download youtube. Remember to set the executable permissions for the script with

chmod a+x dlyt

You then call the script in the following way:

dlyt url name

For example: dlyt 'http://www.youtube.com/watch?v=9TYzRanykbQ' 'Soundwave'

NOTES:

  1. You do not need to include quotation marks around the URL, but I do anyway.
  2. If the name of the video is more than one word (ie. there's a space in the name) then you do need quotation marks – either " or ‘.
  3. The extension .avi is automatically placed on the end of the video. There is no need to include it in the name.

There you have it. Not bad for 10 lines of shell code eh? You can of course clean up the code to your liking or make it more robust. You could also look into the ffmpeg documentation and change the codecs and formats used to your tastes. You might prefer to use Theora and Vorbis audio inside an Ogg container for instance.