To accomplish your task, you can use the Python script below. This script will:
- Traverse through each subdirectory in
C:\Users\lucas\Downloads\a.
- Check if both
Backing Vocals.mp3 and Synth Voice.mp3 exist in the subdirectory.
- If they exist, mix the two audio files and save the result as
other.mp3 in the same directory.
- Delete the original
Backing Vocals.mp3 and Synth Voice.mp3 files only if other.mp3 was successfully created.
Requirements:
- Install the
pydub library by running: pip install pydub
- Ensure that
ffmpeg is installed on your system and is accessible from the command line. You can download it from FFmpeg Official Website.
Python Script:
import os
from pydub import AudioSegment
# Define the root directory
root_dir = r"C:\Users\lucas\Downloads\a"
# Loop through each item in the root directory
for dir_name in os.listdir(root_dir):
dir_path = os.path.join(root_dir, dir_name)
if os.path.isdir(dir_path):
# Define paths to the required files
backing_vocals_path = os.path.join(dir_path, 'Backing Vocals.mp3')
synth_voice_path = os.path.join(dir_path, 'Synth Voice.mp3')
other_mp3_path = os.path.join(dir_path, 'other.mp3')
# Check if both files exist
if os.path.isfile(backing_vocals_path) and os.path.isfile(synth_voice_path):
try:
# Load the audio files
backing_vocals = AudioSegment.from_file(backing_vocals_path)
synth_voice = AudioSegment.from_file(synth_voice_path)
# Mix the audio files
mixed = backing_vocals.overlay(synth_voice)
# Export the mixed audio as 'other.mp3'
mixed.export(other_mp3_path, format='mp3')
# Delete the original files after successful creation of 'other.mp3'
os.remove(backing_vocals_path)
os.remove(synth_voice_path)
print(f"Created '{other_mp3_path}' and deleted source files in '{dir_path}'.")
except Exception as e:
print(f"Error processing '{dir_path}': {e}")
else:
print(f"Required files not found in '{dir_path}'. Skipping this directory.")
How the Script Works:
- Traversal: The script uses
os.listdir to list all items in the root directory and checks if they are directories using os.path.isdir.
- File Checks: It constructs the paths for
Backing Vocals.mp3 and Synth Voice.mp3 and checks if both exist using os.path.isfile.
- Mixing Audio:
- Loading Files: Uses
AudioSegment.from_file to load the MP3 files.
- Overlaying Audio: Uses the
overlay method to mix the two audio tracks.
- Exporting: Saves the mixed audio as
other.mp3 in the same directory.
- Cleanup: If the mixing and saving are successful, it deletes the original MP3 files using
os.remove.
- Error Handling: If any error occurs during processing (e.g., file not found, read/write error), the script catches the exception and prints an error message without deleting any files.
Notes:
- Mixing Logic: The
overlay method mixes the two audio files together. If you need a different mixing method (e.g., adjusting volumes, synchronizing), you might need to adjust the code accordingly.
- ffmpeg Dependency: Since
pydub relies on ffmpeg for handling MP3 files, ensure ffmpeg is installed and added to your system's PATH environment variable. You can verify installation by running ffmpeg -version in the command prompt.
Running the Script:
- Save the script to a file, e.g.,
mix_audio.py.
- Open a command prompt and navigate to the directory containing
mix_audio.py.
- Run the script using
python mix_audio.py.
Example Output:
Created 'C:\Users\lucas\Downloads\a\Subfolder1\other.mp3' and deleted source files in 'C:\Users\lucas\Downloads\a\Subfolder1'.
Required files not found in 'C:\Users\lucas\Downloads\a\Subfolder2'. Skipping this directory.
Error processing 'C:\Users\lucas\Downloads\a\Subfolder3': [Error details]
This script should fulfill your requirements, processing only the directories where both required MP3 files exist, creating the other.mp3 file, and cleaning up the original files upon success.