LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-24-2004, 05:13 PM   #1
zuralin
Member
 
Registered: Sep 2003
Distribution: Debian testing/unstable
Posts: 225

Rep: Reputation: 30
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.
 
Old 11-24-2004, 05:28 PM   #2
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 46
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...
 
Old 11-24-2004, 06:39 PM   #3
zuralin
Member
 
Registered: Sep 2003
Distribution: Debian testing/unstable
Posts: 225

Original Poster
Rep: Reputation: 30
Very cool, thank you very much
 
Old 03-08-2009, 08:41 AM   #4
Worksman
Member
 
Registered: Sep 2004
Location: Romania
Distribution: Ubuntu, Debian, Arch Linux, Gentoo, Slackware
Posts: 171
Blog Entries: 1

Rep: Reputation: 31
Thumbs up 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.
 
Old 03-09-2009, 02:05 PM   #5
sureshsujatha
Member
 
Registered: Mar 2009
Posts: 40

Rep: Reputation: 15
Cool script Worksman ... Thanks!

Last edited by sureshsujatha; 03-14-2009 at 12:18 AM.
 
Old 03-14-2009, 03:51 PM   #6
Worksman
Member
 
Registered: Sep 2004
Location: Romania
Distribution: Ubuntu, Debian, Arch Linux, Gentoo, Slackware
Posts: 171
Blog Entries: 1

Rep: Reputation: 31
Quote:
Originally Posted by sureshsujatha View Post
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...
 
Old 03-14-2009, 10:42 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

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

ecasound -i file.flac -o file.mp3
.
 
Old 03-15-2009, 10:56 AM   #8
Worksman
Member
 
Registered: Sep 2004
Location: Romania
Distribution: Ubuntu, Debian, Arch Linux, Gentoo, Slackware
Posts: 171
Blog Entries: 1

Rep: Reputation: 31
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.
 
Old 08-19-2011, 03:50 AM   #9
slacktroll
Member
 
Registered: May 2011
Distribution: Slackware 13.37
Posts: 50

Rep: Reputation: 3
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!

Last edited by slacktroll; 08-19-2011 at 04:03 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
flac to ogg and mp3 DJOtaku Linux - General 4 09-28-2005 05:24 PM
howto convert .flac to .mp3 Lleb_KCir Linux - Software 4 05-21-2005 06:07 PM
Convert a Bash script to C Linh Programming 5 04-25-2004 12:51 PM
Flac to wav or mp3 doesnt matter....but help Ogledbyoldmen Linux - Software 1 03-14-2004 01:27 PM
mp3 name 'squeezer' bash script ?? purpleburple Linux - General 7 12-12-2002 04:19 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration