Convert audio between MP3, FLAC, WAV - including hi-fi and lo-fi and short clips

by Derek Sivers

Related link: http://sox.sourceforge.net/



Using Sox, LAME, FLAC, and madplay - I put together this "dictionary" of shell commands that can be run to convert audio from one format to other, including dialup-modem-streamable lofi mono MP3 files, and including shorter clips of music.

Above each command-line and the INPUT and OUTPUT audio formats. (represented by $in and $out in the command line.)

For short audio clips, you need $start - what second to start the clip at, and $duration - how long the clip should go. All clips have a 1-second fade-in, and 2-second fade-out.


flac wav
flac -sd $in -o $out

flac wav clip
flac -sdc $in | sox -t wav - -t raw -s -w -c 2 - trim $start $duration | sox -t raw -r 44100 -s -w -c 2 - $out fade h 1 $duration 2

flac wav-lofi
flac -sdc $in | sox -t wav - -s -w -c 1 -r 22050 $out rate

flac mp3-hifi
flac -sdc $in | lame - $out

flac mp3-hifi clip
flac -sdc $in | sox -t wav - -t raw -s -w -c 2 - trim $start $duration | sox -t raw -r 44100 -s -w -c 2 - -t wav - fade h 1 $duration 2 | lame - $out

flac mp3-lofi
flac -sdc $in | sox -t wav - -t wav -s -w -c 1 -r 22050 - rate | lame -b 80 - $out

flac mp3-lofi clip
flac -sdc $in | sox -t wav - -t raw -s -w -c 1 -r 22050 - rate | sox -t raw -r 22050 -s -w -c 1 - -t raw -r 22050 -s -w -c 1 - trim $start $duration | sox -t raw -r 22050 -s -w -c 1 - -t wav - fade h 1 $duration 2 | lame -b 80 - $out

wav flac
flac -s $in -o $out

wav wav clip
sox -t wav $in -t raw -s -w -c 2 - trim $start $duration | sox -t raw -r 44100 -s -w -c 2 - -t wav $out fade h 1 $duration 2

wav wav lofi
sox -t wav $in -s -w -c 1 -r 22050 $out rate

wav mp3-hifi
lame $in $out

wav mp3-hifi clip
sox -t wav $in -t raw -s -w -c 2 - trim $start $duration | sox -t raw -r 44100 -s -w -c 2 - -t wav - fade h 1 $duration 2 | lame - $out

wav mp3-lofi
sox -t wav $in -t wav -s -w -c 1 -r 22050 - rate | lame -b 80 - $out

wav mp3-lofi clip
sox -t wav $in -t raw -s -w -c 1 -r 22050 - rate | sox -t raw -r 22050 -s -w -c 1 - -t raw -r 22050 -s -w -c 1 - trim $start $duration | sox -t raw -r 22050 -s -w -c 1 - -t wav - fade h 1 $duration 2 | lame -b 80 - $out

mp3 flac
madplay -q -o wave:- $in | flac -s - -o $out

mp3 wav clip
madplay -q -o wave:- $in | sox -t wav - -t raw -s -w -c 2 - trim $start $duration | sox -t raw -r 44100 -s -w -c 2 - $out fade h 1 $duration 2

mp3 wav lofi
madplay -q -o wave:$out -m -R 22050 $in

mp3 mp3-hifi
cp $in $out

mp3 mp3-hifi clip
madplay -q -o wave:- $in | sox -t wav - -t raw -s -w -c 2 - trim $start $duration | sox -t raw -r 44100 -s -w -c 2 - -t wav - fade h 1 $duration 2 | lame - $out

mp3 mp3-lofi
madplay -q -o wave:- -m -R 22050 $in | lame -b 80 - $out

mp3 mp3-lofi clip
madplay -q -o wave:- -m -R 22050 $in | sox -t raw -r 22050 -s -w -c 1 - -t raw -r 22050 -s -w -c 1 - trim $start $duration | sox -t raw -r 22050 -s -w -c 1 - -t wav - fade h 1 $duration 2 | lame -b 80 - $out


Got any more single-line open-source audio-converting commands to add?


8 Comments

aristotle
2004-11-07 16:31:17
Almost a makefile
With just a little tweaking, you could make this an actual Makefile. You'd just say make foo.ogg assuming there's a foo.mp3 and you wouldn't have to remember anything. And assuming the Makefile's in the directory or you've put a #!/usr/bin/make -f at the top of the Makefile, made it executable, and put it in your bin directory with a sensible name.
dereksivers
2004-11-08 00:19:54
Almost a makefile
Go for it! I'm going to be turning them into some PHP classes to interface with some other stuff I'm doing, but please feel free to take this stuff (that's why I posted it!) and use it for whatever you'd like.
ChrisMcAvoy
2004-11-09 08:03:25
Nice List
Thanks for posting this. The one gotcha I've found in this sort of batch processing is file names. I have a ton of MP3's with whitespace in the names.
mike baas
2004-11-09 11:11:33
Nice work!
Nice nice work, Derek!
green944
2004-11-22 14:07:40
Nice List

Try this one :)


ls *.flac |while read a; do n=$(basename "$a" .flac); flac -sdc "$a" |lame -V 1 - "${n}.mp3" ; done

digitmachine
2005-02-18 13:33:27
I got one too
just save this into a file
such as convert.sh and make it executable


#!/bin/bash


FILES=$(ls *.flac | cut -d '.' -f1)


for i in $FILES; do
echo converting: $i.flac
flac -sdc $i.flac | lame - $i.mp3
done

jrs
2005-06-26 22:12:49
convert using PHP?
I need to convert WAV to MP3. I want to do it on a server that I don't have administrative access to. I assume the server has any normal Unix libraries, but I don't know about special (e.g. audio format plugins) libraries or plug-ins. I am using PHP as my main script language and I want to make the final mp3 file available for download. Can you help me?
dereksivers
2005-06-26 23:00:17
convert using PHP?
Try putting any of the commands in the article, above, into the PHP command shell_exec()


Even without admin priveleges on the server you might be able to use "lame" or other MP3 encoders they have installed on their server.


Probably best to just email your web-host and ask what server-side MP3 encoders they have.