Search:  
Gentoo Wiki

TIP_Converting_Audio_files_from_one_format_to_another

Contents

Introduction

There are many different audio file formats. This article will show you how to convert from one format to another. I will focus on the console as it is what I use to do this. Feel free to add material about GUI tools.

To avoid quality loss when converting between formats, try to avoid transcoding -- converting from one lossy format to another lossy format. For more information check out: [1]

There are many different sound formats and it is often not possible to convert directly from one format to another, We will use an intermediary format: wav.

Conversion from one format to wav

MP3/MP2

We use lame:

lame --decode file.mp3

The result is file.wav

OGG/Vorbis

With ogg123 :

ogg123 -d wav -f test.wav test.ogg

where test.wav is the output file and test.ogg the input file.

MPC (Musepack)

We need media-sound/musepack-tools:

mppdec file.mpc file.wav

where file.mpc is the file to be converted and file.wav the output file.

APE (Monkey's Audio)

It is not in gentoo, but it is a working ebuild here: bug 52882 Note: don't ask for inclusion in portage, this software have an infamous original license.

emerge -av shntool
shntool conv -o flac *.ape

AAC (Advanced Audio Codec)

We use faad:

faad -o file.wav file.aac

file.wav is the output file, file.acc is the input file.

Includes .m4a files. .mp4 containers are encoded with audio and video with typically .aac as the audio format. Apple's naming conventions for audio-only .mp4 is.m4a.

.m4a lossless ( FAQ ) not supported at this time.

FLAC (Free Lossless Audio Codec)

With flac:

flac -d file.flac

The output file will be file.wav

AC3

We need ac52dec:

a52dec ac3file.ac3 -o wav > outputfile.wav

The output file will be outputfile.wav, a stereo file.

a52dec ac3file.ac3 -o wav6 > outputfile.wav

The output file will be a WavEx file: a wav file with 6 channels of enormous size!

DTS

dtsdec dtsfile.dts -o wav > outputfile.wav

Output file in wav stereo.

dtsdec dtsfile.dts -o wavdolby > outputfile.wav

Output file in wav 6 channels.

WMA, and all the Mplayer formats

Mplayer can transcode all the formats into wav.

 mplayer audio_file.xxx -ao pcm:file=audio_file.wav

Where audio_file.xxx is the input file (MP3, MP2, OGG/Vorbis, FLAC, AAC, WMA, AC3, ...) and audio_file.wav the output file.

When the input file is multi-channels, you can extract it in multi-channels wav:

mplayer audio_file -ao pcm:file=audio_multi-channel.wav -channels Y

Where Y is the channel number: 2, 4, 6.

Conversion from wav to another format

MP2

It is an old and outdated format. We will use ffmpeg:

ffmpeg -i file.wav -acodec mp2 -ac 2 -ar 44100 -ab 224 -y file.mp2

where:

MP3

Not the better format. Prefer a bitrate equal or greater as 128kbits.

$ lame file.wav file.mp3 -b 128 -h

Where file.wav is the input file, file.mp3 the outputfile, -b 128 the bitrate and -h give a better quality but take more time to encode. We can alternatively use -f that speed the encoding but give a lower quality.

$ lame file.wav file.mp3 -V 4

give a MP3 VBR file, it mean with a variable bitrate. When the sound become more complex, the bitrate is increased, and when the sound is calm, the bitrate is decreased. -V give the profile to use: 0 give a big file but a very good quality, 9 give a little file.

OGG/Vorbis

OGG is often called for the audio codec Vorbis. In fact, the audio codec is Vorbis that is included in an OGG container. That said, OGG is one of the free audio format. It gives a better quality for the same bitrate as MP3, so we can win in size or in quality. An OGG file at 80 Kbits would be equivalent to an MP* file at 128 Kbits! Ogg/Vorbis work with VBR by default. It can support up to 254 audio channels and can be utilized at many sampling rate.

We will use oggenc (from media-sound/vorbis-tools) to encode a wav file:

oggenc file.wav -q X -o file.ogg

where X is the quality profile:

Your output file will have the specified sampling rate as middle value, and the encoder will change this value as needed. The default quality level is 3. Fractional quality level as 4.5 are permitted.

MPC (Musepack)

MPC is a codec that, as the OGG/Vorbis, work only with variable bit rate (VBR). To encode in MPC, we must use either profiles or quality values. For that, we will use mppenc:

$ mppenc --telephone file.wav file.mpc

where we use a profile to determine the bitrate:

Or with a quality value:

mppenc --quality X file.wav file.mpc

where X can be:

All those values was measured from an audio test file.

APE

Lossless audio codec with infamous license. Use FLAC instead.

AAC (Advanced Audio Codec)

Of very current usage in video encoding, AAC can archive a very high audio quality at low bit rate (less or equal to 64 kbits). The AAC can even do more, an ACC file at 96 kbits is equivalent to a MP3 at 128 kbits. The AAC also support the 5.1. With the program faac:

faac file.wav file.aac -q XX

where we the output quality is in per cent. As example, -q 90 is for 90% of the original quality.

faac fichier.wav fichier.aac -b XX

where the bitrate is specified.

Tric:

To get a good quality ACC at 64 kbits, we change the file sampling rate at 22050 Hz:

mplayer file_audio.wav -ao pcm:file=file_audio_22khz.wav -srate 22050

Then:

faac file_audio_22khz.wav file.aac -b 64 -c 10000

where -c is the cut-off frequency of the applied low pass filter.

FLAC (Free Lossless Audio Codec)

The FLAC format is one of the so-called Lossless audio codec. That is at this codec will compress the audio without any quality loss. Contrary in MP3, OGG/Vorbis, AAC,..., its algorithm will not suppress any information to compress. FLAC is the audio codec to use in order to archive an identical copy of an audio CD. The bitrate of a flacfile is huge, up to 800 kbits. To encode in FLAC, we use flac:

flac fichier.wav fichier.flac -X

where X is the wanted compression level: 0 for a fast encoding, 8 for a slow encoding. The default is 5.

AC3

Here, we will encode a wav file into AC3. This can be useful if you want to make a video DVD. Be aware at the bitrate of an AC3 file must respect given values. We are doing here only stereo AC3 at 192 kbits (2.0):

ffmpeg -i file.wav -ab 192 -ar 44100 -ac 2 -acodec ac3 -y moi.ac3

where -ar is the sampling frequency and -y overwrite the output file if it already exist.

Conversion from a format X to a format Y

It will be more useful to be able to do the conversion in one single step. For that, we need some scripts that will do the necessary steps for us:

WMA2FLAC

We have some WMA files and we want to convert them into FLAC files:

File: /usr/local/bin/wma2flac
#!/bin/bash
for i in *.wma; do name=`basename "$i" .wma`; mplayer "$i" -ao pcm:file="$name.wav"; rm "$i"; done
for i in *.wav; do flac "$i" "$i.flac"; rm "$i"; done

The first line will transcode all the WMA files in the current directory into wav files and erase the original files. The second line will convert the wav files into FLAC files and erase the wav files. If you don't want to erase the original files and/or the wav files, remove the

rm "$i";

statement in the corresponding line(s).

MPC2OGG

To convert some MPC file into OGG/Vorbis files:

File: /usr/local/bin/mpc2ogg
#!/bin/bash
for i in *.mpc; do name=`basename "$i" .mpc`; mppdec "$i" "$name.wav"; rm "$i"; done
for i in *.wav; do oggenc -q 10 "$i"; rm "$i"; done

It will be better to be able to tell the script which quality we want for the OGG/vorbis files:

File: /usr/local/bin/mpc2ogg
#!/bin/bash
echo "Enter the number corresponding to the quality"
echo "you want to use and press Enter:"
echo ""
echo "0 = 64 kbits"
echo "0.5 = 72 kbits"
echo "1 = 80 kbits"
echo "2 = 96 kbits"
echo "3 = 112 kbits"
echo "4 = 128 kbits"
echo "5 = 160 kbits"
echo "7 = 224 kbits"
echo "8 = 256 kbits"
echo "9 = 320 kbits"
echo "10 = 500 kbits"
echo "fractional value such as x.y are permitted"
read QUALITY

if [[ "${QUALITY}" != [0-9] ]] && [[ "${QUALITY}" != [0-9].[0-9] ]] && [[ "${QUALITY}" != 10 ]]; then
     echo "You must specify only numerical values !"
     exit
fi

for i in *.mpc; do name=`basename "$i" .mpc`; mppdec "$i" "$name.wav"; rm "$i"; done
for i in *.wav; do oggenc -q "$QUALITY" "$i"; rm "$i"; done

What if you want to be able to choose if you want to keep or not the temporary wav files and/or the original files?

We must add 2 tests in the script:

File: /usr/local/bin/mpc2ogg
#!/bin/bash
echo "Enter the number corresponding to the quality"
echo "you want to use and press Enter:"
echo ""
echo "0 = 64 kbits"
echo "0,5 = 72 kbits"
echo "1 = 80 kbits"
echo "2 = 96 kbits"
echo "3 = 112 kbits"
echo "4 = 128 kbits"
echo "5 = 160 kbits"
echo "7 = 224 kbits"
echo "8 = 256 kbits"
echo "9 = 320 kbits"
echo "10 = 500 kbits"
echo "fractional values such as x.y are permitted"
read QUALITY

if [[ "${QUALITY}" != [0-9] ]] && [[ "${QUALITY}" != [0-9].[0-9] ]] && [[ "${QUALITY}" != 10 ]]; then
     echo "You must specify only numerical values!"
     exit
fi

for i in *.mpc; do name=`basename "$i" .mpc`; mppdec "$i" "$name.wav"; done
for i in *.wav; do oggenc -q "$QUALITY" "$i"; done

testtmpf() {
	if [[ "${TMPF}" == n ]]; then
	rm *.wav
	else
		if [[ "${TMPF}" != y ]]; then
			readtmpf
		fi
	fi
}

readtmpf() {
	echo "Do you want to keep the temporary files? y/n"
	read TMPF
	testtmpf
}

testorgf() {
	if [[ "${ORGF}" == n ]]; then
	rm *.mpc
	else
		if [[ "${ORGF}" != y ]]; then
			readorgf
		fi
	fi
}

readorgf() {
	echo "Do you want to keep the original files? y/n"
	read ORGF
	testorgf
}

readtmpf
readorgf

A special case: CDDA2FLAC

We will use cdda2wav for that. cdda2wav do a better job with scsi emulation as with ide-cd. In order to be able to use that script, you need to remove IDE/ATAPI CDROM support from the kernel and add IDE_SCSI (SCSI emulation) support.

Note for the septics: I was able to convert some audio CD with that script that all other programs I try failed to extract. And not every one have an old plextor drive and the new ones are not worth the money with a hardware incompatible with free software!!!.

cdda2wav will use directly the Bus,ID,Lun syntax for the devide id on the generic_scsi interface. See man cdda2wav for the details.

Note: The fs=xx is normally not needed, but due to a non fixed kernel bug, we need to use fs=64k in order to get the generic_scsi interface to work as expected.

File: /usr/local/bin/cdda2flac
# !/bin/bash
# Demo script for processing all audio tracks with a flac encoder
# This variant creates temporary wav files.
#
# usage: cdda2flac <name prefix for all flac files>
#
# list_audio_tracks is a (symbolic) link to cdda2wav
# and used to generate a list of audio track numbers and start
# sectors, which in turn are used in a loop to spawn cdda2wav
# and the post processor on a track by track basis.

# specify the audio track listing program and its options
LAT=list_audio_tracks
LAT_OPTIONS="-D 2,0,0 ts=64k"
# It seams at -D<device> is necessary with the ide-scsi driver (not sure
# but that was not working without).

# specify the sampling program and its options
# do not specify the track option here!
CDDA2WAV=cdda2wav
CDDA2WAV_OPTS="-Owav -H -P0 -q -x -D 2,0,0 ts=64k"
#CDDA"WAV_OPTS="-B -D 2,0,0 ts=64k -x"
# -x is for the best quality with cdda2wav

# for normal use, comment out the next line with a #
#DEBUG="-d1"

# specify the post processing program and its options
MP_CODER=flac
MP_OPTIONS=""

WAVFILE=$$".wav"

FILEPREFIX=${1:-track}

# clean up wav file on exit, abort, ...
trap "rm -rf $WAVFILE" 0 2 3 4 6 7 8 10 11 12 13 15

# feed track numbers and start sectors into loop
$LAT $LAT_OPTIONS | while read TRACK STARTSECTOR;
do
  $CDDA2WAV $CDDA2WAV_OPTS -t$TRACK $DEBUG $WAVFILE
  $MP_CODER $MP_OPTIONS $WAVFILE -o "$FILEPREFIX$TRACK.flac"

  # check result code
  RES=$?
  if [ $RES = 0 ] ; then
    echo File $FILEPREFIX$TRACK.flac finished successfully.
    rm $WAVFILE
  else
    echo File $FILEPREFIX$TRACK.flac failed \(result $RES\). Aborted. >&2
  fi
done

You may need to adjust the device used by cdda2wav. To get a description of the devices on the SCSI bus, run:

$ cdda2wav --status

as root. Change the -D 2,0,0 statements in LAT_OPTIONS= and CDDA2WAV_OPTS= accordingly.

Using sox

An alternative to using a custom script is found in sox. sox is a command-line tool that can convert between many audio formats, resample files, filter them or even add different effects. The usage is quite simple

sox infile.abc outfile.xyz

The command

sox -h

will show you the supported formats and effects (depends on USE flag settings).

Fixing bad mp3 files to suit a portable player

Sometimes your portable player won't correctly decode some mp3 files, and will keep freezing upon them. FFmpeg may give some "invalid new backstep" errors while recoding those, but who knows what's wrong or how to fix it? Or sometimes the problem is you hear some artifacts or strange noises in the file, but just when using the portable player; in the computer, the files seem just fine. That's not a defect of the player or your earphones, it is that these are files encoded in joint-stereo, which some players don't handle well.

The fast and unexpensive solution for both problems is to recode the files to wav, then back to mp3. The files would've then been corrected by the encoder itself, making them suitable for your portable mp3 player. The only drawback is that you'd lose the id3tags, but sometimes you don't have a choice, and you could easily retag them with, for instance, EasyTag.

This script follows those steps and also recodes ogg files to mp3, for the players that still don't handle those. I believe it should be quite elegant, thought certainly a LAME solution; but this' GPL, so please change it who might.

The use is

mp3fix filenames

The new files will be created in a subfolder named mp3fix, with the same file names as the original files.

File: /usr/local/bin/mp3fix
#!/bin/bash
#mp3fix by hiok
# este script converte arquivos mp3 e ogg para wav, os recodifica pelo LAME para mp3 e os renomeia adequadamente.
# Fontes: "TIP Converting Audio files from one format to another" in gentoo-wiki.org; andrewault.blogspot.com (mass
# file renaming in unix); Unix-faq, unit 2.6 in faqs.org

mkdir mp3fix
for f in $*; do cp "$f" mp3fix; done
mkdir mp3fix_temp
for f in $*; do cp "$f" mp3fix_temp; done

cd mp3fix_temp

for f in *.mp3; do lame --decode "$f"; done
for f in *.ogg; do ogg123 -d wav -f "$f".wav "$f"; done

for d in *.wav; do lame --replaygain-accurate -m s -V 5 "$d" "$d".mp3; done

for f in *.mp3.wav.mp3; do g=`echo $f | sed -e "s/.mp3.wav.mp3/.mp3/"`; mv "$f" "$g"; done
for f in *.ogg.wav.mp3; do g=`echo $f | sed -e "s/.ogg.wav.mp3/.mp3/"`; mv "$f" "$g"; done

rm *.ogg
rm *.wav

cp * ..
cd ..
rm -r mp3fix_temp
exit

Convert all MP3 files to AAC (mp4)

AAC gives a very high quality audio at lower bitrate. Most mp3 players can play AAC such as Zune, all Ipods, Creative, Sony, etc...

This Ruby script will convert all the mp3 files in the directory to aac and also keep the proper id3tag for the new aac file. ;) It will create a WAV file then a mp4, it will not delete the original mp3 or wav. You must delete the wavs and mp3s later.

file: mp3_to_aac.rb

#!/usr/bin/env ruby
#
# Writen by Frederico de Souza Araujo.
# Free to redistribute or modify.
#
# Required Software
# - ruby
# - Mplayer (with aac/mp3 support)
# - id3tool
# - faac


# Set default variable bitrate (VBR) quantizer quality in percent.
# default: 100, averages at approx. 120-150 kbps VBR for a normal
# stereo input file with 16 bit and 44.1 kHz sample rate; 
# max.value 500, min. 10.
# for ipod nano/shuffle I'd use 85
@quality="100"

# get all files in the directory
@files = Dir['**/*.mp3']

# get id3 from the mp3
def get_id3(file)
	a = `id3tool "#{file}"`
	a.gsub!("\t", "")
	a = a.split("\n")
	a.each do |t|
		t.rstrip!
	end
	id3={
		:song => a[1].gsub("Song Title:",""),
		:artist => a[2].gsub("Artist:",""),
		:album => a[3].gsub("Album:",""),
		:note => a[4].gsub("Note:",""),
		:track => a[5].gsub("Track:",""),
		:year => a[6].gsub("Year:",""),
		:genre => a[7].gsub("Genre:","")
	}
	id3
end

# convert mp3 to wav
def mp3_to_wav(mp3)
	wav = mp3.gsub(".mp3","")
	exec = "mplayer -ao pcm:file=\"#{wav}.wav\" \"#{mp3}\""
	#puts exec
	`#{exec}`
end

# convert wav to mp3
def wav_to_aac(wav,id3)
	wav = wav.gsub(".mp3","")
	exec = "faac \
		--title \"#{id3[:song]}\" \
		--artist \"#{id3[:artist]}\" \
		--album \"#{id3[:album]}\" \
		--comment \"#{id3[:note]}\" \
		--track \"#{id3[:track]}\" \
		--year \"#{id3[:year]}\" \
		--genre \"#{id3[:genre]}\" \
		-q #{@quality} \
		\"#{wav}.wav\" \
		-o \"#{wav}.mp4\""
	#puts exec
	`#{exec}`
end


def start_all
	@files.each do |file|
		id3 = get_id3(file)
		mp3_to_wav(file)
		wav_to_aac(file,id3)
	end
end

# Run the Process now
start_all



Notes

I may be wrong when stating at it is not possible to use cdda2flac with ide-cd. So, please correct this article with a working ide-cd solution if it is the case. I try on a 2.6.20.3 kernel and it was not working, I recompiled the kernel without ide-cd but with ide-scsi and it work.

See also

Retrieved from "http://www.gentoo-wiki.info/TIP_Converting_Audio_files_from_one_format_to_another"

Last modified: Sun, 12 Oct 2008 03:33:00 +0000 Hits: 11,576