Skip to content

Instantly share code, notes, and snippets.

@MaySoMusician
Last active June 16, 2025 01:23
Show Gist options
  • Save MaySoMusician/9f97ff428c5beb78465b45c00fbae43a to your computer and use it in GitHub Desktop.
Save MaySoMusician/9f97ff428c5beb78465b45c00fbae43a to your computer and use it in GitHub Desktop.
Import each audio file in the directory, normalize it, and export it as a mp3 file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# cf. https://github.com/audacity/audacity/blob/be8bebce7a91042fedef5b0e5a98f9822d4bc0af/au3/scripts/piped-work/pipe_test.py
# cf. https://github.com/audacity/audacity/blob/be8bebce7a91042fedef5b0e5a98f9822d4bc0af/au3/scripts/piped-work/recording_test.py
"""Make sure Audacity is running first and that mod-script-pipe is enabled
before running this script.
Requires Python 2.7 or later. Python 3 is strongly recommended.
"""
import os
import sys
import time
import glob
if sys.platform == 'win32':
print("pipe-test.py, running on windows")
TONAME = '\\\\.\\pipe\\ToSrvPipe'
FROMNAME = '\\\\.\\pipe\\FromSrvPipe'
EOL = '\r\n\0'
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'
print("Write to \"" + TONAME +"\"")
if not os.path.exists(TONAME):
print(" ..does not exist. Ensure Audacity is running with mod-script-pipe.")
sys.exit()
print("Read from \"" + FROMNAME +"\"")
if not os.path.exists(FROMNAME):
print(" ..does not exist. Ensure Audacity is running with mod-script-pipe.")
sys.exit()
print("-- Both pipes exist. Good.")
TOFILE = open(TONAME, 'w', encoding='utf-8', errors='replace')
print("-- File to write to has been opened")
FROMFILE = open(FROMNAME, 'rt', encoding='utf-8', errors='replace')
print("-- File to read from has now been opened too\r\n")
def send_command(command):
"""Send a single command."""
print("Send: >>> \n"+command)
TOFILE.write(command + EOL)
TOFILE.flush()
def get_response():
"""Return the command 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 one command, and return the response."""
send_command(command)
response = get_response()
print("Rcvd: <<< \n" + response)
return response
def quick_test():
"""Quick test to ensure pipe is working."""
do_command("Help: CommandName=Help")
def process_files(path_dir):
"""
For each .wav file in path_dir, import, normalize to 0.2 peak,
export to MP3 (2 channels), and remove tracks.
"""
wav_files = sorted(glob.glob(os.path.join(path_dir, "*.wav")))
if not wav_files:
print("No .wav files found in:", path_dir)
return
for wav_path in wav_files:
base_name = os.path.splitext(wav_path)[0]
mp3_path = base_name + ".mp3"
# Import the .wav file
do_command(f'Import2: Filename="{wav_path}"')
# Normalize
do_command("SelectAll")
do_command("Normalize: PeakLevel=-0.2")
# Export as .mp3 (2 channels)
do_command("SelectAll")
do_command(f'Export2: Filename="{mp3_path}" NumChannels=2')
# Remove tracks
do_command("SelectAll")
do_command("RemoveTracks")
print(f"Processed: {wav_path}")
def main():
path_dir = os.getcwd() # Adjust if you want a different folder
quick_test() # Quick check that pipe is responsive (optional)
process_files(path_dir)
print("All done.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment