Sunday, December 14, 2008

Youtube-lst

Yeah... I had this done in Ocaml a while back... but I lost it somewhere, so I rewrote it in Bash + AWK. Good news is that it turned out to be much more simple.

I also tried to figure out if this is legal or not. I do not know this, in the end, but I figure, that all I'm doing is looping through a list, and youtube-dl is doing all the work, really.

Yeah, so it follows that youtube-dl is, of course, a dependency or a requirement.

Oh... and I didn't mention what it does yet, either... Well, if you want to download a video from Youtube to watch it later, while you're sitting in your hotel room deprived of your only link to civilization, the Internet, courtesy of your employer, or similar - in that situation you use youtube-dl, and everything's fine. If you're there for weeks on end though, you might want to get maybe 10 videos or something - then you can use this program.

First, you make a file, which lists all the files you want to look at. These files include either just lines with addresses, like this:
http://www.youtube.com/watch?v=nQmAGBbOmas
http://www.youtube.com/watch?v=-bMdTmRae6c

or with filenames like this:
-o megadeth.flv http://www.youtube.com/watch?v=1qKGZ4Ysy5M

You can put other parameters for youtube-dl in there as well - you can see in line 26 that this entire string is just inserted as-is into the command.

Here's the code:
 
1 #!/bin/bash
2 #
3 # Batch downloader for youtube-dl
4 #
5 # Prepare a list of addresses in a file and get
6 # then with just one command, which does all the
7 # tedious copy-and-pasting for you...
8 #
9 # Parameters
10 # Files contaning adresses.
11 # If you need to rename the files, prefix the
12 # addresses with: -o <filename>.flv
13 # Requires
14 # youtube-dl to do the actual downloading
15 # (http://www.arrakis.es/~rggi3/youtube-dl/)
16 # Author
17 # Konrad Siek
18
19 # Expect each argument to be a file
20 # and loop through all of these.
21 for file in $@
22 do
23 # Download each resource thorugh youtube-dl.
24 cat $file | \
25 awk '/^[ \t]*$/ {next}{system("youtube-dl "$0)}'
26 done


Note that youtube-dl was developed on the desert planet.

The code is also available at GitHub as bash/youtube-lst.

No comments: