Batch Export Scripting - CloseProject/Exit commands crashing Audacity

Hi,

I’m trying to batch export over a thousand Audacity project files.

I’m not much of a coder, so I’m editing the example .py file to accomplish this.

The script I cobbled together does export files correctly, however on completion when it tries to either close the project before opening a new one, or exit Audacity completely at the end of the conversion process, Audacity crashes and brings up the Submit Error Report dialogue box and never actually closes down any project or the program itself.

The main issue is that, without it closing the project etc. it just loads multiple instances until system resources are used up and everything freezes.

I originally thought that perhaps it was going to the next project file in the array before export was finished, and had put in wait timers, but these made no difference, Audacity would export the file, the script would then wait, then once finished promptly crash Audacity.

I was wondering if either there’s an issue with the script and there’s something else needed to let Audacity gracefully close a project/exit, or if there’s a way of disabling the Submit Error Report completely so that it can just crash.

I could do files in smaller batches, so that there’s enough resources available to cope and then force exit all the open instances through a task manager, but it would be good if I could fix the bug.

I’ve attached the script as a text file to the post:

Audacity_Batch_Export.txt (3.5 KB)

The version of Audacity I’m using is from March 2025, v.3.7.3 which is the version Debian Trixie installed.

Thanks for any help you can give me.

@whoozzem

The Audacity Manual advises:

When applying a macro to files it is recommended not to process more than 500 files at a time.

Peter

I’m wondering if there is another way to approach this. Since you are using Python anyway, could you do it by automating the Audacity interface using pyautogui? This chapter from the excellent book ‘Automate the Boring Stuff with Python’ might hold a clue: ATBS - Chapter 23. The idea being to be able to close each project programmatically before moving to the next one. I only know little bits of Python so I’m not much help.

Wouldn’t this be for audio files rather than project files though? Or am I showing my lack of understanding?

Sorry, yes … :wink:

You can apply a macro to the current project or to a set of files - but NOT a set of projects.

When applied to audio files Audacity opens up a temporary project, runs the macro and then closes the temporary project.

Peter

Thanks for the information, it’s not a perfect solution because:

If you let the script launch Audacity to save you remembering to do so in advance, pyautogui never manages to focus on it as the active window through mouse clicks to bring it to the foreground - sadly, all the active window features that may have been the solution to this are for Windows only.

If you manually launch Audacity beforehand, then pyautogui interacts as expected and is able to click on titlebars to make it the foreground window.

Sometimes keyboard input appears to fail, so the project doesn’t get closed and a new Audacity instance is opened - however in my conversion tests (5 projects) it managed to only have two instances running rather than all five which is a definite improvement.

On the plus side to the above, it does gracefully close projects and also exit Audacity without Audacity crashing with the submit error report dialogue box, so that is a huge improvement over using Audacitys’ in-built close project/exit program commands.

So again, thank you for your help.

Attached is the latest version of the .py script as a .txt file in case it’s of use to anyone else:

Audacity_Batch_Export.txt (5.7 KB)

1 Like

Sounds like the crash is happening because the script is trying to close projects or exit Audacity while some export threads are still running. Audacity’s scripting API isn’t fully thread-safe for CloseProject or Exit commands right after export.

A few ways to handle this:
Wait for export completion properly:
Instead of a fixed timer, use GetInfo(“ExportDone”) or similar (depending on your Python-Audacity bridge) to ensure the export fully finishes before closing the project.
Close and reopen Audacity in smaller batches:
Process, say, 50–100 projects at a time, then manually or via a separate script restart Audacity. This avoids piling up resources and reduces crashes.
Use command-line export (if possible):
Audacity has a CLI mode (audacity –batch) that can export files without fully loading the GUI each time, which is much more stable for large batches.
Disable crash reporting temporarily:
On Linux, you can set export UBUNTU_REPORTER_DISABLE=1 or similar depending on your distro to suppress the submit dialog, but this doesn’t fix the crash itself.
Bottom line: the key is to ensure each export fully finishes and the project is stable before calling CloseProject or Exit. Right now, the script is rushing the commands, which triggers the crash.:grin:

The thing is, even with a single small (3 seconds of audio) project being exported, and sleeping after the export command for 30 seconds, Audacity would successfully export, then there’d be a 30 second wait, then the crash on trying to close the project/exit the program using the in-built scripting commands.

So, as waiting appeared to make no difference I removed all the wait commands so yes, the scripts I’ve posted here do rush the commands, though with the latest script attached in my previous post everything appears to function as intended since it both exports and gracefully closes projects.

Today I tested the latest script in earnest attempting to convert all 1347 projects in one go.

It successfully completed 1136 of these exports, before getting stuck (not running out of system resources though because the pyautogui commands were closing projects post-export) with the error:

File "<frozen codecs>", line 322, in decode

All I then had to do was remove the projects that had already been exported successfully from the scripts projects array, start the script going again and the rest converted just fine (and play correctly in VLC).

2 Likes

I am pleased to see you have made such good progress with your mammoth task. Despite you saying you’re not much of a coder, you impressed me. :grinning_face: You are now the de facto expert in a new area of endeavour for Audacity users.

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.