29 September 2008

-"We're sorry, this video is no longer available" YouTube downloader

More often than not, when the planets and moons aren't oriented in the right manner, I get the dreaded "We're sorry, this video is no longer available" from YouTube. I know that the video is certainly NOT "no longer available" because its clearly viewable by other people.

Fortunately in the linux world, there is this little python utility called youtube-dl which can be run from the command line to download any YouTube file. So when you get this error, copy the URL of the YouTube file, and do this:

# sudo apt-get install youtube-dl
# youtube-dl http://www.youtube.com/watch?v=videofile001

if it doesn't work, it will complain with this verbose error:

Retrieving video webpage... done.
Extracting URL "t" parameter... done.
Requesting video file... failed.
Error: unable to download video data.
Try again several times. It may be a temporary problem.
Other typical problems:

* Video no longer exists.
* Video requires age confirmation but you did not provide an account.
* You provided the account data, but it is not valid.
* The connection was cut suddenly for some reason.
* YouTube changed their system, and the program no longer works.

Try to confirm you are able to view the video using a web browser.
Use the same video URL and account information, if needed, with this program.
When using a proxy, make sure http_proxy has http://host:port format.
Try again several times and contact me if the problem persists.

You can then repeat this process over and over again, until it succeeds. Unfortunately this requires effort. And we computer users hate expending any unnecessary energy if possible. After all, focusing on the terminal, clicking the up arrow and hitting return is so so tedious.

So I wrote a little bash script to complement youtube-dl as it sports a few extra features:
  1. It will automatically try and retry to youtube-download the file until it is successful (thanks to $?)
  2. It will rename the resultant flv video filename to the current date and time, instead of the random garbage filename. (using date +%F...)
  3. It terminates gracefully when the user hits Ctrl-C (by setting a trap ...)
  4. It promotes world peace by waiting a few seconds before trying again (sleep is always good)
And here it is:

#!/bin/bash

i=1
fname=`date +%F-%H%M`

result=1

while [ $result = 1 ]; do
echo Attempt $i "$fname".flv
trap "echo User killing the download; exit" INT TERM
youtube-dl $1 -o "$fname".flv
result=$?
let i=i+1
if [ $result = 1 ]; then
sleep 10
fi
done

Try it out. The most I had to wait was 24 attempts. Thats 46 keypresses saved!


yk.