Tuesday, May 02, 2006

Script to convert lots of OGG to MP3 with GStreamer

After I find out how to use gstreamer to convert ogg to mp3, another obvious problem suddenly appears. How do I convert many ogg files to mp3 using gstreamer?

I solve this problem using some bash script kung-fu. It might be ugly or not the best solution, but it work for me.

First, I create an executable script file with this content:

--- convert.sh script start ---
#!/bin/bash
(
IFS=$'\n';

mkdirhier $2$1;
for j in `ls $1*.ogg`
do
k=`basename $j`;
gst-launch-0.10 filesrc location="$1$k" ! oggdemux ! vorbisdec !
audioconvert ! lame ! filesink location="$2$1$k.mp3";
done
)
--- script end ---

And then, to use this script to convert many ogg to mp3, I use this command on bash prompt:


(
IFS=$'\n'
for i in "folder1/" "nested/folder2/" "here is another folder/with ogg/";
do
./convert.sh $i /destination/folder/;
done
)
IFS is Input Field Separator, an environment variable used by bash to split inputs. Here we need to only split input by newline ('\n').

Hope it helps some poor soul out there :-)

Oh btw, because I'm using gstreamer here, this script can easily be changed to convert __any__ multimedia file to any multimedia file that supported by gstreamer.

No comments: