Converting mp3 files to opus and m4a recursively using command line tools and scripts in Linux.

Hi I am Rupesh from India and I have large collection of music (mp3) file’s and I want to convert some of them to m4a ( advanced audio coding aac ) format and some of them to opus (specifically for speech) format. Previously I have converted using third party GUI tool in windows and the quality is acceptable but I want to try in Linux using scripts.

I have installed opensuse leap 15.0 on my system. My system has powerful components like amd fx 4100 processor which is quad core and 8 GB ram, 1 tb internal hard disk with 64 mb cache memory. So try to suggest which provide quality encoding.

In windows there are GUI tool’s for everything and within a few click’s we can do anything but in Linux there are command line tool’s for everything and we can do anything. There are advantages and disadvantages for the above two methods. For windows software’s are available which need less knowledge to use them but in Linux user needs thorough knowledge on what he is doing.

I want to convert music mp3 file’s to m4a and opus recursively using scripts on which I don’t have deep understanding and so I am requesting your help.

Actually what my intention is I want a bash script in which we can specify source directory path which contains mp3 files and after that we can specify destination directory path which contains converted m4a or opus file’s.

I want two scripts one for converting mp3 file’s to opus file’s and another for converting mp3 files to m4a files.

After converting each and every mp3 file to opus or m4a files all the track information or metadata present in mp3 file must be copied to converted m4a or opus file’s. Track information means album art ie., composer of the track etc.,.

The source mp3 file and the converted m4a or opus must look like same and also contains same music present in source mp3 file.

I think that there are two specific command line utilities to convert mp3 files to m4a or opus file’s and they are fdkaac and opusenc.

My source directory consists of some mp3 file’s and also some sub directories and again these sub directories consists of some mp3 file’s and also some sub directories. So I want to maintain same directory structure as source for the converted m4a or opus file’s.

I am going to illustrate the situation as follows

directory 1
{
file 1.mp3
file 2.mp3
file 3.mp3
{
directory 2
{
file 4.mp3
file 5.mp3
}
file 6.mp3
}

Upon converting the above directory I want suppose opus file’s with directory structure as follows

directory 1
{
file 1.opus
file 2.opus
file 3.opus
{
directory 2
{
file 4.opus
file 5.opus
}
file 6.opus
}

Upon converting each and every mp3 file to m4a or opus if any error occur the corresponding file name and its path must be redirected to a text file so that I can examine them later.

Is it possible to use opusenc tool or fdkaac tool to convert directly from mp3 to opus or m4a like

opusenc -i input.mp3 -o output.mp3.

Once upon a time I read that the mp3 file must be decoded using tools like sox to wave file and then it must be redirected to opusenc tool or fdkaac tool. If this statement is true may I know which is the best tool to decode mp3 to wave file.

In windows if I want to accomplish this task I must buy a software which costs upto 100 dollars and that too we do not have complete control. We can accomplish the same task at free of cost and at the same time with complete control.

Many of you suggest to use ffmpeg but I think that file which is transcoded using opusenc tool has good sound quality. Many of you may even suggest to try foobar but my answer is that sound quality may be poor.

I think that a great script or any software cannot be created by a single person and it can be achieved only through group work.

I have created this thread to get a great script which converts mp3 to m4a or opus file’s which have excellent sound quality. Please try to cooperate and suggest all your valuable thoughts with patience.

If you’d like to start a community group effort,
One of the first and best ways is to create a Github project for the group. You can create a personal repo or create an entirely separate account and entity while you retain management rights (and can delegate if you want to).

I’ve done CLI transcoding in the past using ffmpeg and mpeg and been satisfied with the results.
The quality is determined by the codecs library you use, not by the application or script that does the transcoding or even the container chosen…
I’ve looked into gstreamer codecs but found (at least at the time) that it’s not as easy to reference directly in a CLI console.
A GUI app I use for transcoding is Handbrake, but AFAIK it’s only set up to use ffmpeg.

If you propose using a different codecs library, remember that communities will generally support only what is legal, and preferably with free licensing (at least partial like x264/x265) or entirely free like Xvid and MP3 today(wasn’t always free to use). So, be sure you get a crystal clear understanding of what is being used and post it because everyone will want to be equally clear before investing themselves.

Good Luck,
TSU

Hi Rupesh,

IIRC you already asked similar questions here, can’t you modify the scripts you wrote at the time?

Hi,

You can probably do what you want in just one script. It all boils down to how options are handled in the script. Like for example you can have an option that accepts the path to your files and another that specify which format is the input and the output. This what I have in mind using short and long options.

With the short options.

myscript **-i** mp3 **-o** m4a **-p** /path/to/the/files

With the long options (like what a GNU program would normally support)

myscript **--input** mp3 **--output** m4a **--path** /path/to/the/files

One good advantage is that the options (words or a single letter that starts with a dash or a duoble dash) can be in any order.

Someone has already created scripts.

Please examine the following llinks

https://github.com/kbat/mp32opus

https://sourceforge.net/directory/?q=convert%20opus%20mp3%20command%20line

These scripts are free to use.

Can you examine the perl script called “perl audio converter” ie., pcapl.

Hi,

Examine what exactly? There should be an author somewhere in the script and you can communicate with the author(s) if needed.

Dear Rupesh,

Please be aware that we are all volunteers, doing what we do in our spare time, on our own hardware. We cannot mimic your install, your files etc etc. We cannot do your work for you, however we can support you in getting your work done. But you simply cannot ask from anyone here to start examining any script you find out there on the internet.
My advice: Create a test environment with just a couple of folders and files, and start testing whatever scripts you find that you think may work.

As I was searching for the words “multimedia codecs in Linux” I found the following

gstreamer plugins good
gstreamer plugins bad
gstreamer plugins ugly
libavcodec
xine
etc.,

Which is the best one to use in Linux scripts. Are there any proprietary codecs available for Linux.

I heard somewhere that libavcodec is inferior than others. Is it true.

With a little bit of reading (and IIRC since I haven’t looked at this again for quite awhile)

Anything that’s a plugin generally means it’s built to interface in a certain way with an application, at least I didn’t see a way to use it in a very basic CLI. And, because that’s generally how gstreamer codecs were provided when I looked at this, it then meant that gstreamer could only be used with applications that supported those gstreamer plugins.

There are some recommended codecs, but you should just do a little testing for yourself.
Not only will you be able to actually do an apples to apples comparison, but you’ll also develop some skills using the transcoding app you choose.

As for proprietary codecs…
They’re out there.

If you haven’t already downloaded and installed MediaInfo, you should to analyze every media file you have.
You’ll then get to see what codecs others have chosen, and the application used to transcode… plus a number of other things like whether certain parameters might have an effect.

Remember also that unless you do your own transcoding, you won’t know if compression rates are the same or what you would want. Raw (less compression) of course typically contains more data points so would be better at higher resolutions or produce better quality, but at some point given the limits of your playback application and hardware won’t make any difference.

TSU

I want to use pcapl but I want to know how to use it efficiently and so I searched for the words “pcapl help” “pcapl documentation” etc., but not found except manual page of it.

Can anyone of you suggest a link to it’s documentation.

I suppose you can start with where and what this “apcl” is.
Google search turns up nothing.

Perhaps it doesn’t exist?

TSU

A good starting place might be the project web site: https://vorzox.wixsite.com/pacpl

Or perhaps you could ask on the mailing list: Perl Audio Converter Mailing Lists

There is a strange feeling of déjà vu here :wink:

Aha!
pacpl and not acpl?

Interesting Google’s “fuzzy” search couldn’t come up with that even when I added some multimedia keywords…

TSU

Hi previously I have created a thread starting with “how to compress 90 GB mp3 file’s…” in the current form itself and in that thread I requested you to convert a Windows batch script to shell script.

The Windows script is as below


@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_SOURCE=C:\Rupesh	o convert"
set "_TARGET=E:\Rupesh\ffmpeg out"

rem // Change to source directory temporarily:
pushd "%_SOURCE%" || exit /B 1
rem // Enumerate source files, return paths relative to the source directory:
for /F "delims=" %%F in ('xcopy /L /S /I ".\*.mp3" "%_TARGET%" ^| find ".\"') do (
    echo Currently converting "%%F"...
    rem // Create destination directory, suppress error if it aready exists:
    mkdir "%_TARGET%\%%F\.." 2> nul
    rem // Perform actual file conversion, using paths relative to target directory:
    ffmpeg -i  "%%F" -v quiet -codec:a libmp3lame -b:a 16k -ar 11025 -map_metadata 0 -id3v2_version 3 -write_id3v1 1 "%_TARGET%\%%F\..\%%~nF.mp3"
)
echo Completed.
popd

endlocal
exit /B

Once I have executed the above script in Windows and ran successfully.

Can anyone of you convert the above Windows batch script as it is to bash script so that I can convert all mp3 files to m4a or opus files replacing ffmpeg command with opusenc or fdkaac commands.

You posted that script in 2017 in another thread, and again here? Not to sound nasty, but how much are you going to pay someone to do this for you?

The excuse of you being a ‘newbie’ is not going to work; you have been posting for several years now, on this forum and in many others. The only commonality is your continual insistence that you are a ‘newbie’, and (somehow) cannot ever write a script of your own. Is this not you
on LinuxQuestions?

Writing a simple bash script with a loop is trivial, and there are so many thousands of examples you can find within minutes, I cannot imagine why you’ve not been able to do this yourself for more than 2 years now, or even BEGIN to try.

The Linux Documentation Project has probably the best bash tutorials out there: https://www.tldp.org/LDP/Bash-Beginners-Guide/html/ And despite you’ve claimed in many other forums, you DO NOT have to ‘read many many books’ or spend ‘hundreds of hours studying’ to write
a simple bash script.