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

35,292

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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

Solution 5

Note: the following does not apply to 15.04, 15.10 or 16.04

First:

ffmpeg is deprecated in 14.04 anyhow and you should now use avconv instead.

sudo apt-get install avconv

Use this command to convert wma files to mp3 recursively:

for i in `find . -type f -name '*.wma'`; do avconv -i "$i" "${i/.wma/.mp3}"; done

Then, once you have verified the quality of the files, you may run this command to remove or delete the original .wma versions recursively:

for i in `find . -type f -name '*.wma'`; do rm "$i"; done

Next:

Here's how to use the command in a script instead (named "wma2mp3":

sudo nano /usr/local/bin/wma2mp3

copy and paste this into the file:

#!/bin/bash
for i in `find . -type f -name '*.wma'`; do avconv -i "$i" "${i/.wma/.mp3}"; done

Press ctrl + o and then press enter to save the file and use ctrl + x to exit.

Then, make the file executable with the following command:

sudo chmod +x /usr/local/bin/wma2mp3

Now you can simply run the command wma2mp3 to recursively covert all wma files to mp3.

Just do the same with the script to erase all wma files and name it whatever you want.


BTW, if you just want to convert all the files in the current directory only (not throughout all other subsequent recursive directories), you can use this command instead:

for i in *.wma; do avconv -i "$i" "${i/.wma/.mp3}"; done

and of course, to remove the wma files in the current directory only, just use this command:

rm *.wma
Share:
35,292

Related videos on Youtube

CaRoXo
Author by

CaRoXo

Updated on September 18, 2022

Comments

  • CaRoXo
    CaRoXo over 1 year

    I tried different options from forums, but most of them are out of date (ffmpeg tells me about it, like with -sameq option). Im trying to understand the docs, but I can't.

    I want to know how to convert recursively with ffmpeg from wma to mp3 with a for example max bitrate of 192kbps in output (but not 192 this if the original was 128kbps)

    I have unless 14K wma files spread in many directories. So, I don't want to move them. Just convert them keeping filename and metadata, and delete them, if it would be possible to read the list of the files to convert from a txt file that I can create, one file per line. With this, I would manage to make the recursive search, an paste into it.

    Thanks for any help. And still more thanks for any explanation about Ffmpeg.

    Caroxo

    PS: Soundconverter used to be good, but slow. Now neither one or the other. Doesn't work in 14.04 in many cases, like mine. I'm using Soundkonverter but is very slow. So, all this for preventing this recommendations. And I want to learn to use this powerful ffmpeg! and the CLI

    NOTE: The script here below, was working in the first convertions. But for any reason that I cant explain, suddenly deleted the wma's in conversion, without leaving the mp3. So, I changed again to "unsolved" (to prevent occasionally problem to someone's else). The problem seems coming from avconv " Application provided invalid, non monotonically increasing dts to muxer in stream 0: 23606 >= 21720"(there are pastebins in the comments if there is someone interested in developing this bug). So, no avconv in the future.

    • mchid
      mchid over 9 years
      no need to use a text file just use a wildcard like *.wma to convert all the files in the directory recursively.
  • CaRoXo
    CaRoXo almost 10 years
    Thanks @TheSchwa. Seems like working. Just have two questions. 1- There is an error issue, that as goolge says, seems another 14.04 spread issue. It says "[mp3 @ 0x8c58000] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 23606 >= 21720" .... But the conversion seems to be ok anyway. I listen unless one complete song, and it listen ok (maybe I should try with some other less noisy, for better apreciating). and 2nd- Why converted files become larger than original? If original wma is 4.3M, the same track in mp3 is 6.4M . Is there any way of making this better?
  • CaRoXo
    CaRoXo almost 10 years
    I just see that the original wma was in 128kbps. So maybe is there the explanation of why is bigger. This gives me another question. Its possible to set a maximun bitrate, but not forcing to it? Like, if its 128, keep 128, but if it is higher than 192, just make 192kbps.?
  • TheSchwa
    TheSchwa almost 10 years
    I modified the conv-script to keep the same bit rate in the mp3 as the wma had. Again, I would test it on a few files first to make sure it works on your system, but I think this should do what you want it to.
  • CaRoXo
    CaRoXo almost 10 years
    Thanks for the upgrading. But, no, it doesn't work, shows an error: pastebin.com/BekpwWyE . I have a huge work in front, so any help is a huge help. Huge thanks, so.... :)...... Also: Just a detail. In your upgrade there is an error with the text file, you put the flac version in the script.
  • TheSchwa
    TheSchwa over 9 years
    Oh whoops, had it as flac for when I was testing it on my files. One more update, hopefully fixes the problem. I was making an assumption in the code that the bit rate in the wma files was a normal number like 128000.000000 or 192000.000000. Now it should work correctly for the files that caused the error, which had bit rates of 128016.000000.
  • CaRoXo
    CaRoXo over 9 years
    Its me again. I don't want to abuse, so just tell me if i should open a new topic to ask this. Some inexplicable thing had happened. And most of files in the current conversion (using the new script version) get erased. For reason of space in the comment, the explanation of what happened is also in the pastebin here pastebin.com/w5weqEws
  • TheSchwa
    TheSchwa over 9 years
    From what I've found from Google, this is actually a bug in the avconv package. Some users had success installing ffmpeg from source as in here(trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu) and using that instead of avconv. It might not be a bad idea to ask a new question specifically about Application provided invalid, non monotonically increasing dts to muxer in stream 0.
  • CaRoXo
    CaRoXo over 9 years
    This of "error .. monotonically..." I also googled (see first comment) and seems mostly a problem in 14.04. But this error appears with the ones converted succesfully as the ones that dissapears. Reproducing this, it happens still and i cant find the difference between the ones that dissapears and the ones not. But for example with mp3tag (by wine) they seem to have no tag inside. After converting them to mp3 with soundKonverter, they are ok. Anyway, when I started the topic I was asking specifically about ffmpeg that i installed from source yesterday, and was because why I started this tread.
  • TheSchwa
    TheSchwa over 9 years
    Sorry, I completely glossed over that in your question. Oops :( If you just replace avconv with ffmpeg in the script it might solve the problem (i updated the answer again). If it doesn't solve it, you should either open a new question on askubuntu specifically about the conversion problem or post on the ffmpeg forums.
  • CaRoXo
    CaRoXo over 9 years
    Man, now goes faster. It deletes the files even faster than before! and without converting even one. So, as I suggest you before. The problem of this error/bug of avconv, was not the cause of the erasing. There is something in the script that makes the deletion. Is saying as before "/home/txk/Musik/TmpTest/Django Reinhardt (-fr) Un geant sur son nuage (compilation)[Jazz]/14 - Black and White.mp3: No such file or directory" Recursively, and erasing everything.I still dont understand why in some cases before worked, and in others not.But the answer is in the script pastebin.com/x1zteiF0
  • CaRoXo
    CaRoXo over 9 years
    Sorry, I lost the captcha part in pastebin. I just realised. Here re pasted. pastebin.com/3QkaPzvW All this files got erased en one second or less..
  • TheSchwa
    TheSchwa over 9 years
    Are you quite sure you installed ffmpeg correctly? As well as the additional libraries? The first few error messages say Unknown encoder 'libmp3lame' which is the library used to encode mp3 files. I'm looking at the later ones now...
  • TheSchwa
    TheSchwa over 9 years
    I don't have an explanation for the later errors, and it's rather hard to debug this since I don't get these errors on my system. I recommend you either stop using the script and wait for an answer from someone else, or if you're willing to try fixing it yourself with guidance from other people post on Stack Overflow. This will be my last comment. Sorry I couldn't get this to work for you :(
  • CaRoXo
    CaRoXo over 9 years
    Ok, done. Here the post stackoverflow.com/questions/25227523/… thanks for the help anyway. I didn't think could be so hard to make something that seems so common. But I couldn't find the way still with the docs in ffmpeg.
  • ChrisV
    ChrisV over 8 years
    Note ffmpeg is reinstated in 15.04
  • teradyl
    teradyl almost 7 years
    worked like a charm. did a brew install mplayer and ran this script. After it was done I did a rm *.wma if you want to remove the old wma files
  • Joseph238
    Joseph238 over 5 years
    On Windows, you need to add an option to write ID3v2.3 headers for the metadata to convert properly. See superuser.com/a/453133/529848