LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (http://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   Bash script to convert all mp3 files to aac (http://www.linuxquestions.org/questions/linux-desktop-74/bash-script-to-convert-all-mp3-files-to-aac-768678/)

eeepc4gsurf 11-12-2009 11: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 11:36 AM

see my sig to learn bash

David the H. 11-12-2009 01: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 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!

amwink 11-12-2009 04: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:

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!

ghostdog74 11-12-2009 07:25 PM

Quote:

Originally Posted by amwink
Code:

for x in `ls -1 *.mp3`

no need ls
Code:

for x in *.mp3

eeepc4gsurf 11-13-2009 12:24 AM

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 06:59 AM

Quote:

Originally Posted by amwink (Post 3754745)
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/')"

i92guboj 11-13-2009 08: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 08:20 AM.