-   Linux - Desktop (
-   -   Bash script to convert all mp3 files to aac (

eeepc4gsurf 11-12-2009 10:30 AM

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

ghostdog74 11-12-2009 10:36 AM

see my sig to learn bash

David the H. 11-12-2009 12:05 PM

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 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!

amwink 11-12-2009 03:50 PM

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:


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


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

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!

ghostdog74 11-12-2009 06:25 PM


Originally Posted by amwink

for x in `ls -1 *.mp3`

no need ls

for x in *.mp3

eeepc4gsurf 11-12-2009 11:24 PM

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.

David the H. 11-13-2009 05:59 AM


Originally Posted by amwink (Post 3754745)

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:

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.


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

i92guboj 11-13-2009 07:25 PM

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.

All times are GMT -5. The time now is 09:18 PM.