2008-02-05

One way to save youtube videos.

[MOTIVATION]

There are some very cool videos on youtube such as some of the old Mystery Science Theater 3000 (MST3K) videos. Long videos like these are broken up into manageable slices, sometimes as much as 10 parts. It would be nice to save not just the parts of these videos, but to put them back together into one playable movie.

[ASSUMPTIONS]

For this project, as with most of my projects, I will assume a Ubuntu- or Debian-based Linux distribution and that you are familiar with the command line.

[REQUIREMENTS]

For this project, you will need to obtain the following software:


  • ffmpeg

  • youtube-dl

  • cat

  • Python



To get ffmpeg, run the following command:

 $ sudo apt-get install ffmpeg 


The cat command should be installed already on your system. To verify, try this:

 $ which cat 


If you see output that looks something like: /bin/cat then you are all set.

The youtube video downloader is a Python script that takes as input a link to a youtube video. Get the script here. The youtube video downloader depends on Python, so make sure you install the Python language.

 $ sudo apt-get install python 


[METHOD]

First, head over to youtube and do a search for Santa Claus Conquers the Martians. This video was broken into 10 parts, and it is one hilarious movie, a true MST3K classic. Find the link to part 1, and right-click to copy the link location. Next, go to the directory where the youtube-dl script is located. First, ensure that the script is executable:

 $ chmod a+x youtube-dl 


Next, execute the following command to begin the download:

 $ ./youtube-dl <videoLink> 


To paste in a link on your terminal (I use gnome-terminal), there are a couple methods you can use. First, you can try doing CTRL+SHIFT+V to paste in the previously copied link. You can also use your mouse. Clicking both the left and right mouse buttons simultaneously will paste whatever you had copied into the terminal. Alternatively, pressing down on the scroll wheel of your mouse will usuall work.

The download should begin, and it will download the video file as a .flv file. This file is not that useful, because it is a flash-formatted file, which can be played using the vlc player, but we want a more platform-indpendent format like .mpg for this project.

Assuming that your flash file is named Flash6xui2, use ffmpeg to convert this file into a .mpg file like this:

 $ ffmpeg -i Flash6xui2 -o part1.mpg 


This step may take a while (and most of your cpu resources) to complete, so be patient and you will be rewarded with a file called part1.mpg which is the mpeg encoded first part of the movie. In the meantime, you can move on to the next part of the movie using the steps described above. Paste the youtube link to part two of the movie into your terminal like you did for part one and so on until you have all of the flash-based videos downloaded. Be careful not to confuse yourself by downloading all the flash-based videos first before convering them. This is because the .flv files may have confusing names (something like FlashJWEse552xa), and you need to know the order of the files for the combining step. So, your pattern should be:


  1. Download flash-based movie part.

  2. Convert it to .mpg using a predictable naming convention so you know which part is which.

  3. Repeat until all parts are downloaded



When complete, you should have in your directory something like 10 movie parts. Perhaps your directory looks something like this:

 $ ls

part1.mpg part2.mpg part3.mpg part4.mpg part5.mpg part6.mpg part7.mpg part8.mpg part9.mpg part10.mpg


You may also have the original flash files in there, but they are not really needed at this point. Save them anyway, for now, and delete them once you are sure all is well so you do not have to download them again.

Finally, you have all the files you need to build your movie. The cat program is a wonderful little utility that has as one of its attributes the ability to combine files.

Implement the cat command as follows:

 $ cat part1.mpg part2.mpg part3.mpg part4.mpg part5.mpg part6.mpg part7.mpg part8.mpg part9.mpg part10.mpg >  movie.mpg 


This step will combine each of the smaller parts into one seamless movie. Now you can use mplayer, xine, or even put the movie on removable media for transport to a Windows machine.

[PROBLEMS?]
Let's say the youtube-dl script does not work for you, or you are too scared to use it for some reason. You're not too scared, are you? You can still accomplish this task, but it may be more confusing than what I have already described.

Go to youtube and start the video you would like to save. You can pause it once it starts playing and it will continue to buffer. On Linux systems, the flash file buffers in the /tmp directory. Change directories to the /tmp directory and list the files there.

 $ cd /tmp && ls 


You may see a file in there (you may see very many files in there, because the /tmp directory is the scratchpad of the file system) named something like Flash8xsk2IJWEE. It will be the video file in flash format. Wait until the file finishes buffering in your youtube window, then you can execute the ffmpeg steps as described above.

sBosell