Python script: convert wma to mp3 files with ffmpeg (avconv)

While cleaning up my music library I came across some WMA files that I would like to convert to MP3 format. The whole thing should run as far as possible automatically in the background on my Synology NAS and require as little interaction as possible. For such a project you don’t necessarily need a paid software, because the whole thing can also be realized with ffmpeg. ffmpeg is provided by Synology directly with their NAS devices via the DSM operating system. But also on the Raspberry Pi the absence in the standard installation is no problem. Here ffmpeg can be installed without further ado:

$ sudo apt-get install ffmpeg

Actually you don’t need a script for it, but it simplifies the handling and allows to convert a lot of files with only one command.

The syntax for ffmpeg is:

ffmpeg -i<source file> -codec:a<codec> -qscale:a<quality> <target file>

In our case:

ffmpeg -i input.wma -codec:a libmp3lame -qscale:a 2 output.mp3

Don’t be surprised if you get a message that the ffmpeg command can only be invoked for compatibility reasons. The new command is called avconv , works the same, but is not yet available on my Synology NAS.

ffmpeg: the new command to convert is called'avconv'

ffmpeg: the new command for converting is called "avconv

If at some point ffmpeg cannot be called anymore, just use avconv instead on line 30 of the script.

The script

Since Python is my preferred programming language, I quickly built a script with it. This searches all files in the current directory for the file extension .wma and convert them to MP3 files by calling ffmpeg accordingly. The new files then have the same name as the old ones, but with a different extension.


Just copy the script into a text editor, as and make it executable:

$ chmod +x

Afterwards the file into the directory with .copy wma files and execute them with the following command:

Functionality in detail

line 11 – Set working directory: In the directory, which is specified here, the script looks for the files, which are to be converted. ./ stands for the current directory.

Line 14 – Source material: Here you define which files are to be converted. In my case these are WMA files.

Line 19 – Read file structure in working directory

line 21 – Loop over all files in the working directory

Line 24 – The name of the currently processed file is split at the places containing a dot. This is how several strings are created. The last one is compared with the content of the variable srctype (i.e. wma )in. So if it is a file with the extension wma, the following commands are processed, otherwise the loop goes to the next file.

Line 27 – Create new file name: Again the whole file name is taken and the extension incl. dot ( .wma ) separated. Afterwards .mp3 attached.

line 30 – The actual conversion order is set off. Where os means.popen(. , that an external program (in this case ffmpeg) is invoked. In order for ffmpeg to work properly, some more arguments have to be specified. -i stands for the source file, followed by the file name of the source file, which consists of directory and the actual file name. -codec:a libmp3lame is the codec to be used and basically the information into which format it should be converted, namely mp3 . Then follows -qscale:a 2 for the bitrate of the target file. The script also contains a table at the very bottom, which information leads to which results. Then follows the target file, again consisting of the target directory + the calculated file name. 1&> /dev/null means that all output that you would otherwise see on the command line should be "hidden". There are quite a few lines per file.

Line 32 – Output, which file was converted (appears then in the terminal)

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: