Linux Tutorial Blog

Your regular Linux tutorial source!

Solution: Converting flac to mp3 advanced (supports drag 'n drop)

rechosen | 25 August, 2008 21:30

My short solution to convert flac to mp3 caught a lot of attention, and uncovered a demand for a more sophisticated solution. Therefore, after all the scripts posted in the comments, I decided to go on and write an extensive script that should be able to offer all the requested functionality (and more). This post contains a script and installation instructions to allow you to convert both individual files and directories, keep the tags of the files fully intact and enjoy an easy-to-use drag 'n drop interface.
 

The script

First of all, you can get the script here: flac2mp3. Its features are:

  • Converts both individual files and directories
  • Keeps the tags
  • Full support for weird characters in filenames (they just need to end in .flac, case insensitive)
  • Drag 'n drop support (tested in Gnome and KDE)
  • Progress and estimated time left indication

The script requires a recent version of bash and the flac, metaflac and lame commands. 

Installation

The installation is pretty straightforward. After downloading the script (see the link in the previous section), make sure it's in a standard logical folder (I recommend ~/bin, though others may like to put the script in /usr/bin or /usr/local/bin to make it available to all users) and that you have the execute permission to it. If not, you can use your favourite file manager to give yourself that permission or use a command, like:

[rechosen@localhost ~]$ chmod u+x ~/bin/flac2mp3

Note: if you placed the script in /usr/bin or /usr/local/bin, you should make it executable for everyone, like:

[rechosen@localhost ~]$ sudo chmod +x /usr/bin/flac2mp3

Using drag 'n drop

I prepared .desktop files for both KDE and Gnome. You can download flac2mp3kde.desktop and flac2mp3gnome.desktop, respectively. After saving it to your desktop (usually in the folder ~/Desktop), you may want/need to alter a few things of the shortcut (right click on it and edit its properties):

  • When using Gnome, you'll have to change the username in "/home/USERNAME/bin/flac2mp3" to your own username (provided that you've placed the script there, otherwise you should either specify the full path or omit the path entirely and just use the script's name).
  • You can choose the bitrate of the converted mp3 files by changing the value after the -b option. If you want to use VBR, you can specify custom options for lame using the -l option (see Usage for instructions).
  • You can choose the amount of source directories to retain by altering the value after the -d option. This sets the amount of directories of the source path used for placing the target mp3. It works from the right to the left. Example:
    /home/rechosen/music/After Edmund/Hello/01_Thank_God.flac
    will be converted to the following directory is -d is set to 2:
    <outputdir>/After Edmund/Hello/01_Thank_God.mp3
    And if -d is set to 1:
    <outputdir>/Hello/01_Thank_God.mp3
  • You can choose the output directory by replacing the path after the -o option. If you omit the option entirely, convert files will be placed in the same directory as the source files (e.g. /home/rechosen/music/After Edmund/Hello/01_Thank_God.flac is converted to /home/rechosen/music/After Edmund/Hello/01_Thank_God.mp3). If you do specify a path, don't forget specifying the amount of source directories to retain using the -d option (see the previous bullet).

You should leave the -r option as it is, so both files and directories will be supported. If you placed the script in an other place than ~/bin, you should (of course) change the script location at the start of the command. After you've modified the desktop shortcut to your likings, you may drag and drop files and/or directories into it. Just select some files or directories in your file manager, click and drag them to your desktop and onto the shortcut. The script will try to convert any file ending in .flac (case insensitive) to an mp3. Be aware that you can safely move the shortcut to your panel, so it'll always be easily reachable.

Usage

When running the script with the option --help, you will see the following:

Usage: flac2mp3 [-b bitrate] [-d dirstoretain] [-l lameopts]
[-o outputdirectory] [-r] FILES
This script uses the tools flac, metaflac and lame to convert flac files to
mp3s. Accepts both individual files and directories (the latter when using the
-r option).

Explanation of the options:
    -b bitrate          Specifies the bitrate of the converted mp3. Defaults to
                        128. Specify a bitrate of "none" to stop the -b option
                        from being passed to lame.
    -d dirstoretain     Specifies how many directory names to retain when using
                        both -r and -o. Works from right to left. See the -r
                        option for an example.
    -l lameopts         Allows to give custom options to lame. Make sure to
                        surround them with quotes (e.g. -l "-V 6")!
    -o outputdirectory  Specifies the directory to write the mp3s to. If not
                        specified, the mp3s will be written to the directory
                        where the flac files are located.
    -r                  Makes the script look for flac files recursively. If
                        this option is specified together with an alternative
                        output directory, the mp3s will be written to the
                        specified output directory plus the current relative
                        path of the flac file. Example:
                        "After Edmund/Hello/01_Thank_God.flac"
                        will be converted to
                        "<outputdirectory>/After Edmund/Hello/01_Thank_God.mp3."

                        ATTENTION: When using full paths (like
                        /home/user/music/After Edmund/Hello/01_Thank_God.flac),
                        you can determine how many directories the script will
                        retain using the -d option. With -d 2, the full path
                        file just above will be converted to:
                        "<outputdirectory>/After Edmund/Hello/01_Thank_God.mp3."

    -s                  Do not use this option unless you know what you are
                        doing! It is used internally to self-execute the
                        script for conversion.
    -v                  Be verbose; makes the script tell what it is doing.

For more information see http://www.linuxtutorialblog.com/.
No warranty included with this script ;-)                              Rechosen

That should explain most of it already, though there are some other things useful to know:

  • To convert to a VBR mp3, use (for example) -b none -l "-V 6".
  • To be able to run the script directly (without explicit path) if you placed it in ~/bin, add the following line to your ~/.bashrc:

    export PATH="$PATH:~/bin"

  • The script nullifies any genre tags lame does not provide to prevent errors. It is currently not possible to specify an alternate genre in this case, though I may add that by request. For now, please make sure the flacs' genres are valid lame genres.
  • Related to the former statement, the script truncates the comment tag to 30 characters to prevent lame from throwing an error on comment length. Request if you'd wish to see an option.
Have fun using the script and feel free to comment if you spot anything that could use an update (or if you don't understand something)!

Comments

?

Michael | 07/09/2008, 13:26

Hi,

I run sh flac2mp3 --help and get back:

flac2mp3: 55: [[:not found
flac2mp3: 64: Syntax error: "{" unexpected

Using bash v. 3.2.25, ubuntu 7.10

Probably something I'm doing wrong but not sure what!

flac2ogg?

Robert Delahunt | 15/09/2008, 18:26

I appreciate your work with flac2mp3. I use it in XFCE's Thunar as a right-click custom command to convert FLAC files to MP3, and it works very well.

I was wondering, can you also produce a flac2ogg.sh? I hate to ask, but I didn't want to start hacking away at your flac2mp3.sh, due to its scripting being a bit over my head.

Still, I will test it for you! :)

aha!

Michael | 17/09/2008, 20:48

I made the classic blunder (no, not "never get involved in a land war in Asia"). I made the mistake of equating bash with sh.

ubuntuforums.org/showthread.php?t=499045

Someone had this problem before me, it seems.

Re: flac2ogg?

Rechosen | 19/09/2008, 13:42

Hmmm... Making a derivative flac2ogg script shouldn't be hard, but it'd have to be done well. If there is more interest in it (say, two more people comment that they support the idea), I will look into it.

@Michael: you're not the only one, I made it myself a while ago, too!

ignore / overwrite existing?

Michael | 09/10/2008, 21:24

Huge thanks for flac2mp3. Any plans to ignore existing files (in the target dir) and add the option to overwrite (or vice versa)?

flac2ogg?

gagrilli | 16/10/2008, 11:08

Very useful script. Been using it for about a week as a command line tool and as servicemenu in KDE. I would also welcome an alternative version to .ogg files. Thanks anyway. (very informative comments, those of us not familiar with scripting "beg" for these to understand what the bash code can do)

うつ病

うつ病 | 23/10/2008, 19:30

あるがままのうつ病の姿をたくさんの人に読んでうつ病がほんとにうつ病どんなものを知って欲しくて最近下がってきてちょっと。

Filenames with spaces

Creap | 28/10/2008, 22:30

Drag 'n drop doesn't work for me. I have "flac2mp3 -l "-V 2" -d 1 -o /media/warez/flac2mp3 -r %U" in my shortcut command, but when I drag a flac file or folder to it, a terminal windows (I guess) just pops up for 1 second and then closes. Running the exakt same command in a terminal but specifying the target folder works fine.

However, as long as neither the directory nor the file has any spaces in their name, it works. How can I solve this?

Thanks.

eyeD3

jt | 02/11/2008, 03:40

What a sophisticated script!
Since I have all of my music library in flac, this is what I was wanted for my mp3 player for a long time. I'm very grateful to you for your great script.

Few of you here are interested in, but let me mention one thing as a Japanese.
This script uses lame's options (--tt, --ta etc.) to put tags to the generating mp3s, but unfortunately this garbles Japanese characters in mess. (I don't know for other non-latin languages.)
It went everything fine when I changed the script to use eyeD3, putting the following at the end of the script.
I hope this might be of use for some people.

# Add the tags to outputfile in id3v2.3/UTF-16LE
[ "$verbose" ] && echo "Adding tags to \$outputfile..."
i=0
for option in {"t","a","A","n","Y","G","c"}; do
if [ "${info[$((i/2))]}" ]; then
command[$i]=-$option
command[$((i+1))]="${info[$((i/2))]}"
fi
i=$(($i+2))
done
eyeD3 -2 --to-v2.3 --set-encoding=utf16-LE "${command[@]}" "$outputfile"

It works (..) - flac2ogg Please

scribe63 | 03/11/2008, 20:47

Finally got the drag n drop to work in kde on etch.
It wasn't working from $HOME/bin/flac2mp3/flac2mp3 or ~/bin/flas3mp3/flac2mp3

Konsole would just pop up and disappear or i kept getting the below error when run the script from a script calling it the command line

/home/USERNAME/bin/flac2mp3/flac2mp3: /bin/bash: bad interpreter: Permission denied

So i copied the flac2mp3 script to /usr/local/bin and it works with the following in the .desktop properties

flac2mp3 -b 128 -d 2 -l --cbr -o /home/USERNAME/MultiMedia/Mp3s/lastfm -r %U

Thanks 4 the script, It works (..) - flac2ogg Please

scribe63 | 03/11/2008, 20:53

Finally got the drag n drop to work in kde on etch.
It wasn't working from $HOME/bin/flac2mp3/flac2mp3 or ~/bin/flas3mp3/flac2mp3

Konsole would just pop up and disappear or i kept getting the below error when run the script from a script calling it the command line

/home/USERNAME/bin/flac2mp3/flac2mp3: /bin/bash: bad interpreter: Permission denied

So i copied the flac2mp3 script to /usr/local/bin and it works with the following in the .desktop properties

flac2mp3 -b 128 -d 2 -l --cbr -o /home/USERNAME/MultiMedia/Mp3s/lastfm -r %U

grep -a

jt | 04/11/2008, 21:45

I thought all was fine last time, but I found a problem when transcoding the files located in the directories named in Japanese (UTF-8).
flac2mp3 stops at the 'grep' part and says:
"stat: cannot stat `Binary file (standard input) matches\n': No such file or directory"
By trial and error, adding '-a' (or '--binary-files=text') to the grep options solved the problem.

added a switch -n to only translate convert newer/non-existing files

Mavo | 13/11/2008, 23:05

It's a very very nice script. Thanks a lot.
I used the recursive behavior. But I subsequently will add new flac files to
my collections and so I thought of adding a -n option to the script that skips
converting to the corresponding mp3 file ($file to $outputfile)
if the the mp3 is newer than the flac file or the mp3 file does not exist yet.

I just don't know where/how to submit the patch ..

Add comment

(optional, will not be published)
(optional)

<><