08 July 2010

Adding YouTube watch?v= links to auto-download

The linked chunk of Python does the job well... but leaves a .flv file with a crypto-looking name. This chunk of shell script ($1 is the full YouTube URL, as in http://www.youtube.com/watch?v=XYZZY123abc or whatever) hauls out the video title:

wget $1 -O /tmp/fetched_from_youtube.data
name=$(gawk '/<.title>/ { x=0; } x > 0 { if($1 == "-" && $2 != "") { sub ("^[^A-Z]*",""); sub("\\(.*\\).*$",""); gsub("[[:punct:]]",""); sub(" +$",""); gsub(" +","_"); print $0; } } // { x=2; }' /tmp/fetched_from_youtube.data)
rm -f /tmp/fetched_from_youtube.data

The title is savagely pruned to exclude most things which would fail as part of a filename. Experiments will happen tomorrow AM, but I’m guessing that something like this would do the appropriate conversion/renaming:

arg=$(echo $1 | sed -e 's/^.*v=//')
ffmpeg -i ${arg} -ab 56 -ar 22050 -b 500 -s 320x240 ${name}.mpg

That results in a plain MPEG file which should convert losslessly to a .vob for burning onto a standard video DVD. If you use .mp4 instead, you wind up with a smaller video file for playing using smplayer, xine or whatever you have to hand.


Bruce said...

Have you tried fetching videos with youtube-dl?

Leon RJ Brooks said...

Ta, Bruce.

Doing that now. It doesn’t rename or process the Flash video file, but the script above does that handily.