Monday, May 12, 2008

Batch Convert FLV to MP3

Many times on youtube I find concert videos or acoustic shows that I want to keep for later. Using the Firefox plugin VideoDownloader, I can download these videos as flv files but these flv's are not as portable as mp3. So I searched for a way to convert flv to mp3 and found help on the Ubuntu forums.

After looking at the script and looking at my flv files, I noticed that it would be very inefficient to do each file individually. Therefore, I modified the script to convert a whole folder of flv's. I will show the whole script itself and how to run it, but first lets install some programs that we need in order to convert the flv's.

sudo apt-get install ecasound mpg123 lame ffmpeg

The script itself is the following:
#!/bin/bash
# FLV to MP3
#flv2mp3.sh

FLV_FILE=/home/ditto/Videos/
cd $FLV_FILE

for vid in *.flv
do
ffmpeg -i $vid -f mp3 -vn -acodec copy /tmp/temp.mp3
ecasound -i /tmp/temp.mp3 -etf:8 -o ${vid/.flv}.mp3
rm -f /tmp/temp.mp3
done

exit 0

I save my scripts in a ~/scripts and don't forget to make the script executable.
chmod u+x flv2mp3.sh

Run as follows:
user@home:/scripts~$ ./flv2mp3


Let's take a closer look at the code.
FLV_FILE=/home/ditto/Videos/
cd $FLV_FILE


FLV_FILE is the location of the flv video files that we want to be converted. In the next line, the directory is changed to the location of the videos.

Next, we will look through the files in that directory, only using the .flv files. Notice:
for vid in *.flv
Next we will convert the video to mono audio and create a temporary mp3 called temp.mp3
ffmpeg -i $vid -f mp3 -vn -acodec copy /tmp/temp.mp3
Since the audio by default is mono, we will then convert it to stereo, output it into the current directory, and save it as an mp3, while keeping the basename. The file is renamed by ${vid/.flv}.mp3
ecasound -i /tmp/temp.mp3 -etf:8 -o ${vid/.flv}.mp3
rm -f /tmp/temp.mp3
That is pretty much it. Any comments or ways to make it better, please let me know.

1 comment:

  1. 1st

    I dont know why nobody has left a comment, because IMHO your script is absolutely GREAT!!

    Best regards from the caribbean.

    ReplyDelete

Sociable