Skip to main content

Convert audio between MP3, FLAC, WAV

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.

You can use the commands in php : Try putting any of the commands in the article, into the PHP command shell_exec(). The server must have the lame installed

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