Linux - Convert all Ogg files in a directory to MP3

I need to convert all Ogg Vorbis files in a directory to MP3. I will create a small bash script that uses avconv to convert the audio files.

Let us assume that we have the following Ogg music files in a directory.

In A Beautiful Place Out In The Country/
|-- 01 - Kid For Today.ogg
|-- 02 - Amo Bishop Roden.ogg
|-- 03 - In A Beautiful Place Out In The Country.ogg
`-- 04 - Zoetrope.ogg

avconv command

The following command will convert the file 01 - Kid For Today.ogg to 01 - Kid For Today.mp3.

avconv -i 01\ -\ Kid\ For\ Today.ogg -acodec libmp3lame 01\ -\ Kid\ For\ Today.mp3
avconv version 11.8-6:11.8-1~deb8u1, Copyright (c) 2000-2016 the Libav developers
  built on Oct  1 2016 07:16:29 with gcc 4.9.2 (Debian 4.9.2-10)
Input #0, ogg, from '01 - Kid For Today.ogg':
  Duration: 00:06:23.00, start: 0.000000, bitrate: 109 kb/s
    Stream #0.0: Audio: vorbis, 44100 Hz, stereo, fltp, 160 kb/s
    Metadata:
      ALBUM           : In A Beautiful Place Out In The Country
      track           : 1
      ARTIST          : Boards Of Canada
      DATE            : 2000
      TITLE           : Kid For Today
Output #0, mp3, to '01 - Kid For Today.mp3':
  Metadata:
    TSSE            : Lavf56.1.0
    Stream #0.0: Audio: libmp3lame, 44100 Hz, stereo, fltp
    Metadata:
      ALBUM           : In A Beautiful Place Out In The Country
      track           : 1
      ARTIST          : Boards Of Canada
      DATE            : 2000
      TITLE           : Kid For Today
      encoder         : Lavc56.1.0 libmp3lame
Stream mapping:
  Stream #0:0 -> #0:0 (vorbis (native) -> mp3 (libmp3lame))
Press ctrl-c to stop encoding
size=    5985kB time=382.99 bitrate= 128.0kbits/s
video:0kB audio:5985kB other streams:0kB global headers:0kB muxing overhead: 0.003067%

Loop over all Ogg files in a directory

We want to loop over all Ogg files in a directory and print the filename with the extension mp3 instead of ogg. The following bash script ogg2mp3.bash placed in the directory with the Ogg files is doing that.

#!/bin/bash

for i in *.ogg; do
    printf "${i:0:(-3)}mp3\n"
done

We can call that script the following way.

bash ogg2mp3.bash
01 - Kid For Today.mp3
02 - Amo Bishop Roden.mp3
03 - In A Beautiful Place Out In The Country.mp3
04 - Zoetrope.mp3

Convert Ogg to MP3 in a loop

We will combine the avconv command with the loop script and can execute it the same way as above in the directory.

#!/bin/bash

for i in *.ogg; do
    avconv -i "$i" -acodec libmp3lame "${i:0:(-3)}mp3"
done

The script should have produced for every Ogg file a MP3 file in the current directory.

In A Beautiful Place Out In The Country/
|-- 01 - Kid For Today.mp3
|-- 01 - Kid For Today.ogg
|-- 02 - Amo Bishop Roden.mp3
|-- 02 - Amo Bishop Roden.ogg
|-- 03 - In A Beautiful Place Out In The Country.mp3
|-- 03 - In A Beautiful Place Out In The Country.ogg
|-- 04 - Zoetrope.mp3
|-- 04 - Zoetrope.ogg
`-- ogg2mp3.bash
Next Previous