Solution: Converting flac to mp3
Sometimes you need to convert a load of flac files to mp3's, for example when wanting to listen them on your mp3 player. This solution contains a single line of bash that'll convert all flac files in the current directory to mp3's, keeping the flac files.
Attention: this post now has an advanced counterpart providing a nice script. See Solution: Converting flac to mp3 advanced (supports drag 'n drop).
Note that you will need flac and lame for this to work. Run the following line in the directory where the flac files are:
[rechosen@localhost ~]$ for file in *.flac; do $(flac -cd "$file" | lame -h - "${file%.flac}.mp3"); done
This will output the mp3 files in the same directory as the flac files. If any of the mp3 files that's being created already exists, it will be overwritten. When the conversion has finished, you can copy the mp3 files to an other location like this:
[rechosen@localhost ~]$ cp *.mp3 /media/sda1
Note that this will copy all mp3 files in the current directory, not just the ones converted from the flac files! Of course, you could replace "/media/sda1" with any other directory. You could also replace "cp" by "mv" to move the mp3 files instead of copying them.
You can also make lame create the mp3 files somewhere else. Take the following line:
[rechosen@localhost ~]$ for file in *.flac; do $(flac -cd "$file" | lame -h - YOURLOCATION/"${file%.flac}.mp3"); done
And replace YOURLOCATION with the location you want the mp3 files to be created in. For example:
[rechosen@localhost ~]$ for file in *.flac; do $(flac -cd "$file" | lame -h - /tmp/"${file%.flac}.mp3"); done
The above line would output the mp3's in the /tmp directory.


Topic: Get tags
The above solution is simple but does not copy the tags from the flac file to the mp3 file. This require a more complicated solution and here is a script that I use:
--------------------------------------------------
#!/bin/bash
FLAC=$1
MP3="${FLAC%.flac}.mp3"
[ -r "$FLAC" ] || { echo can not read file \"$FLAC\" >&1 ; exit 1 ; } ;
metaflac --export-tags-to=- "$FLAC" | sed 's/=\(.*\)/="\1"/' >tmp.tmp
cat tmp.tmp
. ./tmp.tmp
rm tmp.tmp
flac -dc "$FLAC" | lame -b 192 -h --tt "$Title" \
--tn "$Tracknumber" \
--tg "$Genre" \
--ty "$Date" \
--ta "$Artist" \
--tl "$Album" \
--add-id3v2 \
- "$MP3"
-------------- end of script ----------
The script conver one flac file to an mp3 file and it take the .flac file as a parameter. Do not forget to quote the file if it contains spaces.
Topic: Delay
Your blog is great, but unfortunately you don't seem to find much time writing. I hope you can; because I check every once in a while. Keep up the good stuff.
Topic: Re: Delay
Thanks, Ustun! Indeed, I find it hard to keep up the writing, there are so many things I have to do, and, on top of that, loads of things I love to do besides blogwriting. I will do my best and see if I can write something the next few days, because, after that, my examinations will start and I won't have that much spare time. Anyway, thanks for the comment!
Topic: Work for me here
Thanks Fredric,
But for me (I using Slackware) I can't get the right tag (No tag go in new mp3 file)
Here my solutions
#!/bin/bash
FLAC=$1
MP3="${FLAC%.flac}.mp3"
[ -r "$FLAC" ] || { echo can not read file \"$FLAC\" >&1 ; exit 1 ; } ;
metaflac --export-tags-to=- "$FLAC" | sed 's/=\(.*\)/="\1"/' >tmp.tmp
cat tmp.tmp
. ./tmp.tmp
rm tmp.tmp
flac -dc "$FLAC" | lame --tt "$TITLE" \
--tn "$TrackNumber" \
--tg "$GENRE" \
--ty "$Date" \
--tc "$COMMENT" \
--ta "$ARTIST" \
--tl "$ALBUM" \
--add-id3v2 \
- "$MP3"
You can notice the tag option is case sensitive for my system ,If anyone have idea about command to ignore case sensitive will be great
Topic: Handle multipe flac files
Mucho thanks for the script. The ID tags in my case are in CAPS (openSUSE 102.).
Here is a slightly modified version to handle multiple FLAC files and write the MP3 files into $PWD/mp3
#!/bin/bash
OUT_DIR="./mp3"
[ ! -d ${OUT_DIR} ] && mkdir -p ${OUT_DIR}
# modify the lame options to your
# preference
lame_opts=" --vbr-new -V 2 -B 256 "
for x in "${@}"
do
FLAC=${x}
MP3="${FLAC%.flac}.mp3"
[ -r "$FLAC" ] || { echo can not read file \"$FLAC\" >&1 ; exit 1 ; } ;
metaflac --export-tags-to=- "$FLAC" | sed 's/=\(.*\)/="\1"/' > tmp.tmp
. ./tmp.tmp
rm tmp.tmp
echo "Converting ${FLAC} to MP3 format"
flac -c -d "$FLAC" | lame ${lame_opts} \
--tt "$TITLE" \
--tn "$TRACKNUMBER" \
--tg "$GENRE" \
--ty "$DATE" \
--tc "$COMMENT" \
--ta "$ARTIST" \
--tl "$ALBUM" \
--add-id3v2 \
- ${OUT_DIR}/"$MP3"
done
Topic: Re: Handle multipe flac files
Thanks for the multiple script.
I had two problems :
1) tags variables case where once upper and once lower with metaflac (in debian/testing)
2) I wanted to be able to run the script upon a directory structure (ie. $MP3 would be "artist/album/track") and store all the files on one level into the "mp3" directory, then use another script to store them the right way
Here is a modified script that does that :
#!/bin/bash
# from http://www.linuxtutorialblog.com/post/solution-converting-flac-to-mp3
OUT_DIR="./mp3"
[ ! -d ${OUT_DIR} ] && mkdir -p ${OUT_DIR}
# modify the lame options to your
# preference
lame_opts=" --vbr-new -V 2 -B 256 "
for x in "${@}"
do
FLAC=${x}
MP3=`basename "${FLAC%.flac}.mp3"`
[ -r "$FLAC" ] || { echo can not read file \"$FLAC\" >&1 ; exit 1 ; } ;
metaflac --export-tags-to=- "$FLAC" | sed 's/=\(.*\)/="\1"/' > tmp.tmp
. ./tmp.tmp
rm tmp.tmp
[ -z $TITLE ] && TITLE="$Title"
[ -z $TRACKNUMBER ] && TRACKNUMBER="$Tracknumber"
[ -z $GENRE ] && GENRE="$Genre"
[ -z $DATE ] && DATE="$Date"
[ -z $COMMENT ] && COMMENT="$Comment"
[ -z $ARTIST ] && ARTIST="$Artist"
[ -z $ALBUM ] && ALBUM="$Album"
echo "Converting ${FLAC} to MP3 format"
flac -c -d "$FLAC" | lame ${lame_opts} \
--tt "$TITLE" \
--tn "$TRACKNUMBER" \
--tg "$GENRE" \
--ty "$DATE" \
--tc "$COMMENT" \
--ta "$ARTIST" \
--tl "$ALBUM" \
--add-id3v2 \
- ${OUT_DIR}/"$MP3"
done
I have one last problem : I would also like the id3v1 tags to be written. Any idea ?
Topic: Re: Handle multipe flac files
I noticed there was a bug in my submission, and also that lame could not handle unknown genres (World, for example). Here is a new submission resolving that bug and using id3v2 instead of lame to tag the resulting mp3 file.
#!/bin/bash
# from http://www.linuxtutorialblog.com/post/solution-converting-flac-to-mp3
OUT_DIR="./mp3"
[ ! -d ${OUT_DIR} ] && mkdir -p ${OUT_DIR}
# modify the lame options to your
# preference
lame_opts=" --vbr-new -V 2 -B 256 "
for x in "${@}"
do
FLAC=${x}
MP3=`basename "${FLAC%.flac}.mp3"`
[ -r "$FLAC" ] || { echo can not read file \"$FLAC\" >&1 ; exit 1 ; } ;
TITLE=""
TRACKNUMBER=""
GENRE=""
DATE=""
COMMENT=""
ARTIST=""
ALBUM=""
Title=""
Tracknumber=""
Genre=""
Date=""
Comment=""
Artist=""
Album=""
metaflac --export-tags-to=- "$FLAC" | sed 's/=\(.*\)/="\1"/' > tmp.tmp
. ./tmp.tmp
rm tmp.tmp
[ -z "$TITLE" ] && TITLE="$Title"
[ -z "$TRACKNUMBER" ] && TRACKNUMBER="$Tracknumber"
[ -z "$GENRE" ] && GENRE="$Genre"
[ -z "$DATE" ] && DATE="$Date"
[ -z "$COMMENT" ] && COMMENT="$Comment"
[ -z "$ARTIST" ] && ARTIST="$Artist"
[ -z "$ALBUM" ] && ALBUM="$Album"
echo "Converting ${FLAC} to MP3 format"
flac -c -d "$FLAC" | lame ${lame_opts} - ${OUT_DIR}/"$MP3"
id3v2 \
-a "$ARTIST" \
-A "$ALBUM" \
-t "$TITLE" \
-c "$COMMENT" \
-g "$GENRE" \
-y "$DATE" \
-T "$TRACKNUMBER" \
${OUT_DIR}/"$MP3"
done
Topic: I created something similar
Well guys after alot of reading and going threw some of these documents i always seemed to encounter an error of some kind. i created a script which is located in an article on my website hope it helps the rest of you.
Flac to Mp3 Simplified.
http://www.sklav.com
Topic: flac attack
I got lame and used the usall method of running a console tool and wen lame --help, which simply said what to do, so I did what it told me to do, went to my resulting mp3 and opened it up. The resulting screaming roar of noise may have caused permanent damage to my ear drums. Thanks for the warning lame inc. you bastards.
Topic:
Thanks for your trick very useful.
Topic: The perfect command-line
I love that people can't resist the urge to "one-up" your KISS command line solution to a basic and common need.
What I found even more amazing is how the offered shell scripts fail to take into account the function being performed; specifically, converting an ENTIRE folder full of flac files to mp3. If these helpful show-offs wanted to be relevant, they'd have accounted for that purpose and written a loop that includes reading all the filenames and loops to process them ALL?
Topic: Re: The perfect command-line
+1
I'm looking for a script that automatically syncs a folder tree of flacs to another folder tree with mp3s. Seems like I must try making one myself, but I'm not trained with bash or python scripting.
Topic: RE: The perfect command-line
erroneus i fail to understand what you are trying to say exactly? Care to go in a little more detail?
Topic:
Is the no GUI tool for *nix to achieve this? This is no attempt to flame or anything, and I am glad this solution worked for me.
Topic: make your own loop
for i in *flac; do flacscript $i; done
It's not that hard. It's also not one-upmanship since the original script does neglect any mp3 tags, so if you care about that stuff you would have to go and add onto the original script yourself. Some people try to be helpful and post their ways of doing it and instead of taking what they've done and adding in your own modification you just tell them they're doing it wrong. Thanks, guy.
And if you have a huge tree of directories to convert, then something like
find . -iname "*flac" -exec flacscript {} \;
should work for you...haven't tested it though since the only files I'm worried about are in the same directory.
Topic:
Check out mp3fs which is relatively simple and works well. All your flacs appear as mp3 in a virtual filesystem. Just copy out what you need and tags are retained.
Topic:
And, assuming Debian Etch running Gnome...
If you have a list of files you want to convert in say a playlist in Rhythmbox...you can do it the really easy way with soundconverter.
1. apt-get install soundconverter
2. Go to debian-multimedia.org and follow instructions:
2.1. Download http://www.debian-multimedia.org/pool/main/d/debian-multimedia-keyring/debian-multimedia-keyring_2007.02.14_all.deb
2.2. # dpkg -i debian-multimedia-keyring_2007.02.14_all.deb
2.3. Edit /etc/apt/sources.list to include deb http://www.debian-multimedia.org etch main
3. # apt-get install gstreamer0.10-lame
4. Run soundconverter and drag and drop files to convert etc...
Regards, Darren
Topic: mass conversion
I had to convert quite a few files from different directories and started by doing them one by one having to keep an eye on the progression so I'd change directory and apply the command again. I found a better way :
I made a new folder, which I called MP3, and a subdirectory in it, which I called tmp. I then went through the directories and applied the following 3 commands recursively :
1. "cp -l * /path/to/MP3/tmp" - putting everything in the same directory without having to move thing back and forth.
2. "touch /path/to/MP3/tmp/*" - date on the present date to separate the file name as I had a lot beginning with the same pattern.
3. "mv /path/to/MP3/tmp/* /path/to/MP3" - putting in the main directory otherwise the new date would keep being applied on all the files, undoing the separation.
I then applied the suggested conversion command in the /path/to/MP3 directory.
Topic: YAFLAC2MP3: The definitive script :)
Hey guys, you've got the idea, I've just improved those and coded into a really simple script which solves in an elegant bash'ism way the upper-case output from metaflac (running it just one time per file). It work recursively.
http://github.com/tacvbo/yaflac2mp3/tree/master/yaflac2mp3.sh
Please give me your ideas/patches and I would love to add them for you.
This is just for the bash lovers, I really liked the FUSE based mp3fs which I'm using right now.
Topic: Perl-based converter ...
Some years ago I created a similar script in perl that converted *.flac to *.mp3 recursively in all subdirectories below working directory(pwd). I also implemented a nice progress bar. Obviously, it could be solved quite simple with bash too.
Topic: another flac2mp3
for FNAME in *.flac
do
PREFIX=`basename "${FNAME}" .flac`
if [ ! -e fifo ]
then
mkfifo fifo.wav
fi
lame --vbr-new -b 160 fifo.wav "${PREFIX}".mp3 &
mplayer "${FNAME}" \
-vc null -vo null -benchmark \
-ao pcm:fast:file=fifo.wav
rm fifo.wav
done
Topic: Full blown script
Hi,
thank's for your script and the comments.
Based on them I've created a python script that's walks recursivly and supports more file types (wav, ogg, flac, mp3, mru):
see:
http://code.google.com/p/music-collection-sync/
Greg
Topic: Perl Audio Converter 4.0.5
For Debian based systems (I run Kubuntu Jaunty) I found a nice app that works like a charm.
sudo apt-get install pacpl
Description:
Perl Audio Converter is a tool for converting multiple audio types from one format to another. It supports AAC, AC3, AIFF, APE, AU, AVR, BONK, CAF, CDR, FAP, FLA, FLAC, IRCAM, LA, LPAC, M4A, MAT, MAT4, MAT5, MMF, MP2, MP3, MP4, MPC, MPP, NIST, OFR, OFS, OGG, PAC, PAF, PVF, RA, RAM, RAW, SD2, SF, SHN, SMP, SND, SPX, TTA, VOC, W64, WAV, WMA, and WV. It can also convert audio from the following video extensions: RM, RV, ASF, DivX, MPG, MKV, MPEG, AVI, MOV, OGM, QT, VCD, SVCD, M4V, NSV, NUV, PSP, SMK, VOB, FLV, and WMV. A CD ripping function with CDDB support, batch conversion, tag preservation for most supported formats, independent tag reading/writing, and extensions for Amarok, Dolphin, and Konqueror are also provided.
Topic: Flac to mp3
Thanks, this was very handy. I didn't have lame installed by default (using fc11) and wasn't aware that conversion was this easy. Cheers.
This is all fine and dandy, but with your blog hacking up what's a quote and what's a double dash none of these scripts will truly copy and paste as is and work.
Besides, with SoundConverter being available for most if not all distributions, it's an easy drag and drop to convert an entire directory with easy control.
None of the above scripts will properly handle tags - if your comments field is multiple lines, you actually risk executing arbitrary commands on your system by accident.
Well spotted, Tony. I fixed the blog hacking up the quotes and stuff, didn't realize the move to WordPress would have such implications! And I'll take a look at SoundConverter if I can spare the time somewhere.
[...] поиск нормальных скриптов или программ я использовал этот скрипт и немного его модифицировал, чтобы [...]