| Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-12-2009, 10:30 AM
|
#1
|
|
LQ Newbie
Registered: Nov 2008
Posts: 10
Rep:
|
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
|
|
|
|
11-12-2009, 10:36 AM
|
#2
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
see my sig to learn bash
|
|
|
|
11-12-2009, 12:05 PM
|
#3
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,568
|
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!
|
|
|
|
11-12-2009, 03:50 PM
|
#4
|
|
Member
Registered: Mar 2005
Location: Connecticut/USA
Distribution: Ubuntu 9.04
Posts: 68
Rep:
|
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.
|
|
|
|
11-12-2009, 06:25 PM
|
#5
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
|
Originally Posted by amwink
Code:
for x in `ls -1 *.mp3`
|
no need ls
|
|
|
|
11-12-2009, 11:24 PM
|
#6
|
|
LQ Newbie
Registered: Nov 2008
Posts: 10
Original Poster
Rep:
|
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.
|
|
|
|
11-13-2009, 05:59 AM
|
#7
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,568
|
Quote:
Originally Posted by amwink
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.
|
|
|
|
11-13-2009, 07:25 PM
|
#8
|
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 3,965
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:47 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|