Batch split 35 separate 45 minute files into "3" 15 minute files each?

Is there a way to open up all 35 files in Audacity (45 minutes each) and split them up into 3 files of 15 minutes each for all files then export it all to .wav in one folder?

By “files” do you mean you have 35 Audacity projects or 35 WAV files?

This can be done with Macros, but the exact method depends on what you’re starting with.

– Bill

Hey Bill, most are WAV files and some are m4a, but I imagine the operation would be the same? I was thinking it would require some kind of scripting, like keyboard maestro or similar, it’s nice that Audacity has that built in (new to it).

I just realized the files need to be under “9” minutes each now, but they don’t have to be precisely cut on any transients or silence, just at 9 minute increments. I’m not a script wizard, is this one easy to macro up?

These macros work. They may not be optimum - perhaps someone with more experience with macros could improve on them.

They should get you started. There are three, called “First Chunk”. “Second Chunk” and “Third Chunk”. You’ll need to make two more (“Fourth Chunk” and “Fifth Chunk”, and perhaps “Sixth Chunk” if your input files are over 45 minutes but less than 54 minutes).

To install these go to the Audacity settings folder, which is at ~/Library/Application Support/audacity. There should be a “Macros” folder there. Download the attached macro files and drag them into that folder. They should then show up in Audacity’s Macros dialog.

You’ll need to run each macro on the set of files. The macro “First Chunk” will export the first 9 minutes. The macro “Second Chunk” will export the second 9 minutes, etc. If the input file is named “myfile.mp3”, the first exported file will be named “myfile.wav”, the second file will be named “myfile001.wav”, etc.

Audacity Macros have no if-then testing ability, so it is possible to export empty WAV files if the input file is shorter than expected.

This manual page explains how to apply macros to files.

– Bill

EDIT: See better solution in message below.
Third Chunk.txt (82 Bytes)
First Chunk.txt (48 Bytes)
Second Chunk.txt (81 Bytes)

I was interested to see what you would come up with Bill.
I agree, there’s no really elegant way to do this with normal Macros.

The best solution that I came up with, was to create a “Nyquist Macro”, and use it in a normal macro.

Normal Macros have the convenience that they can automatically run through a list of files, and look after the file naming for you when you export.
Nyquist Macros have the benefit of being able to do things according to programmed logic.

In this case, we are splitting each file into 5 parts, so the Nyquist Macro will calculate the exact length of each part, then loop through the track, extracting successive parts at the calculated lengths.
This is the Nyquist code:

;nyquist plug-in
;version 4
;type tool analyze
;name "Multi-split"
;author "Steve Daulton"
;release 2.3.2
;copyright "Released under terms of the GNU General Public License version 2"

(setf num 5)  ; Number of parts to  split into.

(setf dur (/ (get '*track* 'end-time) num))
(dotimes (i num)
  (aud-do "SelectTracks: Track=0 TrackCount=1 Mode=Set")
  (aud-do (format nil
                  "SelectTime:End=~a Start=~a"
                  (* i dur)
                  (* (1+ i) dur)))
  (aud-do "Trim:")
  (aud-do "Align_StartToZero:")
  (aud-do "ExportWAV:")
  (aud-do "Undo")
  (aud-do "Undo"))

;; Return no-op.
""

Note that line 5 determines how many parts to split the file into, which in this case is 5 parts.

To install the Nyquist macro, download the file here:
multi-split.ny (617 Bytes)
then install the downloaded file as described here: https://manual.audacityteam.org/man/installing_effect_generator_and_analyzer_plug_ins_on_mac_os_x.html#nyquist_install

When installed and enabled, the plug-in will appear in the “Tools” menu, but we don’t want to run it from there. Create a new (normal) Macro, and add the “Multi-split” plug-in to the Macro.

You can now run the normal Macro on the files, and it will apply the Nyquist Macro to each imported file in turn.

Steve: Aha! I didn’t think of using Undo to get back to the state where the entire imported track is still there. Using that method one could string my macros together to extract the 5, 9-minute chunks.
– Bill

Here’s a straight macro that will divide a file into 6, 9-minute chunks. Much faster than my first try, as this only has to import the original file once. Also, just select your 35 files (all in the same folder, right?) and walk away.
You can delete the sixth export (and the Undo, Undo, Select Time, Trim and Start-to-End steps) (steps 27 through 32) in the macro if you know your original files are exactly 45 minutes or less.
– Bill
9-minute chunks.txt (456 Bytes)

Thanks much! I’ve been using the last one and it seems to work well for those 45-minute files. I’m also trying to reverse engineer (somewhat understanding) the Nyquist code.

So one question, if I needed to split the files up into “five-minute segments” and the folder of files I wanted to do that inside of had random files of unequal lengths (45 min, 90 min, 120 min etc), would the “9-minute chunks” macro be the best way to go about that? Could I just adjust the numbers (smaller segments) inside the macro to five minutes, then increaste the number of segments to always go to the 120 mark? What would happen if the file is shorter, would it just be wasted time? Thanks!

You could do that, but for the shorter tracks you will get a bunch of empty files when the track runs out of audio.

The “Nyquist Macro” has the advantage (because Nyquist is a programming language) that it can make the number of loops conditional on the length of the track. The plug-in that I posted previously could be modified quite easily to provide a general solution. A control could be added for the length that you want the sections to be, which you would then set when you add the plug-in to the Macro. If I have time later this evening, I’ll see if I can knock together an example, and post it here.

The line beginning “;control” tells Audacity to create a GUI with a “time” control (a new feature in Audacity 2.3.0) https://wiki.audacityteam.org/wiki/Nyquist_Plug-ins_Widgets#Time_Widget

When calculating the number of segments, we need to round up “track length” / “segment length”. I’ve done that by rounding down (truncate to integer), then checking if “num x dur” is less than the track length.

The double quotes at the end provide a valid “no-op” return value so that the macro is not interrupted.

“AUD-DO” is a Nyquist command that passes “Scripting command” (like the commands use in normal Macros) to Audacity (Missing features - Audacity Support)

;nyquist plug-in
;version 4
;type tool analyze
;name "Multi-split"
;author "Steve Daulton"
;release 2.3.2
;copyright "Released under terms of the GNU General Public License version 2"

;control dur "Maximum duration of files" time "" 300 1 14400

;Calculate number of segments
(setf num (truncate (/ (get '*track* 'end-time) dur)))
(when (< (* num dur) (get '*track* 'end-time))
  (setf num (1+ num)))

(dotimes (i num)
  (aud-do "SelectTracks: Track=0 TrackCount=1 Mode=Set")
  (aud-do (format nil
                  "SelectTime:End=~a Start=~a"
                  (* i dur)
                  (* (1+ i) dur)))
  (aud-do "Trim:")
  (aud-do "Align_StartToZero:")
  (aud-do "ExportWAV:")
  (aud-do "Undo")
  (aud-do "Undo"))

;; Return no-op.
""

I was away for a few days and will try this after work tonight. Appreciated!

I know this thread is a little bit old but I am trying to do something similar. I have modified the code to work for my purpose, which is truncate silence and then break up the clips but how do I get it to save multiple separate files? The macro keeps saving over one single mp3 file.

;control dur "Maximum duration of files" time "" 300 1 14400

;Calculate number of segments
(setf num (truncate (/ (get '*track* 'end-time) dur)))
(when (< (* num dur) (get '*track* 'end-time))
  (setf num (1+ num)))

(setf dur (/ (get '*track* 'end-time) num))
  
  (aud-do "SelectTracks: Track=0 TrackCount=1 Mode=Set")
  (aud-do "TruncateSilence:Action="Truncate Detected Silence" Compress="50" Independent="0" Minimum="0.5" Threshold="-20" Truncate="0.5"")

(dotimes (i num)
  (aud-do "SelectTracks: Track=0 TrackCount=1 Mode=Set")
  (aud-do (format nil
                  "SelectTime:End=~a Start=~a"
                  (* i dur)
                  (* (1+ i) dur)))
  (aud-do "Trim:")
  (aud-do "Align_StartToZero:")
  (aud-do "ExportMp3:")
  (aud-do "Undo")
  (aud-do "Undo"))

;; Return no-op.
""

I don’t remember for certain, but I’d guess that back in the summer of 2019, macro export would automatically number exported files rather than overwriting.

For current versions of Audacity you could use the “Export2:” command and set a new name for each exported file.
The easiest way to do this is to hard code the output path and a file name, then append a number for each successive file.

Also, this won’t work:

(aud-do "TruncateSilence:Action="Truncate Detected Silence" Compress="50" Independent="0" Minimum="0.5" Threshold="-20" Truncate="0.5"")

You need the command to be all one quoted string, but the quotes close after the first equal sign:

"TruncateSilence:Action="

To avoid closing the quotes prematurely, you can escape quotes with a backslash.

(aud-do "TruncateSilence:Action=\"Truncate Detected Silence\" ...

Tip: When using numeric values, quotes are optional.

This should work:

(aud-do "TruncateSilence:Action=\"Truncate Detected Silence\" Compress=50 Independent=0 Minimum=0.5 Threshold=-20 Truncate=0.5")

There’s quite a few changes since this topic began, so here’s an updated version of my previous plug-in for Audacity 3.1.x

This version will attempt to export to the default macro-output folder (see: https://manual.audacityteam.org/man/directories_preferences.html), but the folder must exist before running the plug-in. If the macro-output folder does not exist, it will throw an error to tell you so.

;nyquist plug-in
;version 4
;type tool analyze
;name "Multi-split"
;author "Steve Daulton"
;release 3.1.2
;copyright "Released under terms of the GNU General Public License version 2"

;control dur "Maximum duration of files" time "" 300 0 14400

;Calculate number of segments
(setf num (truncate (/ (get '*track* 'end-time) dur)))
(when (< (* num dur) (get '*track* 'end-time))
  (setf num (1+ num)))

;get the current project name
(setf project-name (get '*project* 'name))
(if (string= project-name "")
    (setf project-name "Audacity"))


(defun get-macro-output-dir ()
  (let ((dir (first (aud-do "GetPreference: Name=\"Directories/MacrosOut/Default\"")))
        (file-sep (string *file-separator*)))
    (when (string= dir "")  ;default macro-ouptut directory
      ;get the default documents directory
      (setf dir (get '*system-dir* 'documents))
      (setf dir (string-right-trim "\\/" dir))
      (when (char= *file-separator* #\\)  ;Windows
        (string-append dir "\\Audacity")))
    (setf dir (string-right-trim file-sep dir))
    (format nil "~a~a~a~a" dir file-sep "macro-output" file-sep)))


(defun dir-exists (dir)
  (let ((old (setdir ".")))
    (cond ((setdir dir) (setdir old) t)
          (t (setdir old) nil))))


(defun process ()
  (let ((msg "Exported files:\n")
        (dir (get-macro-output-dir))
        new-name)
    (if (not (dir-exists dir))
        (throw 'err (format nil "Error.~%~s must exist" dir)))
    (dotimes (i num)
      (aud-do "SelectTracks: Track=0 TrackCount=1 Mode=Set")
      (aud-do (format nil
                      "SelectTime:End=~a Start=~a"
                      (* i dur)
                      (* (1+ i) dur)))
      (setf new-name (format nil "~a~a~a.wav" dir project-name i))
      (aud-do (format nil "Export2: Filename=~s" new-name))
      (string-append msg new-name "\n"))
    msg))


(if (< dur 0.1)
    "Error.\nFile duration must be at least 0.1 seconds."
    (catch 'err (process)))

Thanks Steve!

This worked very well. Quite a bit more coding to update things though it seems. I really appreciate the help. I ended up just adding the truncate silence to the macro and changing your code to export as mp3 and it works exactly as I needed.