Short Tip: Convert Ogg Vorbis file to MP3

Convert Ogg file to MP3
In case you have an ogg vorbis file and need to transform that to a MP3 file (for example for mobile phones or crappy mobile audio players) the following command line will transform all ogg files of the directory to mp3 files. Please keep in mind that you will lose audio quality when you transform one lossy audio format to another.

for file in *.ogg;do oggdec -o - "$file"|lame -h -V 4 –-vbr-new - "$(basename "$file" .ogg).mp3";done

I am aware that there are thousand other methods – you could write entire scripts, GUIs, use other tools, and so on. But this way works for me, and I need some place to remember it. In case you wonder: if you want to increase the qualit, change the “4” to a “2”. This of course only makes sense if you have a high quality ogg vorbis file. 🙂

Thanks maha and sb.


31 thoughts on “Short Tip: Convert Ogg Vorbis file to MP3”

  1. Using CBR (constant bitrate), as you did in your script, is generally a bad idea qualitywise. If there is no special need to go CBR, like creating audio with a purpose to mux it into a video stream, it’s always advisable to use default VBR switches such as ‘-V 2 –vbr-new’ which will achieve similar size to CBR 192 but with a higher quality.
    I know however, that at these bitrates there’s hardly any chance to spot a difference, but still, as you transcode, it’s better to lose as little quality as possible.
    And as I’m a bit anal about “quality”, treat my words with a grain of salt 😉

  2. “Please keep in mind that you will lose audio quality when you transform one lossy audio format to another.”

    Well, that is true. But ogg is not a lousy format, because to start with: it is not a proper format.

    It is just a container, just like aví. On an avi file you can have video encoded in a variety of format, mainly mpg derivatives, divx/xvid and the like.

    Another example of a container format is matroska.

    In an ogg file you can have streams encoded with many formats, like for example theora or flac, even dinamic streams like icecast. And of course, vorbis compresed audio. This /vorbis) was the first format that used ogg as a container, and probably because of that, some people think that ogg and vorbis are the same, when they are not.

    Some of the formats used by ogg are lousy, but some others /flac comes to my mind) are loseless.

  3. Eike, yes, but I wanted a solution which I could use in a script.

    flx: Of course, but the post wasn’t aiming at that particular problem.

    Constantin: Mine was stolen, unfortunately 😦 Which one do you have?

    sb : Thanks for the comment. I thought about the constant bitrate but then dropped it and wrote the short tip. I added your way, thx.
    And: I’m only focussed on quality when it comes to the original ogg files. As long as I have them the rest doesn’t matter.

    Jesús: In the post itself I wrote Ogg vorbis, I should have added “vorbis” to the headline, though. But I am very aware of the difference: I wrote most parts of the German Wikipedia entries about Ogg, Vorbis, Theora, flac and so on. I even chatted with the Xiph guys about particular bits of the articles and helped at the xiph web page once. 🙂
    But thanks for the comment anyway, others might not take that for granted.

  4. In KDE you’ve got Amarok, which support converting to ie. mp3 when moving to audio player. It’s a way simpler that scripting 😉

  5. @liquidat, you are right, I supposed I read that too fast..

    Just to be of any use, I can contribute a mre bash-like way to do it, no need to use basename:

    for file in *.ogg;do oggdec -o - "$file"|lame -h -V 4 --vbr-new - "${file/.ogg/.mp3}";done

    (I hope bbtags work 😛 ).

    By Liquidat: Nope, they don’t, but I fixed that to wordpress code tags 😉

  6. Semi offtopic: anyone looking for a no bullshit, ogg-friendly player should check the Samsung YP-U2.
    More ontopic: back when I owned an mp3-only player, it was always Amarok who helpfully converted the oggs before transfer 🙂

  7. About Amarok: of course that works, but hey, I like my command line 😉
    Seriously, I was just looking for a quick and easy way which I could embed in a script. But thanks anyway for the GUI suggestions 🙂

    And about Ogg-Vorbis players: My girlfriend also has a Samsung player which supports Ogg. I had an iAudio once, and I bought an iRiver for friends of mine. All of them are capable of playing Ogg Vorbis.

  8. But you’re missing the critical bit: tag conversion! mp3s without tags are fairly useless on portable players.
    Here’s an alternative that will copy the tags over (if you have the gstreamer bad and ugly plugins installed):

    for file in *.ogg; do
       gst-launch-0.10 filesrc location="${file}" ! oggdemux ! vorbisdec ! audioconvert ! lame quality=4 ! id3v2mux ! filesink location="${file/.mp3/.ogg}";

    The only limitation is that the tags are streamed in and not written in the header, so not all tools will see the tags (for example id3v2 or id3tool won’t, but rhythmbox and mpg321 will)

  9. That should be

    for file in *.ogg; do
    gst-launch-0.10 filesrc location=”${file}” ! oggdemux ! vorbisdec ! audioconvert ! lame quality=4 ! id3v2mux ! filesink location=”${file/.ogg/.mp3}”;

    Otherwise it just overwrites all the .ogg files with empty ones. Youch. Good thing I was working on a copy…

  10. kudos, as written above there are man tools available. I just wanted to write down the quickest way which does not require any further downloads 🙂

  11. Hi,

    Two issues.

    1. for file in *.ogg;do oggdec -o – “$file”|lame -h -V 4 –-vbr-new – “$(basename “$file” .ogg).mp3″;done

    Notice the dash before “-vbr” is not a hyphen, but an n-dash. This will cause a copy/paste to fail. What works:

    for file in *.ogg;do oggdec -o – “$file”|lame -h -V 2 –vbr-new – “$(basename “$file” .ogg).mp3″;done

    (Note the clean double-hyphen before “vbr”.)

    2. The word “quality” has lots its y.

  12. Here’s the command I used, which works better than a for/in loop when there are filenames with spaces and other garbage in them. It also works recursively:

    find -type f -name ‘*.ogg’ | while read file; do echo “working on $file”; gst-launch-0.10 filesrc location=”${file}” ! oggdemux ! vorbisdec ! audioconvert ! lame quality=4 ! id3v2mux ! filesink location=”${file/.ogg/.mp3}” 2>&1 && rm “$file”; done >> /home/baron/ogg2mp3results.txt

  13. Xaprb:

    Thanks for that command for conversion. The only thing is that it didn’t handle spaces in the directory names (for example “Trans-Siberian Orchestra”). Here’s my minor modification which did handle them properly. I used /usr/bin/tee so that I could see the output in the terminal window as well as put the output into a file:

    /usr/bin/find -type f -name ‘*.ogg’ | while read file; do echo “working on $file”; /usr/bin/gst-launch-0.10 filesrc location=”${file}” ! oggdemux ! vorbisdec ! audioconvert ! lame quality=4 ! id3v2mux ! filesink location=”`/usr/bin/dirname “$file”`/`/usr/bin/basename “$file” .ogg`.mp3″ 2>&1 && /bin/rm “$file”; done | /usr/bin/tee -a $HOME/ogg2mp3results.txt

    I use absolute pathnames for the commands that are not shell-builtins for security purposes — in case someone has been able to put a trojan executable on my path. Feel free not to be as paranoid as me . . . . 🙂

  14. So, I’m flying out of tomorrow and didn’t have time to get my nice beautiful RockBoxed Vorbis player ready to go (I burned up a battery and am waiting for it in the mail) so, had to prep mine, and my wife’s, back-up MP3 players for the trip… the above script came in such handy!!!!

    However, the comment about tags is spot on. Well, I was fortunate enough that my file-naming is good ( it goes like “(##)_(album)_(title).ogg” and the artist name is encoded in the folder it is in). Well, I found that on my system, installing gst and ugly was… well, infeasible.

    I took a chance on a lesser known app, called Picard (it is from the MusicBrainz project) and ran it on my folder of one single newly-transcoded album, and it automatically detected three of the songs correctly, and placed them in the correct album (it fingerprints the audio data directly and then presents a best-guess at metadata). It incorrectly placed half the album into a foreign release, and the rest into the EP. Well, I just drag/drop’ed the wrongly placed songs onto the correct songs in the album that was correctly identified (by now, I’ve spent 1.5 minutes in tagging) and then clicked “save metadata”.

    *POOF* all songs tagged correctly. So, here’s one vote for Picard for automagical tag management. I’m not sure what it would do about a giant soup of random MP3s, though…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s