There have been many instances where I’ve needed to extract the audio from a flash movie — typically obtained from YouTube — and convert this to mp3 format. I’ve decided to simply this process by creating a little script and sharing it with you.
This is known to work on Linux, and is also likely to work on other POSIX systems running bash as the default shell though testing has only occurred on Linux. As such, the instructions below are for Linux distributions.
Requirements:
Certain off-the-shelf software packages are required to make the script work. These can probably be obtained via your distribution’s software repository eg. Synaptic if using Ubuntu, apt-get if using Debian, yum if using Fedora etc. If not, I’ve included links to each package’s homepage below.
-
You require a copy of mplayer which, if it is not already included with your distribution can be found at http://www.mplayerhq.hu/
-
LAME – a very nifty MP3 encoder which can be found at http://lame.sourceforge.net/
-
youtube-dl – a tiny Python script used for downloading YouTube videos, which can be found athttp://www.arrakis.es/~rggi3/youtube-dl/ . Unlike mplayer and LAME above, youtube-dl is a Python script and requires that you have Python installed. Most distributions install Python by default these days, so it really shouldn’t be a problem. A direct link to the youtube-dl script can be found here. Ensure the name of the file is “youtube-dl” when saving it. Also, ensure the file is in your path, so move it to, say, /usr/local/bin and run “chmod 755 /usr/local/bin/youtube-dl” to make it executable
Using The Extractor Script
Lastly, you require the extractor script I’ve written which can be found below. Copy and paste the script into a regular text file, then move it to a directory in your path, say /usr/local/bin, and make it executable with the command chmod 755 /usr/local/bin/youtube-mp3.sh. That’s all there is to it.
To use the script, you need the link of a YouTube video. So, search YouTube, find the video you’re looking for and copy the link. In firefox, this can be done by right-clicking the link and selecting “Copy Link Location”.
Next, run the script as follows:
youtube-mp3.sh my_filename.flv youtube-link
As an example:
youtube-mp3.sh southpark.flv http://www.youtube.com/watch?v=0-22EpQOm8c
The script will then download the video using youtube-dl and save it as the file “soutpark.flv”. It will then use mplayer and lame to extract the audio and convert it to mp3 format. On completion, you will have two files: southpark.flv which is the original downloaded video, and southpark.mp3 which is an mp3 of just the audio.
I’ve noticed that mplayer does not play certain flash videos well, and so far the only fix I’ve found is adding the -fps <framerate> option to mplayer. This feature hasn’t as yet been included in the script.
Downloading the Script:
Sadly, I’m not sure how to host a text file at WordPress. So if anyone can tell me how, or host it elsewhere, then great. Until then, I’m just going to have to post the code here. Copy and paste the stuff below to a text file named youtube-mp3.sh, move it to /usr/local/bin and make it executable with chmod 755 /usr/local/bin/youtube-mp3.sh
#!/bin/bash
#this script is released under the GNU GPLv2 license which may be viewed at
#http://www.gnu.org/licenses/gpl-2.0.html
#
#Author: Faeem Ali
#Email: Faeem.Ali@gmail.com
prog_name="`basename $0`";
if [ ! "${#}" = "2" ];
then
echo "Usage: `basename $0` output_filename.flv youtube_link_in_singlequotes";
exit 1;
fi
output_file=$1;
link=$2;
output_extension="`echo ${output_file##*.}`";
if [ ! "${output_extension}" = "flv" ];
then
echo "Warning: output file does not have .flv extension. Appending automatically";
output_file="${output_file}.flv";
fi
mplayer_cmd="`which mplayer`";
if [ ! "${?}" = "0" ];
then
echo "Error locating mplayer. mplayer not found in path";
exit 1;
fi
lame_cmd="`which lame`";
if [ ! "${?}" = "0" ];
then
echo "Error locating lame. lame not found in path";
exit 1;
fi
youtube_cmd="`which youtube-dl`";
if [ ! "${?}" = "0" ];
then
echo "youtube-dl script not found in path";
exit 1;
fi
${youtube_cmd} -o ${output_file} "${link}";
if [ ! "${?}" = "0" ];
then
echo "Error downloading youtube video with youtube-dl";
exit 1;
fi
#convert to wav
base_output_file=${output_file%.*}
${mplayer_cmd} -vo null -ao pcm:file=${base_output_file}.wav ${output_file}
if [ ! "${?}" = "0" ];
then
echo "Extracting raw audio from video";
rm -f ${base_output_file}.wav
exit 1;
fi
#convert to mp3
${lame_cmd} ${base_output_file}.wav ${base_output_file}.mp3
if [ ! "${?}" = "0" ];
then
echo "Error converting wav file to mp3";
rm -f ${base_output_file}.wav;
rm -f ${base_output_file}.mp3;
exit 1;
fi
#delete wav file
rm -f ${base_output_file}.wav
echo
echo "Downloaded video is ${output_file}"
echo "Extracted mp3 is ${base_output_file}.mp3";
echo "Done";
#----------------- script ends here -------------------------

