How to convert from WMA to MP3

Converting Windows Media Audio files into MP3 format is rather easy with mplayer and lame. My car stereo plays MP3s, so WMA is of no use to me!
This code is based on this script but I have to wonder why they overwrote the wma file?
Piping the output of mplayer into lame is left as an easy lesson for the reader!
for i in *.wma ; do mplayer -vo null -vc dummy -af resample=44100 -ao pcm -waveheader "$i" && lame -m j -h --vbr-new -b 160 audiodump.wav -o "`basename "$i" .wma`.mp3"; done; rm -f audiodump.wav


You might also like

If you like this post then please subscribe to my full RSS feed. You can also click here to subscribe by email. There are also my fabulous photos to explore too!

34 thoughts on “How to convert from WMA to MP3


  1. Thanks. I recently found a similar technique for m4 files. In case you don’t want the big temporary file, you can use a fifo. Here’s my script:
    #!/bin/bash
    #
    # Dump wma to mp3

    for i in *.wma
    do
    if [ -f $i ]; then
    rm -f “$i.wav”
    mkfifo “$i.wav”
    mplayer -vo null -vc dummy -af resample=44100 -ao pcm -waveheader “$i” -aofile “$i.wav” &
    dest=`echo “$i”|sed -e ‘s/wma$/mp3/’`
    lame -h -b 192 “$i.wav” “$dest”
    rm -f “$i.wav”
    fi
    done


  2. I would just like to thank the above(/below?) person for replying with his method of converting WMA’s to MP3′s.
    It helped me. :)


  3. The latest version of mplayer complains about -ao pcm -waveheader – actually, it just needs to be replaced with -ao pcm:waveheader, and everything works again. Btw, thanks for the instruction, it is both useful and educational ;)


  4. and did anyone bother to make it to choose appropriate bit rate and mono/stereo depending on what was in the original wma??


  5. For latest versions of mplayer (on Fedora Core 4), you will need the following instead:

    for i in *.wma
    do
    filename=`basename "$i" .wma`

    #Rip with Mplayer / encode with LAME
    echo "Ripping $i"
    mplayer -quiet -vo null -vc dummy -af volume=0,resample=44100:0:1 -ao pcm:waveheader "$i"
    echo "Encoding $i to "$filename".mp3"
    lame -quiet -m s audiodump.wav -o "$filename".mp3

    rm audiodump.wav
    done


  6. On SuSE 9.3, (MPlayer 1.0-pre7) got it working (with the FIFO) sweetly with


    #!/bin/bash
    #
    # Dump wma to mp3
    for i in *.wma
    do
    if [ -f "$i" ]; then
    rm -f "$i.wav"
    mkfifo "$i.wav"
    mplayer -quiet -vo null -vc dummy -af volume=0,resample=44100:0:1 -ao pcm:waveheader:file="$i.wav" "$i" &
    dest=`echo "$i"|sed -e 's/wma$/mp3/'`
    lame -V0 -h -b 160 --vbr-new "$i.wav" "$dest"
    rm -f "$i.wav"
    fi
    done

    I like hq VBR, but you can change the lame settings as you like. Note the MPlayer settings have changed.


  7. I recentlt download a media file with a m4. extension and found that I could’nt open it udwer Windows XP . How do I eliviate this problem ? . If ou have any solutions please email me .


  8. Hey, *really* use the power of Unix and skip creating the intermediate .wav file by piping mplayer output to a fifo, and having lame read that as its stdin!

    e.g:

    if [[ -e ./pcm_audio ]]
    then
    else
    mkfifo ./pcm_audio
    fi

    function wmatomp3 {
    lame \
    –tt “$title” \
    –ty “$year” \
    –ta “$artist” \
    –tc “$comment” \
    –tg “$genre” \
    –tl “$album” \
    –quiet -h – “$mp3file”


  9. Let’s try that again:

    Hey, *really* use the power of Unix and skip creating the intermediate .wav file by piping mplayer output to a fifo, and having lame read that as its stdin!

    e.g:


    if [[ -e ./pcm_audio ]]
    then
    else
    mkfifo ./pcm_audio
    fi

    function wmatomp3 {
    lame \
    --tt "$title" \
    --ty "$year" \
    --ta "$artist" \
    --tc "$comment" \
    --tg "$genre" \
    --tl "$album" \
    --quiet -h - "$mp3file"


  10. OK, one more time, just the function:


    function wmatomp3 {
    lame \
    --tt "$title" \
    --ty "$year" \
    --ta "$artist" \
    --tc "$comment" \
    --tg "$genre" \
    --tl "$album" \
    --quiet -h - "$mp3file"



  11. function wmatomp3 {
    lame \
    --tt "$title" \
    --ty "$year" \
    --ta "$artist" \
    --tc "$comment" \
    --tg "$genre" \
    --tl "$album" \
    --quiet -h - "$mp3file"


  12. ok. it doesn’t like ampersands.
    following the “$mp3file” should be:
    [ampersand]

    mplayer -quiet -af resample=44100 -ao pcm:waveheader -ao pcm:file=pcm_audio -vc dummy -vo null -playlist $url
    }


  13. here is what I have done:


    remove_spaces=1

    tmp_file="pcm_audio"

    function rm_spaces() {
    mv "$1" `echo $1 | tr ' ' '_'`
    }

    function tomp3() {
    ext=$1

    for i in *.$ext
    do
    if [ -f "$i" ]; then
    dest=`basename "$i" .$ext`

    if [ "$ext" = "ogg" ]; then
    echo "Encoding $i to "$dest".mp3"
    transcode -q 0 -i "$i" -o "$dest" -y null,lame 2>/dev/null
    else
    rm -f "$tmp_file"
    mkfifo "$tmp_file"

    #echo "Ripping $i"
    mplayer -quiet -vo null -vc dummy -af volume=0,resample=44100:0:1 -ao pcm:waveheader:file="$tmp_file" "$i" &>/dev/null &

    echo "Encoding $i to "$dest".mp3"
    lame --quiet -m s -h -b 192 -V0 --vbr-new "$tmp_file" "$dest".mp3

    # remove temp file
    rm -f "$tmp_file"

    fi
    # remove spaces
    if [ $remove_spaces = 1 ]; then
    rm_spaces "$dest".mp3
    fi
    fi
    done

    echo "done."
    }

    case "$1" in
    ogg)
    tomp3 ogg
    ;;
    wma)
    tomp3 wma
    ;;
    *)
    echo "Usage: tomp3.sh wma|ogg"
    exit 1
    esac

    exit 0


  14. Thanks. I had to burn Audio CDs with 3rd party Windows programs then Rip those in unix to get the MP3 formats. The above bashes help a lot.


  15. Sorry Wilson, it probably means you are on the completely wrong page because this is how to convert WMA to MP3 files for Linux and if you don’t speak computer then I very much doubt you are a Linux user


  16. Excellent! Anyone want to share how to recursively traverse directories to do this? And maybe removal of the wma file following encoding completion?

    -Jonny5


  17. Here is a similar script I made.
    It can recursively look for files in subdirectories by passing -r, as well as it can remove the wma file for you by passing -d. The script does not need to remove any whitespaces, so you can use nice file names! ;D

    It is a bit too long to post it here, so plz use this link: http://88.198.16.39/seek/make-mp3

    You can easyly encode other files with it, just change the two functions on the top to do this.

    I really hope somebody will find this script usefull, since it’s one of the first shellscripts I made. (filenames with whitespaces are cruel if you don’t know how to handle it ^^;)

    Please send a mail if you have some improvements and/or better ideas.

    Have a lot of fun!
    Bernhard


  18. #!/bin/bash

    if [ $# -eq 0 ]
    then
    echo “Usage: `basename $0` FILE [FILE]…”
    exit 1
    fi

    for source in “$@”; do
    if [ -f "$source" ]; then
    base=${source%.*} # remove suffix
    base=`echo “$base” | tr ‘[A-Z]‘ ‘[a-z]‘`; #remove uppercase
    base=`echo “$base” | tr ‘ ‘ ‘_’`; #remove spaces
    tmp=$base.wav
    dest=$base.mp3

    if [ -e $tmp ]; then
    echo “Could not create temporary file $tmp: file exists”;
    exit 1;
    fi

    echo “Encoding $base.mp3″
    rm -f $tmp
    mkfifo $tmp

    #Rip with Mplayer / encode with LAME
    mplayer -quiet -vo null -vc null -ao pcm:waveheader:file=$tmp “$source” &>/dev/null \
    & lame –quiet -m s $tmp -o $dest;
    stat=$?
    rm -f $tmp

    if [ $stat -ne 0 ]; then
    echo “Interupted”
    exit $stat
    fi
    fi
    done


  19. Yep,

    Thanks since i”m using a “Peter Ndiku customed” code right now to do… , you guess ?

    Sure that’s … Pop Corn. ;)

    So thanks “Mesdames, Mesdedoiselles, Messieurs” to share, bloggy style…


  20. You guys are a gr8 help. Thanks for the tips. BTW… -ao pcm -waveheader is deprecated… try -ao pcm:waveheader instead.


  21. Hi all,

    There is a nautilus script for this in the Ubuntu repo’s.

    sudo apt-get install nautilus-script-audio-convert

    Right click file then convert, nice n’ easy.

    Dave


  22. Awesome page !

    Just learning how to script this year and am so glad I switched to Linux.
    Been making a playlist for a big party from files of all types and it’s great to do everything from the terminal !

    thanks for al the scripts

    wbg


  23. I’ve been struggling to find a way to do this without mplayer.
    It’s been a pain the the a** to get it to a reasonably productive state, but this is how it goes:
    1-run the command below to generate a script which will convert your files
    2-check the script for apostrophes or single quites or double quotes in the names of your files
    3-run the script

    find -L /path/to/your/mp3/library -iname *.wma -exec echo 'vlc -v  "{}" --sout '\'#transcode{acodec=mp3}:standard\{access=file,dst=\"{}.mp3\",mux=ffmpeg\}\''' \; >> batch_wma2mp3.sh; chmod u+x batch_wma2mp3.sh;
    

  24. this works w/latest Ubuntu & mplayer

    #!/bin/bash
    #
    # Dump wma to mp3

    for i in *.wma
    do
    if [ -f $i ]; then
    rm -f $i.wav
    mkfifo $i.wav
    mplayer -vo null -vc dummy -af resample=44100 -ao pcm:waveheader $i -ao pcm:file=$i.wav &
    dest=`echo $i|sed -e ‘s/wma$/mp3/’`
    lame -h -b 192 $i.wav $dest
    rm -f $i.wav
    fi
    done


  25. You don’t need sed. Bash has all the functionality you need. And use trap to remove temporary files:


    #! /bin/bash
    set -eu

    TMP=tmp.wav
    trap 'rm -f "$TMP"' EXIT
    mkfifo "$TMP"

    for WMA in "$@" ; do
    MP3=${WMA%.wma}.mp3
    mplayer -quiet -vo null -vc dummy -af resample=44100 -ao "pcm:waveheader:file=$TMP:fast" "$WMA" &
    lame -h -b 192 "$TMP" "$MP3"
    done

Leave a Reply

Loading Facebook Comments ...