WAV or MP3 to OGG

WAV or MP3 to OGG is a script that allow the user to convert multiple music files using bash from GNU/Linux command line. It was originaly designed by Jérôme Bardot and modified by Cajetan Bouchard.

What you will need

The first thing you will need is a computer that can execute Bash commands from a terminal.


You will then need to install oggenc from the vorbis-tools package and the ffmpeg tool

Under a Debian distribution, use the following

# apt-get install vorbis-tools ffmpeg

For an Arch Linux distribution, use the following

# pacman -S vorbis-tools ffmpeg

Next, we will create a few folders that will help us manage what files need to be converted, what files has been converted and what files has been created. Navigate the a directory in which you desire to create this project. We will thencreate a directory call convertion and 3 children directories call source, destination, and done. After that we will create a file that will be our script file.

$ cd ~/Music
$ mkdir conversion conversion/source conversion/destination conversion/done
$ touch cv.sh

The script

Lets start by defining a few key words. The first line is to let the system know that you will be executing bash code. The next three lines are simply variables that we will use in the script later.

#!/bin/bash
cvsource="./source/"
cvdestination="./destination/"
cvdone="./done/"

Then, lets define a function wish will allow the script to ask the user some information about the current track.

getInfo()
{
    echo "Artist: "
    read artist

    echo "Title: "
    read title

    echo "Track number: "
    read number

    echo "Label: "
    read label

    echo "Genre: "
    read genre

    echo "Quality: "
    read quality

    echo "Copyright:"
    read copyright
}

You may change the arguments order or add new ones. Those are the default values we will use in this tutorial. Later we will see how to add the Date keyword, but for now lets look at the core of the script.

First thing we need to do is to gather all the songs to be converted from the folder called source. Then we will do a series of check to make sure the the current selected element

  • is not a directory
  • can be modified (we will move the file when everything is done)
  • is not an executable file
  • is ether a WAV or a MP3

conversion ()
{
    for file in ${cvsource}*;
    do
        echo ${file}
        if [ -d "${file}" ];    # check if a directory
        then
            echo "${file} is a folder"
        else
            if [ -w "${file}" ];    # check if the file can be modified
            then
                if [ "${file}" != "${0}" ];     # check if not an executable
                then
                    # permet de donner l'extension et d'agir en conséquense
                    case "${file##*.}" in
                        mp3 | MP3 )
				# INSERT MP3 CODE HERE
                        wav | WAV )
				# INSERT WAV CODE HERE
                        ogg | OGG );;
                                # No code to be added here, but you may want to be 
				# able to convert OGG files to a lower quality or 
				# change the meta data 
                        *);;
                                # This is the default case. You may want to display
				# information here
                    esac
                fi
            else
		echo "Error this file can't be modified"
            fi
        fi

        echo "Continue ? (y/n)"
        read next

        if [ ${next} == "n" ]
        then
            echo "Manuel exit requested by the user"
            exit 0
        fi
    done
}

Converting WAV files

Alright, now take a sip of coffee. We will continue by adding the code for a WAV file. You will see it's quite simple. I'll explain everything just under the following code

getInfo

oggenc "${file}" -N "${number}" -a "${artist}" -t "${title}" -l "${label}" \
    -G "${genre}" -q "${quality}" -c "copyright=${copyright}" \
    -n "${cvdestination}%a - %t.ogg"

filename="${file##*/}"
mv "${file}" ${cvdone}"${filename}";;

Don't forget the two ;; they define when the case statement finishes.

The first line we call the function getInfo that we declared earlier. This function will store in global variables what the user inputs from the keyboard. The second line is where the magic happens. oggenc is a command from the Vorbis-Tools package we downloaded earlier. Its job is to convert WAV files into OGG. It has many arguments which allows the user to specify the file meta-data.

  • -N : Track number (Capital N)
  • -a : Artist name
  • -t : Track title
  • -l : Label (in my case, I use this for the name of the album)
  • -G : Track genre
  • -q : Quality (number between -2 and 10)
    • 4 = 128 kbits
    • 6 = 192 kbits
    • 9 = 320 kbits (I always use this one)
    • 10 = 500 kbits
  • -c : Comment information (when you say copyright="SOME TEXT", it is detected like the copyright meta-data)
  • -n : output file name (Note that we are adding the destination directory to the file name so it is saved in a separate folder)

A trick to allows the user to use spaces withhout having to escape the space character is to put all the user input variable between quote.

The last line move the source file (WAV) to the done directory. You may want to remove the file by using the rm commande. Although it might be wise to keep that file until you verify that the song has been converted with perfection.

Converting MP3 files

The problem with the oggenc tool is that it does not allow to pass from MP3 to OGG. So we need a middle file which need to be a WAV file. This adds two commands.

getInfo

ffmpeg -i "${file}" -acodec pcm_s16le -ac 2 a.wav
oggenc a.wav -N "${number}" -a "${artist}" -t "${title}" -l "${label}" \
    -G "${genre}" -q "${quality}" -c "copyright=${copyright}" \ 
    -n "${cvdestination}%a - %t.ogg"

filename="${file##*/}"
rm a.wav
mv "${file}" ${cvdone}"${filename}";;

ffmpeg is an amazing tool that allows you to convert audio an videos files in pretty much any format. Althought the OGG meta-data tags are not well supported. Lets convert the MP3 file into a WAV file called a.wav. This new file is the used in the oggenc tool as the input file. Don't forget to delete the temporary a.wav file from your system as you will see the take a lot more place than your MP3 file.

Adding the Date meta-data

The date meta-data is represented by the -d in the oggenc tool. To add this meta-data to the script, do the following.

  1. Add these two lines to the getInfo fonction
  2. echo "Release Date: "
    read date
    
  3. Add the -d to the oggenc command in the MP3 and WAV case statement
  4. oggenc [...] -d "${date}" -n "filename.ogg"

For more meta-data check the oggenc documentation with a terminal. (q to exit the manual)

$ man oggenc

How to use

To lauch the script, change to your project directory (if you are not already there). Make sure your script is executable (you only need to do this the first time). Then launch the script and let it guide you.

$ cd ~/Music/convertion
$ chmod +x cv.sh
$ ./cv.sh

Sources

  1. Jérôme Bardot @ Wild Turtles
  2. Wikipedia - Vorbis Technical details