7

I have a 45 minute long audio (.m4a) file.

I would like to split this audio file into a separate audio file every 2 minutes.

So, that:

AudioFile1.m4a is created from [00:00] to [02:00] of originalAudioFile.m4a.

AudioFile2.m4a is created from [02:00] to [04:00] of originalAudioFile.m4a.

AudioFile3.m4a is created from [04:00] to [06:00] of originalAudioFile.m4a.

...

and this is continued until

AudioFile23.m4a is created from [44:00] to [45:00] of originalAudioFile.m4a.

Here are additional details:

  • I am not particular about the filenames of the resulting files.

  • I am not particular about the file types of the resulting files (e.g., whether they become .mp3 files or stay .m4a files).

  • This is not a situation where the resulting audio files will be mastered to a CD or something. I'm just looking for a quick and handy way to split an audio file based on a specific interval. A minor loss in audio quality is not a big deal (but this would obviously not be preferred).

  • I do not want the originalAudioFile.m4a to be altered in the process.

The most important condition is that the method requires minimal time and effort from the user.

There are various methods to accomplish what I want in Microsoft Windows, but can this be done on a Mac?

2

Use ffmpeg. It's available via Homebrew and MacPorts, but I just prefer to use the prebuilt binaries

Just off the top of my head, you could do something simple like:

$ ffmpeg -i source_audio_file.mp3 -ss 0 -t 120 segment_1.mp3
$ ffmpeg -i source_audio_file.mp3 -ss 120 -t 240 segment_2.mp3
$ ffmpeg -i source_audio_file.mp3 -ss 240 -t 360 segment_3.mp3
.
.
.

To put this into a script wouldn't be too hard

0

I've written an AppleScript that will accomplish exactly what I described in my original post. It makes use of ffmpeg, as suggested in a previous answer by user Allan.


Note: If you do not have ffmpeg currently installed on your computer, my script is worthless.

I installed ffmpeg via Homebrew. Homebrew is very easy to install; directions can be found at the top of this page (the official Homebrew website).

After installing Homebrew, run these lines individually in Terminal to install ffmpeg on your system:

brew install ffmpeg
brew link ffmpeg

Now, you will be able to make use of my script.


Notes about my code:

  • Even though this was not totally necessary for my purposes, ffmpeg also provides the user with total control over filenames, file types, bit rate, the output directory, etc., which is gravy.

  • Certain aspects of my script are tailored to my personal preferences (e.g., including the current date in the name of the output directory), so obviously modify the code as you see fit.

  • I've done my best to include some error handling to catch certain potential errors, but this is obviously far from all-inclusive.

  • I don't envision myself needing to do this audio splitting task that often, so a simple .scpt file created in Script Editor is fine by me.

    • If you prefer to convert this script to a launchable, standalone application, you can easily do so in Automator (by inserting a "Run AppleScript" action into your workflow and pasting the full code).

      • In that case, you might want to adjust the app so that the user can simply drag the source audio file onto the app icon to initiate the app (instead of having to provide the file path of this file in a dialog).
  • It is entirely possible, if laborious, to program a VBR (variable bit rate) encoding option for the resulting files into the script. I chose not to do so because, for my purposes, this ability is not particularly needed. It would be somewhat complicated to write, since not all audio formats can be encoded in VBR (only Opus, Vorbis, MP3, WMA, and AAC audio files can).

  • Finally, I am 100% certain that the code contains at least some shortcomings, blind spots, and/or bugs, so I apologize in advance.


I am not able to paste the entire script directly in this answer. The length of the code exceeds the maximum character limit for Stack Exchange answers.

You can view and download the full code on this Pastebin page.

Using the linked script, the user can intuitively split up any audio file into numerous files based on a defined interval, while requiring minimal time and effort from the user.

Note that when the user does not choose the source bit rate as the bit rate for the resulting files, the script can take a while. This is because ffmpeg is slow at re-encoding. But, fortunately, the user can simply set it, and direct their attention to something else as it completes.


Note that the format of two lines that Allan provided in their answer is incorrect.

After several hours of troubleshooting my AppleScript file, I realized that my AppleScript file was not the issue.

-t reflects the duration of the resulting audio file, not the ending time stamp.

Allan's sample code should read:

$ ffmpeg -i source_audio_file.mp3 -ss 0 -t 120 segment_1.mp3
$ ffmpeg -i source_audio_file.mp3 -ss 120 -t 120 segment_2.mp3
$ ffmpeg -i source_audio_file.mp3 -ss 240 -t 120 segment_3.mp3

Here is the relevant passage from the ffmpeg documentation:

-t duration (input/output)

When used as an output option (before an output url), stop writing the output after its duration reaches duration.

-2
find ./ -type f -exec ffmpeg -i {} -f segment -segment_time 300 -c copy {}%03d.mp3 \; -exec rm {} \;
  • 3
    There's a lot of different things going on here, including an ffmpeg dependency (something most people won't already have installed.) Could you explain what this command does and how? – JMY1000 Jan 14 '18 at 8:20
  • 2
    Also, how does this split one audio file into 23 pieces? And why is find necessary to split this one file? – nohillside Jan 14 '18 at 10:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .