Linux - DesktopThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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!
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.