User Tools

Site Tools


technology:linux:wma2mp3

WMA2MP3

I needed a way to convert Windows WMA files to MP3 files. I found plenty of cheap utilities for Windows, but my gut told me there had to be a built-in way to do it in Linux. I looked around, and fortunately I found two tools that can do what I want:

  • mplayer - a very versatile media player for audio and video files. It can play WMA files with no hassles.
  • LAME - an easy-to-use MP3 encoder

A quick search through the respective man pages revealed the command line I needed:

for i in *.wma ; do mplayer -vo null -vc dummy -af resample=44100 -ao pcm:waveheader $i && lame -m s audiodump.wav -o $i; done

This loops through all the WMA files in the current directory, playing each with mplayer and encoding them into MP3 files with lame.

Note: mplayer uses the Linux sound system, not stdout, so we can't use a pipe symbol to send the output of one command to the other. Instead, we use the double ampersand (&&). It lets the first command complete before deciding if the second one should run. If mplayer is successful, then lame will run against the audiodump.wav file that mplayer created.

I turned the command line into a very simple script and called it /usr/bin/wma2mp3.

#!/bin/bash
# wma2mp3 - converts a directory of *.wma files into *.mp3 files
# Chris Freyer 9/2/2009
# chris@thefreyers.net

current_directory=$( pwd )

#remove uppercase
for i in *.[Ww][Mm][Aa]; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done

#remove spaces
for i in *.wma; do mv "$i" `echo $i | tr ' ' '_'`; done

#Rip with Mplayer / encode with LAME
for i in *.wma ; do mplayer -vo null -vc dummy -af resample=44100 -ao pcm:waveheader $i && lame -m s audiodump.wav -o $i; done

#convert file names
for i in *.wma; do mv "$i" "`basename "$i" .wma`.mp3"; done

rm audiodump.wav

Here is the output of running it in a directory that contains a WMA file:

cfreyer@frankie:~/wma$ wma2mp3
mv: `01.wma' and `01.wma' are the same file
MPlayer 1.0rc2-4.3.3 (C) 2000-2007 MPlayer Team
CPU: AMD Athlon(TM) XP 1900+ (Family: 6, Model: 10, Stepping: 0)
CPUflags:  MMX: 1 MMX2: 1 3DNow: 1 3DNow2: 1 SSE: 1 SSE2: 0
Compiled with runtime CPU detection.
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing 01.wma.
ASF file format detected.
[asfheader] Audio stream found, -aid 1
Clip info:
 name: 1
==========================================================================
Forced audio codec: mad
Opening audio decoder: [ffmpeg] FFmpeg/libavcodec audio decoders
AUDIO: 44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16002->176400)
Selected audio codec: [ffwmav2] afm: ffmpeg (DivX audio v2 (FFmpeg))
==========================================================================
[AO PCM] File: audiodump.wav (WAVE)
PCM: Samplerate: 44100Hz Channels: Stereo Format s16le
[AO PCM] Info: Faster dumping is achieved with -vc null -vo null -ao pcm:fast
[AO PCM] Info: To write WAVE files use -ao pcm:waveheader (default).
AO: [pcm] 44100Hz 2ch s16le (2 bytes per sample)
Video: no video
Starting playback...
A: 220.7 (03:40.7) of 221.0 (03:41.0)  0.8%

Exiting... (End of file)
LAME 3.98 32bits (http://www.mp3dev.org/)
CPU features: MMX (ASM used), 3DNow! (ASM used), SSE
Using polyphase lowpass filter, transition band: 16538 Hz - 17071 Hz
Encoding audiodump.wav to 01.wma
Encoding as 44.1 kHz stereo MPEG-1 Layer III (11x) 128 kbps qval=3
    Frame          |  CPU time/estim | REAL time/estim | play/CPU |    ETA
  8393/8393  (100%)|    0:25/    0:25|    0:26/    0:26|   8.4913x|    0:00
-----------------------------------------------------------------------------------------------------------------------
   kbps        LR  %     long switch short %
  128.0      100.0        84.7   9.2   6.1
Writing LAME Tag...done
ReplayGain: -1.9dB
/home/cfreyer/public_html/data/pages/technology/linux/wma2mp3.txt · Last modified: 2009/10/01 10:42 by Chris Freyer