How to use Ffmpeg to convert wma to mp3 recursively, importing from txt file?

This is the command I use for ffmpeg all the files in the current directory (works if names have spaces in them) on Mac where brew doesn't have avconv:

for file in *.wma; do ffmpeg -i "${file}"  -acodec libmp3lame -ab 192k "${file/.wma/.mp3}"; done

In a terminal, first browse to the folder that contains all of your music using cd, for example:

cd /home/username/music/wma-to-convert

The following will make a list of all files in the current folder and all subfolders ending in "wma" and store the list in a text document called wma-files.txt:

find . -type f | grep wma$ > wma-files.txt

Or you could create the text file manually if you want. Then type the following in a text editor and save it in the same directory of wma-files.txt, for example naming it conv-script:

#!/usr/bin/env bash

readarray -t files < wma-files.txt

for file in "${files[@]}"; do
    out=${file%.wma}.mp3
    probe=`avprobe -show_streams "$file" 2>/dev/null`
    rate=`echo "$probe" | grep "^bit_rate" | sed "s:.*=\(.*\)[0-9][0-9][0-9][.].*:\1:" | head -1`
    ffmpeg -i "$file" -ab "$rate"k "$out"
done

You probably have to set the executable bit on the script:

chmod +x conv-script

Then just run it:

./conv-script

You also have the option of adding this to the end of the ffmpeg line, but be careful:

 && rm "$file"

For those who don't have access to avprobe, you can use ffprobe which does the equivalent (i.e. getting the bit_rate). To do that:

Replace:

probe=`avprobe -show_streams "$file" 2>/dev/null`
rate=`echo "$probe" | grep "^bit_rate" | sed "s:bit_rate=\([0-9]\+\)[0-9]\{3\}:\1:" | head -1`

with:

rate=`ffprobe -v error -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1 "$file"`

Once this is done, all your files should start converting and the wma originals should get deleted! This worked great for me converting flac files to mp3. Unfortunately I don't have any wma to test, but I expect it should do fine for those as well.

NOTE: While I don't foresee any problems, it would be a good idea to copy a few files to a new directory and test it on them.

If you plan to often convert Wma to mp3, the below script automatically convert and delete everything in the folder given as an argument, It improves a bit the above-mentioned. In addition, the ffmpeg command is a bit different than the first proposed, if the first one does not work, use the below one.

#!/usr/bin/env bash

cd "$1"
find . type f | grep wma$ > wma-files.txt

readarray -t files < wma-files.txt

for file in "${files[@]}"; do
    out=${file%.wma}.mp3
    ffmpeg -i "$file" -map_metadata 0:s:0 $out"
done
rm wma-files.txt

Then, you simply call it by typing in you terminal as:

./convScript /home/username/music

Thus, all the music folder and its subfolders will have its wma music automatically converted in mp3.

As before, you can add this to end of the ffmpeg line for file removal:

&& rm "$file"

EDIT (2017/06/23): don't make rm the default


For any version

I found a neat way to do this with mplayer. This script will rename the files to remove all blank spaces in the names of the files and will recursively convert all .wma files in the current directory to .mp3:

#!/bin/bash
for f in *.wma; do
 newname=`echo $f | tr ' ' '_' `
 mv "$f" $newname
 f=$newname
 mplayer $f -novideo -ao pcm:file=tmp.wav
 lame -V 0 -q 0 tmp.wav ${f/.wma/.mp3}
 rm -f tmp.wav
done

Here's a one liner version:

for f in *.wma; do; newname=`echo $f | tr ' ' '_' `; mv "$f" $newname; f=$newname; mplayer $f -novideo -ao pcm:file=tmp.wav; lame -V 0 -q 0 tmp.wav ${f/.wma/.mp3}; rm -f tmp.wav; done

I rewrite TheSchwa script (thanks TheSchwa!), with these changes:

  • avprobe detection works (at least on ffprobe version 2.5.2 from current Debian unstable).
  • Don't delete files with rm (convertors usually don't delete files, this can be a big surprise!).
  • Load wma files in script.
  • Help when missing binaries.
#!/bin/bash

which ffmpeg > /dev/null || { echo "Install ffmpeg, with 'sudo apt-get install ffmpeg'" >&2; exit 1; }
which avprobe > /dev/null || { echo "Install libav-tools, with 'sudo apt-get install libav-tools'" >&2; exit 1; }

output_format=mp3
file=`mktemp`
find . -type f | grep -i wma$ > $file

readarray -t files < $file

for file in "${files[@]}"; do
    out=${file%.wma}.$output_format
    probe=`avprobe -show_streams "$file" 2>/dev/null`

    rate=`echo "$probe" | grep "^bit_rate" | sed "s:bit_rate=\([0-9]\+\)[0-9]\{3\}:\1:" | head -1`
    ffmpeg -i "$file" -ab "$rate"k "$out"
done