Extracting an MP3 From a YouTube Flash (FLV) Download…

My wife likes YouTube.

A *lot*.

I had found out how to make MP3 files from the audio sections of the FLV files, so she wanted to know if I could set things up for her to do this on her own.

Of course I said yes.

So, first step was downloading the video from YouTube. That was pretty easy – I installed the UnPlug add-on for Firefox and tested it. I used to use Ook! Video, but it stopped working for me on YouTube. Maybe they changed the site and broke it, I don’t know. Anyway, Unplug seems to work much better, and soon I had the FLV files I was interested in.

Next step was streaming the audio into an MP3 file – I say streaming because the original FLV file is left unchanged – it does not get converted to an MP3, but rather a new MP3 is made from the audio protion of the video file.

For this, I used ffmpeg at the command line. It worked great, and I was able to create OGG and MPG files as well. The command I ran was:

  • ffmpeg -title “my_title” -i filename.flv -acodec mp3 -ac 2 -ab 128 -vn -y filename.mp3

The -i is the input file, -acodec is the type of audio codec to use, -ac is the number of audio channels, -ab is the encoding bitrate, -vn disables video recording, and the -y option overwrites output files. Plenty of other info is here.

Still, even though it worked fine, it is a little clunky to use for multiple files and is well past what I can expect my wife to manage, so I wrote my first *real* bash shell script – youtube2mp3. The script, which I made executable and placed in /usr/local/bin, basically acts within the current working directory, accepting one input parameter – the output directory location. It runs through and extracts an MP3 file for each FLV file, provided that the MP3 file does not already exist in the output directory. The sanity checking it does is limited to determining if the output path (assumed to be a USB MP3 player) exists (is plugged in), and it looks for Control-C to delete the MP3 file currently being encoded and break out of the script. Anyway, here is the scripţ in all its ugly glory:

#!/bin/bash

####################
# Set variables – interrupt, extension1, extension2, and the audio file name
####################
USER_INTERRUPT=13
xt1=flv
xt2=mp3
audio=${filename%$xt1}$xt2
pluginmsg=”$USER, please plug in the MP3 player and try again. Quitting…”

####################
# Set the output directory, so as not to clutter up the folder full of flv files
####################
if [ -n "$1" ]
then
outputdir=${1}/
else
outputdir=/dev/dummy/
fi

####################
# Catch Control-C events to break out of the loop and remove the partial audio file
####################
trap ‘echo “Quitting…”; rm -f $audio; exit $USER_INTERRUPT’ TERM INT

####################
# Loop through the working directory and create from xt2 from xt1
####################
mount | grep -i ${outputdir%/} &> /dev/null # Is the MP3 player mounted?
if [ $? = 0 ]
then
for filename in *.$xt1
do
title=${filename%$xt1}
audio=${filename%$xt1}$xt2

if [ ! -f ${outputdir}$audio ] # Does the mp3 already exist in the output directory?
then
ffmpeg -title “$title” -i $filename -acodec mp3 -ac 2 -ab 128 -vn -y $audio # Change this if xt2 is not an mp3
mv $audio $outputdir
fi
done
else
echo “$pluginmsg” # No MP3 player – try again!
sleep 1
exit 1
fi

exit 0

I tried to write it generic enough it could be easily modified for other file types, etc, etc, etc. I then made a new KDE “Link to Application”, set the working directory as the folder my wife saves her videos to, and passed in the media path for the MP3 player for the command to run – “/usr/local/bin/youtube2mp3 /media/disk”. I also set the desktop link to run as a terminal window, so she could see the progress and any exit messages.

Now, she saves her YouTube vidoes to that folder, plugs in her MP3 player, clicks the script icon on her desktop, and waits while it creates the MP3 files and moves them to her MP3 player. Too easy.

Follow

Get every new post delivered to your Inbox.