How To: Converting all of your flac files to mp3s

Reply to topic
 
Author Message

iisaphd ®

Gender: Unknown

Longevity: 8 years 9 months

Posts: 119

Location: USA

Post 23-Mar-2010 22:35

[Quote]

Today I decided that I wanted to stream my flac files into a game called secondlife using shoutcast. The issue with shoutcast, at least the linux version, is that it only wants to stream mp3s. Probably not a bad idea since this is going to be streamed in real time and mp3s contain a significant smaller amount of information than the flac files do.
So, since I have about 400 shows to convert, I wanted to automate this conversion with a simple linux shell script. Here's the way it will work:
1. Get a list of all my flac files
2. Loop through that list one file at a time
3. For each file, convert the flac to a wav
4. Convert the wav to an mp3
5. Remove the wav file [I don't have enough diskspace to store them longterm, hence the delete]
6. Repeat the loop until done
Here's the script particular to my file system:
find /media/FreeAgent\ Drive/data/music/19\ -\ S\ Bands/The\ Smiths/ -name "*.flac" -print0 | while read -d $'\0' file; do flac -d "$file"; lame -h "${file%.flac}.wav" "${file%.flac}.mp3"; rm "${file%.flac}.wav"; done
The bolded text above is specific to my file system, yours will be different. Generically, it would look like this (replace the bold letters with what is correct on your system):
find /path/to/your/drives/flac/collection/ -name "*.flac" -print0 | while read -d $'\0' file; do flac -d "$file"; lame -h "${file%.flac}.wav" "${file%.flac}.mp3"; rm "${file%.flac}.wav"; done
So how does this all work? I'll try to describe below.
1. find /path/to/your/drives/flac/collection/ -name "*.flac" -print0 | while read -d $'\0' file;
2. do flac -d "$file";
3. lame -h "${file%.flac}.wav" "${file%.flac}.mp3";
4. rm "${file%.flac}.wav";
5. done
Step #1 above simply runs the GNU find command to find all files with a name ending in .flac and prints those files to the shell with a null character appended to the end of each file. Then it pipes this output into the second part of the command which reads that list of files one file at a time (using the null character as a delimeter) and stores the current filename in a variable called file.
Step #2 starts the loop, calling the flac decompression program on each file. Quotes are important as many of the files/directories have spaces in their names. The output of this command is to create a wav file with the same name as the variable $file, except it will strip off the .flac at the end and replace it with .wav
Step #3 performs the mpeg encoding. lame is used for the encoding. It takes one option "-h" to indicate high quality (my choice) and two arguments. The first argument is the input filename and the second is the output filename. Since the variable $file will contain something like "1. This is a song.flac", we have to get fancy so that we can feed "1. This is a song.wav" and "1. This is a song.mp3" as arguments to lame.
As a consequence of doing the conversion to .wav and then to .mp3 in two steps, I need step #4 above to remove the .wav file after it is no longer needed. You can probably improve this script by collapsing steps 2 and 3 into one line via piping the output of flac directly into lame instead of taking the two step route.
Prerequisites:
You'll need to have flac and lame. Just open up a terminal and get them if you need them via:
sudo apt-get install flac
sudo apt-get install lame
Since I have about 400 shows (~500 hours?) and because my cpus do the conversion at a rate of about 10x, I can expect that the script is going to take about 50 hours to run. When I get done, I'll post the secondlife location where I am streaming the music if you ever want to drop by.
Lance Gerhadsen


Last edited by iisaphd on 2010-11-20 19:10; edited 1 time in total
[Profile] [PM]

iisaphd ®

Gender: Unknown

Longevity: 8 years 9 months

Posts: 119

Location: USA

Post 25-Mar-2010 00:59 (after 1 day 2 hours)

[Quote]

I've made the above a little more robust. Since the program takes so long to run, I want to make it so that it does not reconvert anything already converted in case I have to stop and restart the program.
Here is the updated script:
find /media/FreeAgent\ Drive/data/music/ -name "*.flac" -print0 | while read -d $'\0' file;
do
if [ -e "${file%.flac}.mp3" ]; then
echo Skipping "$file" because "${file%.flac}.mp3" is already there
else
flac -d "$file";
lame -h "${file%.flac}.wav" "${file%.flac}.mp3";
rm "${file%.flac}.wav";
fi
done
[Profile] [PM]

DavidA

Gender: Unknown

Longevity: 11 years

Posts: 1406

Post 25-Mar-2010 07:28 (after 6 hours)

[Quote]

Thanks iisaphd!
[Profile] [PM]

iisaphd ®

Gender: Unknown

Longevity: 8 years 9 months

Posts: 119

Location: USA

Post 27-Nov-2012 16:00 (after 2 years 8 months)

[Quote]

And if you wanted to remove all those mp3 files that you created, you could do something like this from the root of your music directory:
find . -name "*.flac" -print0 | while read -d $'\0' file;
do
if [ -e "${file%.flac}.mp3" ]; then
echo Removing "${file%.flac}.mp3" ;
rm "${file%.flac}.mp3";
fi
done
[Profile] [PM]
Display posts:    
Reply to topic

Current time is: 17-Oct 20:18

All times are UTC ± 0



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum