Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Friday, May 25, 2007

Audio file conversion

Here's a collection of small scripts for converting audio file formats using lame, mpg123, and mplayer.

wav2mp3.pl

#!/usr/bin/perl
use strict;
use warnings;

my $in = "/home/doug/music/somedir/";
my $out = $in;

opendir DH_IN, $in || die "Can't open $in: $!";
foreach my $name (sort readdir(DH_IN)) {
next unless $name =~ /\.wav$/;
print "NAME: $name\n";
my($base_name) = ($name =~ /^(.*)\.wav$/);
my $in_file = "${in}$base_name.wav";
my $out_file = "${out}$base_name.mp3";
`lame -b 192 $in_file $out_file`;
}
close DH_IN;


wav2mp3.sh

#!/bin/sh
bitrate="192"
for wavfile in *.wav
do
echo "input: $wavfile"
mp3file=`echo $wavfile | sed s/\\.wav/.mp3/`
echo "output: $mp3file"
lame -b $bitrate $wavfile $mp3file
done


mp32wav.sh

#!/bin/sh
for mp3file in *.mp3
do
echo "input: $mp3file"
wavfile=`echo $mp3file | sed s/\\.mp3/.wav/`
echo "output: $wavfile"
mpg123 -w $wavfile $mp3file
done


wma2mp3.sh

#!/bin/sh

if [ -z "$1" ]
then
echo "Usage: wma2mp3 "
exit 1
fi

name=`echo $1 | tr ' ' '_' | tr '[A-Z]' '[a-z]'`

name="`basename "$name" .wma`.mp3"

cp "$1" $name

mplayer -vo null -vc dummy -af resample=44100 -ao pcm:waveheader $name && lame -m s -h --vbr-new audiodump.wav -o $name


m4a2mp3.sh

#!/bin/sh

MPLAYER=mplayer
LAME=lame

for m4a in *.m4a
do

mp3=`echo "$m4a" | sed -e 's/m4a$/mp3/'`
wav="$m4a.wav"

if [ ! -e "$mp3" ]; then
[ -e "$wav" ] && rm "$wav"
mkfifo "$wav" || exit 255
mplayer "$m4a" -ao pcm:file="$wav" &>/dev/null &
lame "$wav" "$mp3"
[ -e "$wav" ] && rm "$wav"
fi
done

No comments:

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.

Labels