Using KM to Merge Audio Files?

I’m curious if it is possible to combine audio files (placing one file’s audio after the previous file’s audio) natively within Keyboard Maestro with an action.

If not I could always use MP4Box

Thanks

Hey @8times9,

No, Keyboard Maestro has no native functions to do this.

However - it can run all manner of AppleScripts and shell scripts - so you might be able to create a workflow with Keyboard Maestro that does what you want.

-Chris

Here is a macro that joins selected files in the Finder with MP4Box:

Join files with MP4Box.kmmacros (5.0 KB)

The joined file will be saved to the directory of the last item in the Finder selection. You can set the name of the joined file in the first (green) action.

Make sure that KM’s ENV_PATH variable contains the path where your MP4Box is located. (Usually /usr/local/bin if installed via Homebrew.)

4 Likes

This is great Tom, thank you!

When trying to use it for .aiff files it gives the error

Unknown input file type for "/Users/redacted/Downloads/Test/Test.aiff"

Does mp4box not support aiff files or am I doing something wrong? I did change the output name to end with a .aiff file extension.

According to the GPAC documentation MP4Box supports Iso Media files.

For AIFF you likely have to use a different tool. I made the macro for MP4Box because you mentioned MP4Box in your OP.

FFmpeg is a pretty good command line tool for many A/V formats (except for ISO Media, where MP4Box excels).

So, either…

  • find out the FFmpeg syntax and rewrite the macro for it
  • or, if you are going store the files anyway as m4a/mp4, you could convert them before and then join them with the MP4Box macro (the joining itself is lossless, nothing gets transcoded)

Edit:

Here is the FFmpeg documentation to join files of the same codec.

However, FFmpeg looks like overkill here and it seems to be easier with sox. It works simply like that:

sox file1.aiff file2.aiff file3.aiff newfile.aiff

You can install sox via Homebrew:

brew install sox
1 Like

Here is the macro from above, with sox instead of MP4Box. Seems to work fine with AIFF files:

Join files with sox.kmmacros (3.9 KB)

2 Likes

Thank you so much for taking the time to make the macro and for the information @Tom .

Thanks so much for sharing your work here! Personally I find it very exciting as it could vastly ease my management of a very large library. Alas, I'm no coder, don't know what to do with an "MP4Box: command not found." I tried to re-install via brew and got "gpac 1.0.1 is already installed and up-to-date." I followed your good instruction re paths but still get this same error. Could you help me out without too much trouble?

Hi,

Could you post two additional bits of information?

  • If you enter type -a mp4box in the Terminal, what does it return?

  • What is your Keyboard Maestro ENV_PATH?


For the ENV_PATH: You find it in Keyboard Maestro > Preferences > Variables.

Example:

Screen Shot 2022-04-15 at 12.11.38-pty-fs8

2 Likes

Thanks for making this so easy Tom.

mp4box is /usr/local/bin/mp4box
mp4box is /Applications/GPAC.app/Contents/MacOS//mp4box
pamela@MBP ~ %

and, embarrassed as I am to say it, both items under variables (Parameter and varname) are empty. I have nothing like the populated list you have but then I'm not very far into KM yet.

This is great, because then the problem should be solved by creating the variable and adding the path:

So, create a variable in Preferences > Variables (see screenshot) with the name ENV_PATH and add the usual paths of the binaries as value:

/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin

As you see, the path of your MP4Box (/usr/local/bin) is included as first (= preferred) location.

For more on this, see the KM Wiki article.

1 Like

Your patience really does make you a Tech Saint, Tom. Thank you so much.

If I can ask you one more basic and probably obvious question ; ) is it the case that we trigger the script by selecting our folder and pressing the shortcut? If so I'm unable to find any evidence of success on my Mac. What else am I missing?

The macro from above works on selected files. (I.e. you select the files you want to join in the Finder and then launch the macro.)

Yes, exactly what I've been doing but with no result. I'll try other triggers and see if any better.

I was referring to what you have said above: “we trigger the script by selecting our folder”.

Have you selected the files or the containing folder?

Well I'm not smart enough to workout the other triggers, apart from adjusting the macro combination (the default triggered another app so I had to change it slightly). When I click "Try," I get no return as I did before we added the path. I select the files in finder, press the macro keys, then look and listen carefully but nothing occurs. Then I check for a new file anywhere on the computer and still nothing.

Sorry...I've tried both folders and files.

Just tested it and it still works fine,

You should get a script output in a KM window, like this:

Screen Shot 2022-04-16 at 07.34.49-pty

The joined file is written to the folder where the source files are.

But you definitively have to select the files, not the folder. As macro hotkey trigger you can use whatever you want. The Try or Run button in KM also works.

Hmm. Perhaps there's something off about my KM installation. I'll poke around for a day and see if I can figure it out. I'll report back tomorrow. Thanks again for everything!