LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   quick bash script to convert flac to mp3 (https://www.linuxquestions.org/questions/programming-9/quick-bash-script-to-convert-flac-to-mp3-258957/)

zuralin 11-24-2004 05:13 PM

quick bash script to convert flac to mp3
 
I was wondering if someone could give me a hand at writing a quick bash script to convert some .flac's to .mp3's so I can play them on my palm pilot. Right now all I have is:

Code:

flac -sdc $1 | lame - $2
Which as you can see is very limiting. I would like to be able to convert a while directory with something like: sh convert.sh /dir/full/of/flac and have it place the file's in the current directory.

Ultimately, I would like it to compose the file name from the previous file and the two top directories because my file structure is like so: artist/album/songs.flac, so the finished file would look like: artist-album-song.mp3

If I wasn't so busy with school I would read the bash coding how-to, but for now if someone could get my started that would be great. I also think this would benefit others, because of right now it seems to be sort of a pain in the ass to convert flac's to mp3's. Hell, I had to dig outside the debian repositories just to install lame.

sigsegv 11-24-2004 05:28 PM

Re: quick bash script to convert flac to mp3
 
Quote:

Originally posted by zuralin
I was wondering if someone could give me a hand at writing a quick bash script to convert some .flac's to .mp3's so I can play them on my palm pilot. Right now all I have is:

Code:

flac -sdc $1 | lame - $2
Which as you can see is very limiting. I would like to be able to convert a while directory with something like: sh convert.sh /dir/full/of/flac and have it place the file's in the current directory.

Code:

#!/bin/sh

cd $1

for S in *.flac;  do
        flac -sdc ${S} | lame - ${S%.flac}.mp3;
done

Quote:


Ultimately, I would like it to compose the file name from the previous file and the two top directories because my file structure is like so: artist/album/songs.flac, so the finished file would look like: artist-album-song.mp3

I'm too lazy to look up the shell variable that contains the path. I generally break out perl when I want something more complex than a for loop anyway.

Quote:


If I wasn't so busy with school I would read the bash coding how-to, but for now if someone could get my started that would be great. I also think this would benefit others, because of right now it seems to be sort of a pain in the ass to convert flac's to mp3's. Hell, I had to dig outside the debian repositories just to install lame.

Welcome to GPL hell. It seems to be catching...

zuralin 11-24-2004 06:39 PM

Very cool, thank you very much

Worksman 03-08-2009 08:41 AM

Quirk ;-)
 
This didn't work for me.
Using:

Code:

$ bash --version
GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.

But it did after I found out that
Code:

`*.flac`
doesn't escape spaces and other chars, then I enclosed the file names in double quotes.

This works perfectly for the current folder full of flac files, which will be copy-converted to file.mp3 instead. :)

Code:

#!/bin/sh

for S in *.flac; do
    flac -d -F --totally-silent -c "${S}" | lame -ms -q0 -V0 -B192 --resample 44.1 - "${S%.flac}.mp3";
done

Put it inside a file in the flac's folder and run it like "sh convert.sh" or "chmod u+x convert.sh; ./convert.sh"

Thanks for the initial script!

Note the extra arguments to the lame encoder. They are just for better conversion and slightly better quality. It just uses the best (slowest) algorithms and goes for a 192kbps VBR MP3. The mod is also set to Stereo (not Joint Stereo) for personal reasons; you can remove the -ms flag if you want to. My main target for this conversion was to play my files on a car FM MP3 player.

sureshsujatha 03-09-2009 02:05 PM

Cool script Worksman ... Thanks!
:)

Worksman 03-14-2009 03:51 PM

Quote:

Originally Posted by sureshsujatha (Post 3469966)
Cool script Worksman ... Thanks!
:)

Arch Linux

It's not even remotely "cool". It even makes me fell embarrassed to receive a compliment like that for such a lame mixture of command line parameters and pipes... I've been meaning to learn some real Bash'ing...

Sergei Steshenko 03-14-2009 10:42 PM

FWIW, one tool can be used for conversion instead of two: flac + lame - as simple as

ecasound -i file.flac -o file.mp3
.

Worksman 03-15-2009 10:56 AM

I doubt that is as flexible as using the two tools option.
Besides, I could go as far as to say that everyone prefers lame for their mp3 encoding needs. :)

No bashing intended.

slacktroll 08-19-2011 03:50 AM

Code:

#!/bin/sh

for S in *.flac; do
    flac -d -F --totally-silent -c "${S}" | lame -ms -q0 -V0 -B192 --resample 44.1 - "${S%.flac}.mp3";
done

it's not that bad: but could be tweaked to this:
Code:

#!/bin/sh

for S in *.flac; do
    flac -d -F --totally-silent -c "${S}" | lame -V2 --vbr-new --resample 44.1 - "${S%.flac}.mp3";
done

Edit: Note!! use lame 3.97 to achieve best quality!


All times are GMT -5. The time now is 02:19 AM.