#!/usr/bin/env python # -*- coding: utf-8 -*- #On Debian, install missing python modules as below with python3- before the name: #sudo apt install python3-pyautogui #Command to launch this script: #python3 Audacity_Batch_Export.py import os import sys import psutil import pyautogui #If the script launches Audacity, it never becomes the active foreground window, even if you click on it before the conversion process starts - no idea why this happens, so commented out. #Launch Audacity, wait 12 seconds to allow it to launch. #os.popen("audacity") #pyautogui.sleep(12.0) #Allow the user to cancel the conversion before proceeding. OKCancel = pyautogui.confirm("Caution: Audacity must be running fullscreen with the title bar text not covered by any other window. \n\n Additionally Mod-Script-Pipe module enabled in preferences before continuing. \n\n You cannot use the computer for any other tasks while conversions are taking place as the Audacity window must be able to be made the Foreground window by the script! \n\n Click OK to proceed or Cancel to Quit the conversion process") #If Cancelled, just quit the program, otherwise proceed. if OKCancel == 'Cancel': quit() else: #Get the current screen size, and divide the width (X) by two. screen_size = pyautogui.size() click_area_X = screen_size[0] / 2 click_area_Y = screen_size[1] / 2 #Make sure the Audacity window is our active window, expects it to be fullscreen! pyautogui.click(click_area_X, click_area_Y) pyautogui.sleep(2.0) ######################################## #Remember the . in the extension ! export_extension = ".wav" #Slash direction (linux style / vs Windows \) path_slash_style = "/" #Note, the quotes and end comma are important "", !!! Find/Replace .aup3 with .aup3", and file:// or C:\ with # " or "C:\ etc. project_files_list= [ "Path-To-Project_1.aup3", "Path-To-Project_2.aup3", "Path-To-Project_3.aup3", "Path-To-Project_Etc.aup3", ] ######################################### if sys.platform == 'win32': print("pipe-test.py, running on windows") TONAME = '\\\\.\\pipe\\ToSrvPipe' FROMNAME = '\\\\.\\pipe\\FromSrvPipe' EOL = '\r\n\0' if str("audacity.exe" in (i.name() for i in psutil.process_iter())) == "False": pyautogui.alert("Audacity needs to be running first - launch it and maximise it to be Fullscreen. \n\n Also make sure Audacity > Edit > Preferences > Modules > Mod-Script-Pipe is Enabled. \n\n The script will now exit.") quit() else: print("pipe-test.py, running on linux or mac") TONAME = '/tmp/audacity_script_pipe.to.' + str(os.getuid()) FROMNAME = '/tmp/audacity_script_pipe.from.' + str(os.getuid()) EOL = '\n' if str("audacity" in (i.name() for i in psutil.process_iter())) == "False": pyautogui.alert("Audacity needs to be running first - launch it and maximise it to be Fullscreen. \n\n Also make sure Audacity > Edit > Preferences > Modules > Mod-Script-Pipe is Enabled. \n\n The script will now exit.") quit() TOFILE = open(TONAME, 'w') FROMFILE = open(FROMNAME, 'rt') def send_command(command): TOFILE.write(command + EOL) TOFILE.flush() def get_response(): result = '' line = '' while True: result += line line = FROMFILE.readline() if line == '\n' and len(result) > 0: break return result def do_command(command): send_command(command) response = get_response() print("Rcvd: <<< \n" + response) return response for project_file_path in project_files_list: #Make sure the Audacity window is our active window, expects it to be fullscreen! pyautogui.click(click_area_X, click_area_Y) #Split our project file path down and create our export audio path from it. path = os.path.split(project_file_path) filename = str(path[1]) filename = filename.split(".") filename = str(filename[0]) filename = filename+export_extension #Create the export path. export_path = str(path[0]+path_slash_style+"Export"+path_slash_style+filename) #Make sure we have an export folder. try: os.mkdir(str(path[0]+path_slash_style+"Export"+path_slash_style)) except Exception as e: print(f"An error occurred: {e}") #Now open our project. open_command_prefix = "OpenProject2: Filename=\"" open_command_path = str(project_file_path) open_command_suffix = "\"" #Combine our three strings together to create the final export command. open_command_final=open_command_prefix+open_command_path+open_command_suffix #Open the current array item project in Audacity. do_command(open_command_final) #Required for export to some filetypes (e.g. mp3). do_command('SelectAll:') #Now export. export_command_prefix = "Export2: Filename=\"" export_command_suffix = "\"" #Combine our three strings together to create the final export command. export_command_final=export_command_prefix+export_path+export_command_suffix #Export the project to the desired output format. do_command(export_command_final) #Close the project, give a small sleep timeout before proceeding to the next project to allow the project to close. pyautogui.keyDown('ctrlleft'); pyautogui.press('w'); pyautogui.keyUp('ctrlleft') pyautogui.sleep(0.5) #Close Audacity once all conversions are complete. pyautogui.keyDown('ctrlleft'); pyautogui.press('q'); pyautogui.keyUp('ctrlleft') pyautogui.alert("Conversion Complete !")