HOWTO Convert audio files

From LinuxReviews
Jump to: navigation, search

A huge variety of file formats are used to store and distribute digital audio. The most commonly used for compressed audio is mp3, and the most common format for raw audio is .wav.

But there are many other audio formats, and you likely want to be able to convert between the various formats.


[edit] .wav, the middle-man for most audio conversion

If you want to convert between compressed audio formats then there is no way around decoding the original compressed format to .wav before re-encoding the file to another common format such as .wav.

mplayer \
  -quiet \
  -vo null \
  -vc dummy \
  -ao pcm:waveheader:file="rawaudio.wav" "convertthis.rm"

This gives you audio.wav with the audio.

You may want to use -af to change sample-rate or other options. Example:

mplayer \
  -quiet \
  -vo null \
  -vc dummy \
  -af volume=0,resample=44100:0:1 \
  -ao pcm:waveheader:file="rawaudio.wav" "convertthis.wma"

Note that you will not gain anything (except more used harddrive space) by resampling a file to a higher sample-rate.

[edit] ogg files

.ogg files are best decoded with oggdec. So, you could for example use this (very dangerous!) little script to convert any .ogg file in your folder to wav:

oggdec *ogg
lame --preset 192 -ms -h *wav
# rm -f *ogg
rm -f *wav

As you should clearly see, it removes any .wav file already present in your current folder when it converts any .ogg file there to .wav, and if you comment out the file above then that's nuked also. But it works, it's automated, there's not much to it.

[edit] Fixing Broken WAV File Headers Using Mplayer

Some very cheap MP3 players include a voice recording capability but create WAV files with broken headers that will not load into editing or playing programs, but do play using MPlayer. These broken WAV files may resist other attempts to fix their headers with or ecasound. You can use Mplayer to fix the broken WAV files in the following way:

mplayer \
  -quiet \
  -vo null \
  -vc dummy \
  -ao pcm:waveheader:file="audio_FIXED.wav" audio_BROKEN.wav

[edit] Converting your .wav to other formats

[edit] To mp3

[edit] Using lame

LAME is a great command-line tool for converting audio files to MP3. Basic usage:

lame (options) input.wav output.mp3

These are nice general settings to use:

lame -V0 -h -b 160 --vbr-new input.wav output.mp3


  • -V n 0 <= n <= 9
    • Enable VBR (Variable BitRate) and specifies the value of VBR quality (default = 4). 0 = highest quality.
  • -h
    • Use some quality improvements. Encoding will be slower, but the result will be of higher quality. The behaviour is the same as the -q 2 switch. This switch is always enabled when using VBR.
  • -b n
    • For MPEG1 (sampling frequencies of 32, 44.1 and 48 kHz) n = 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320
    • For MPEG2 (sampling frequencies of 16, 22.05 and 24 kHz) n = 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160
    • Default is 128 for MPEG1 and 64 for MPEG2.
  • --vbr-new
    • Invokes the newest VBR algorithm. During the development of version 3.90, considerable tuning was done on this algorithm, and it is now considered to be on par with the original --vbr-old. It has the added advantage of being very fast (over twice as fast as --vbr-old).

See the Lame manual page for more LAME encoding options.

[edit] To ogg

Use oggenc,

oggenc input.wav

..which creates input.ogg. Ogg is a very nice format used by some Linux users, but it is not supported by very many vendors. Windows users can play .ogg files, but need to install a codec (which is included with WinAmp).

Very few mp3 players support .ogg - some high-end iriver players (iriver H300 series) do by default, but after that you'll need to use 3rd party firmware - such as Rockbox.

Check the oggenc man page for all the available options.

Many video game venders use .ogg for game sounds and music, and an increasing number of mp3 players are beginning to support the .ogg vorbis format. Many tests have shown that .ogg provides equal or better sound quality when compared with .mp3s, but with lower bit rate and smaller file size.

[edit] Batch converting audio files

This little script may be of interest and value:

File: convertomp3
# Usage: convertomp3 fileextention
if [ $1 = "" ];then
  echo 'Please give a audio file extention as argument.'
  exit 1

for i in *.$1
  if [ -f "$i" ]; then
  rm -f "$i.wav"
  mkfifo "$i.wav"
  mplayer \
   -quiet \
   -vo null \
   -vc dummy \
   -af volume=0,resample=44100:0:1 \
   -ao pcm:waveheader:file="$i.wav" "$i" &
  dest=`echo "$i"|sed -e "s/$1$/mp3/"`
  lame -V0 -h -b 160 --vbr-new "$i" "$dest"
  rm -f "$i"

Running convertomp3 wma will covert every .wma file in the current folder to .mp3.

Personal tools
hardware tests
Privacy policy
linux events


linux newz | random page | poetry | free blog