LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 11-12-2009, 10:30 AM   #1
eeepc4gsurf
LQ Newbie
 
Registered: Nov 2008
Posts: 10

Rep: Reputation: 0
Bash script to convert all mp3 files to aac


Hello. I've decided to spend some time to learn bash scripting and related subjects. As an exercise, I want to write a simple script to convert all .mp3 files in a folder (which contains subfolders, containing the mp3 files), and replace them, in place, with aac files. Of course, I could easily use foobar2000 in windows to do it, but I'm trying to do it using the command line. I have madplayer and faac installed. The new files would have to have the same name as the original files. eg. file1.mp3 would have to be converted to file1.mp4, within the same directory. Any ideas how it may be done? I've tried to do it with find, but the problem is in preserving the file names.

Thanks in advance
 
Old 11-12-2009, 10:36 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
see my sig to learn bash
 
Old 11-12-2009, 12:05 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You should always start by searching Google and the LQ archives. This is a very common request, and there's tons of information about it.

But to really learn bash scripting (and I heartily recommend it), you'll have to work through some guides and tutorials first. In addition to the link ghostdog gave, I recommend linuxquestions.org and the Bash Guide for Beginners.

For the record, your task is very easy. All you need is a simple "for" or "while" loop with a bit of parameter substitution in the variable expansion for modifying the filenames. Good luck!
 
Old 11-12-2009, 03:50 PM   #4
amwink
Member
 
Registered: Mar 2005
Location: Connecticut/USA
Distribution: Ubuntu 9.04
Posts: 68

Rep: Reputation: 15
After you read tutorials and do more searching to teach yourself, if you still can't solve using your creativity to write the script, here is then a suggestion:

Assuming that your converter has a general syntax like this:

Code:
convert_my_song input_song.mp3 output_song.aac
then the following general idea should work

Code:
for x in `ls -1 *.mp3` ; do
   echo "Converting ${x}" # just some feedback
   convert_my_song ${x} ${x}.aac # this will convert, but produce undesired filenames like *.mp3.aac
   mv ${x}.aac `echo "${x}.aac" | sed s/.mp3.aac/.aac/g` # this renames correctly
end
This is not your final script, but just an idea for you to build upon, with your own customizations.

Depending on your Linux flavour, there might be different versions of the commmand 'rename'. Check 'man rename' to see which is yours, and you can use instead of mv with sed.

Hope this helps!

Last edited by amwink; 11-12-2009 at 04:04 PM.
 
Old 11-12-2009, 06:25 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by amwink
Code:
for x in `ls -1 *.mp3`
no need ls
Code:
for x in *.mp3
 
Old 11-12-2009, 11:24 PM   #6
eeepc4gsurf
LQ Newbie
 
Registered: Nov 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks for the help. Sorry for not searching around before. I have a test tomorrow, and after that I will get back to this. I'll post the solution here after solving my problem.
 
Old 11-13-2009, 05:59 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by amwink View Post
Code:
convert_my_song ${x} ${x}.aac # this will convert, but produce undesired filenames like *.mp3.aac
mv ${x}.aac `echo "${x}.aac" | sed s/.mp3.aac/.aac/g` # this renames correctly
This is where I said parameter substitution comes in. It makes it unnecessary to use external commands like sed. You can modify the filename directly in the original command.

I'd purposely left off providing any specific code to give the OP the learning challenge, but I suppose I need to point this out now. Using the above generic converter command:
Code:
convert_my_song "${x}" "${x%.*}".aac
This will remove the previous extension (everything from the last dot to the end of the string), then tack on the new extension.

You also need to quote any variable where the string may include spaces.

PS: Also note that even with sed, you can embedded the command in the first line and avoid the separate renaming step.

Code:
convert_my_song "${x}" "$(echo ${x}|sed 's/*.mp3/.aac/')"

Last edited by David the H.; 11-13-2009 at 06:08 AM.
 
Old 11-13-2009, 07:25 PM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Mind that, if this is just for learning purposes, then it's fine. But beware that mp3 and aac are both lossy formats. That means that when you encode in one of these formats you lose quality. If you encode in mp3, then decode, then encode into mp4 you are losing quality in both encodings. In other words, the resulting mp4 files will have lesser quality than the original mp3 ones.
 
  


Reply

Tags
aac, bash, faac, find, mp3



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
quick bash script to convert flac to mp3 zuralin Programming 8 08-19-2011 03:50 AM
need a bash script to batch convert .wav to .mp3 nass Slackware 15 06-23-2007 01:00 AM
Batch convert MP3 and AAC files to FLAC burninGpi Linux - General 2 05-07-2007 08:01 PM
Convert mp3 to aac conn-fused Linux - Software 3 07-11-2006 06:11 AM
Convert mp3 to m4b (aac) merixon Linux - Software 1 01-18-2006 04:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 07:19 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration