PDA

View Full Version : Convert Entire Music Library



durangowildlife
3rd January 2009, 08:18 PM
Objective: Convert 4,000 flac files to mp3 files.

The flac files are in Artist/Album directories, e.g., ~/Music/Beethoven/Symphony_7/foo.flac -- but I want the mp3 files in a separate directory, e.g., ~/Music-mp3/Beethoven/Symphony_7/bar.mp3

There are lots of scripts to convert individual files, but they would either write over the original flac file or leave the mp3 files in the same directory as the flac files. Does anyone have a script that will recreate my directory layout when converting the files?

I guess there are three ways to go about this -- I'm looking for help on the third:

1) Duplicate the 100 gig Music directory then convert the copy to mp3 files by writing over the flac files.

2) Convert the flac files to mp3 files then find all *.mp3 files and move them to the new Music-mp3 directory.

3) Read in a flac file and save it as an mp3 file in the appropriate directory in Music-mp3.

Obviously option #1 would be a huge waste, i.e., duplicating 100 gigs of flac files then converting to mp3. Options #2 and #3 are pretty much the same thing but done in a different order. It would be nice to use option #3 so that when I add flac files to the Music directory I can easily convert just those files to mp3.

brunson
3rd January 2009, 09:40 PM
for file in *.flac; do flac -cd "$file" | lame -h - /newlocation/"${file%.flac}.mp3" && rm "$file"; done

I'd copy a couple of flac files into a temp directory and try that out, I haven't tested it, just jotted it off the top of my head.

durangowildlife
3rd January 2009, 10:41 PM
Thanks! Yeah, good thing I tried it with test files -- that script deletes the original flac files after saving the converted mp3 files in the specified directory. My music library has hundreds of directories, so I think it has to be something like

find ~/Music/ -name *.flac -print and then execute the conversion programs.

What I can't figure out is how to specify where I want the mp3 files saved. I want to convert files in

~/Music/Beethoven/Symphony_7/foo.flac

and save them to

~/Music-MP3/Beethoven/Symphony_7/bar.mp3

I could use your script and find to read in and convert all the files in the various directories, but then I'd have to save them all in one location, which isn't ideal because the tags are lost once the files are converted -- another problem I just noticed, d'oh.

brunson
3rd January 2009, 10:49 PM
Oh, that's what I thought you wanted was to delete the original when you spoke of "write over the flac file".

You can get a bit fancier with the file name handling:


find ~/Music/ -name *.flac -print | while read file
do
filehead = $( echo $file | sed -d 's/.flac$//' )
blah blah $file | whatever $filehead.mp3
done

Once again, completely tested

durangowildlife
3rd January 2009, 11:28 PM
Got an error from sed

sed: invalid option -- 'd'

And couldn't see the output file because it was just .mp3 and was therefore invisible.

Using

find ~/Test/ -name *.flac -print | while read file
do
filehead = $( echo $file | sed -d 's/.flac$//' )
flac -cd "$file" | lame -h - ~/Test/flac-mp3/"${filehead%.flac}.mp3"
done

If I could preserve the tags I could probably save all the mp3 files in one location and let Rhythmbox do the rest, though I'd prefer not to do that. The goal is to have an mp3 library for my ipod and an identical flac library for my stereo.

RupertPupkin
3rd January 2009, 11:29 PM
I just tested this, it works:

#!/bin/bash
TOPDIR=~
IFS="$(echo -e "\n\r")"
for d in $(find ${TOPDIR}/Music -regex "^.*\.flac$"|sed 's/\/[^/]*\.flac$//'|sort|uniq)
do
echo -e "\nConverting files in ${d}:"
NEWDIR="${TOPDIR}/Music-mp3/${d#${TOPDIR}/Music/}"
if [ ! -d ${NEWDIR} ]; then
mkdir -p "${NEWDIR}"
fi
for f in ${d}/*.flac
do
echo -n "Converting ${f} ... "
flac="${f##*/}"
mp3="${flac%.flac}.mp3"
flac -cds "${f}" | lame --quiet -b 256 -q 2 - "${NEWDIR}/${mp3}"
echo "done."
done
done

durangowildlife
3rd January 2009, 11:31 PM
Thanks! Will give it a test -- super fast responses today.

durangowildlife
3rd January 2009, 11:50 PM
That works! The only thing is that it created the directories in ~/Music-mp3/home/alex/Music/...
And, also, the tags are still not preserved in the converted files.

brunson
3rd January 2009, 11:55 PM
Details left as an exercise for the student.

durangowildlife
4th January 2009, 12:08 AM
Ha, would be a good exercise, but I just installed soundconverter with yum and it preserves the tags.

brunson
4th January 2009, 12:10 AM
Even better. :-)

durangowildlife
4th January 2009, 12:50 AM
Eh, not so much. Still have to think about how I'll keep the two directories in sync when I rip more CDs, and for that I think I'll have to use Mr. Pupkin's nifty script -- but I'll find flac files created after date X.

RupertPupkin
4th January 2009, 07:07 AM
To convert only those flac files that haven't already been converted to mp3, all you need is an if statement to check if the mp3 exists. I also changed it to say "Checking files in ..." instead of "Converting files in ...". Nothing will be done on a flac file unless the corresponding mp3 file does not already exist. The changes are in bold:

#!/bin/bash
TOPDIR=~
IFS="$(echo -e "\n\r")"
for d in $(find ${TOPDIR}/Music -regex "^.*\.flac$"|sed 's/\/[^/]*\.flac$//'|sort|uniq)
do
echo -e "\nChecking files in ${d}:"
NEWDIR="${TOPDIR}/Music-mp3/${d#${TOPDIR}/Music/}"
if [ ! -d ${NEWDIR} ]; then
mkdir -p "${NEWDIR}"
fi
for f in ${d}/*.flac
do
flac="${f##*/}"
mp3="${flac%.flac}.mp3"
if [ ! -f ${NEWDIR}/${mp3} ]; then
echo -n "Converting ${f} ... "
flac -cds "${f}" | lame --quiet -b 256 -q 2 - "${NEWDIR}/${mp3}"
echo "done."
fi
done
done

durangowildlife
4th January 2009, 07:21 AM
Thanks! That's much better than trying to use find -mtime

Does LAME not keep the tags?

durangowildlife
4th January 2009, 07:37 AM
Er, got it with gstreamer...

#!/bin/bash
TOPDIR=~
IFS="$(echo -e "\n\r")"
for d in $(find ${TOPDIR}/Music -regex "^.*\.flac$"|sed 's/\/[^/]*\.flac$//'|sort|uniq)
do
echo -e "\nConverting files in ${d}:"
NEWDIR="${TOPDIR}/iPodMusic/${d#${TOPDIR}/Music/}"
if [ ! -d ${NEWDIR} ]; then
mkdir -p "${NEWDIR}"
fi
for f in ${d}/*.flac
do
echo -n "Converting ${f} ... "
flac="${f##*/}"
mp3="${flac%.flac}.mp3"
gst-launch filesrc location = "${f}" ! flacdec ! lame vbr=0 bitrate=128 ! id3mux name=tag v2-tag=true v1-tag=true ! filesink location = "${NEWDIR}/${mp3}"
echo "done."
done
done

durangowildlife
5th January 2009, 05:33 AM
Notice that gstreamer is using only one of my cores when converting the files... bah.