Menu
Text Links
Search
Calendar
Solution: Converting flac to mp3
rechosen | 10 April, 2007 14:40
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:
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 - YOURLOCATION/"${file%.flac}.mp3"); done
The above line would output the mp3's in the /tmp directory.[rechosen@localhost ~]$ for file in *.flac; do $(flac -cd "$file" | lame -h - /tmp/"${file%.flac}.mp3"); done
Comments
Delay
Ustun Ozgur | 07/05/2007, 23:01
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.
Re: Delay
Rechosen | 11/05/2007, 14:20
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!
Work for me here
noom | 13/06/2007, 09:47
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
Handle multipe flac files
knura | 16/08/2007, 14:27
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
Re: Handle multipe flac files
jerome moinet | 08/09/2007, 21:04
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 ?
Re: Handle multipe flac files
jerome moinet | 08/09/2007, 23:53
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
I created something similar
Nick Sklav | 19/09/2007, 18:34
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.
www.sklav.com
flac attack
knifemonkey | 30/09/2007, 23:16
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.
Re: Solution: Converting flac to mp3
animus | 08/12/2007, 18:50
Thanks for your trick very useful.
The perfect command-line
erroneus | 27/01/2008, 06:32
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?
Re: The perfect command-line
miceagol | 24/03/2008, 21:28
+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.
RE: The perfect command-line
Nick Sklav | 28/03/2008, 16:32
erroneus i fail to understand what you are trying to say exactly? Care to go in a little more detail?
Re: Solution: Converting flac to mp3
the_dude_abides | 14/04/2008, 22:54
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.
make your own loop
naysayer | 15/04/2008, 16:22
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.
Re: Solution: Converting flac to mp3
Darren Yeats | 02/05/2008, 16:18
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.
Re: Solution: Converting flac to mp3
Darren Yeats | 02/05/2008, 17:51
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
mass conversion
DynV | 28/05/2008, 20:35
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.
YAFLAC2MP3: The definitive script :)
Octavio Ruiz | 12/06/2008, 20:27
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.
Add comment
Recently...
- Solution: Converting flac to mp3 advanced (supports drag 'n drop)
- Tutorial: Conditions in bash scripting (if statements)
- Linux Tutorial Blog is no longer frozen!
- Solution: Getting a Gravis GamePad Pro to work on Linux
- Linuxtutorialblog.com is now officially frozen!
- Solution: Preventing damage after a system lockup
- Solution: Creating an mpeg with mencoder that plays on Windows Media Player
- Cropping multiple images the same way (short tutorial)
- Solution: Converting flac to mp3
- Tutorial: Disabling unused daemons to speed up your boot sequence
Trackbacks (0)
Get tags
Fredric | 07/05/2007, 01:09
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.