Here’s a nifty little script that will take a YouTube URL as an argument, pull down the associated flv and convert it into mp3 format. It’s basically all shell but I wrapped it with php.
First:
Take the YouTube URL as an argument
$url = $argv[1];
Next:
Use a little shell magic to grab the video arguments for later use:
$vid_args = `lynx -source "$url" | grep video_id | grep fullscreen | awk -F'video_id=' '{print $2}' | awk -F'&title'
Next:
use basically the same command to grab the title of the video:
$title = 'lynx -source "$url" | grep video_id | grep fullscreen | awk -F'video_id=' '{print $2}' | awk -F'&title=' '{print $2}' ';
Then:
Do a little formatting of the title to make it useable:
$title = preg_replace("| |", "_", $title);
$title = trim($title);
$title = preg_replace("/^[^a-z0-9]?(.*?)[^a-z0-9]?$/i", "$1", $title);
$title = preg_replace("|'|", "", $title);
$title = preg_replace("|\)|", "", $title);
$title = preg_replace("|\(|", "", $title);
$title = preg_replace("|\/|", "_", $title);
Then:
Grab the flv with the video arguments you captured from the line above:
'wget -O /tmp/$title.flv "http://www.youtube.com/get_video?video_id=$vid_args"';
Next:
Use ffmpeg to break it down to mp3 format:
'ffmpeg -i /tmp/$title.flv -ar 44100 -ab 160k -ac 2 /tmp/$title.mp3';
Finally:
Dump the flv and enjoy:
'rm -f /tmp/$title.flv';
These files are of course only for educational purposes when you are finished so make sure you delete them