No diretório C:\Users\lucas\Downloads\a tem várias pastas e em cada pasta tem 2 arquivos e em algumas pastas 3 arquivos.
Sendo esses arquivos: Lead Vocal.mp3, Backing Vocals.mp3 e Synth Voice.mp3
Preciso de um script python que mixe o arquivo Backing Vocals.mp3 no arquivo Synth Voice.mp3 e salve o resultado como other.mp3 no respectivo diretório
Após ter criado o arquivo other.mp3, deverá excluir o arquivo Backing Vocals.mp3 e o Synth Voice.mp3, mas deverá excluir somente dos diretórios em que deu para criar o arquivo other.mp3.
Por fim deverá renomear o arquivo other.mp3 para Backing Vocals.mp3
Assistant
Certainly! Here is a Python script that accomplishes your requirements:
python
import os
from pydub import AudioSegment
# Define the root directory
root_directory = r'C:\Users\lucas\Downloads\a'
# Walk through all directories and subdirectories under the root directory
for dirpath, dirnames, filenames in os.walk(root_directory):
# Define the full paths for the files
backing_vocals_path = os.path.join(dirpath, 'Backing Vocals.mp3')
synth_voice_path = os.path.join(dirpath, 'Synth Voice.mp3')
# Check if both files exist in the current directory
if os.path.exists(backing_vocals_path) and os.path.exists(synth_voice_path):
try:
# Load the audio files
backing_audio = AudioSegment.from_mp3(backing_vocals_path)
synth_audio = AudioSegment.from_mp3(synth_voice_path)
# Get the durations of both audio files
duration_backing = len(backing_audio)
duration_synth = len(synth_audio)
max_duration = max(duration_backing, duration_synth)
# Pad the shorter audio with silence to match the length of the longer one
if duration_backing < max_duration:
silence_padding = AudioSegment.silent(duration=max_duration - duration_backing)
backing_audio += silence_padding
if duration_synth < max_duration:
silence_padding = AudioSegment.silent(duration=max_duration - duration_synth)
synth_audio += silence_padding
# Mix the two audio files
mixed_audio = backing_audio.overlay(synth_audio)
# Export the mixed audio as 'other.mp3' in the current directory
output_path = os.path.join(dirpath, 'other.mp3')
mixed_audio.export(output_path, format='mp3')
# Delete the original 'Backing Vocals.mp3' and 'Synth Voice.mp3' files
os.remove(backing_vocals_path)
os.remove(synth_voice_path)
# Rename 'other.mp3' to 'Backing Vocals.mp3'
new_backing_vocals_path = backing_vocals_path # This is the desired new filename
os.rename(output_path, new_backing_vocals_path)
print(f'Processed directory: {dirpath}')
except Exception as e:
print(f'Error processing directory {dirpath}: {e}')
continue
else:
# No action needed if both files do not exist
pass
Instructions to run the script:
Install Required Libraries:
Make sure you have the pydub library installed. You can install it using pip:
code
pip install pydub
Also, ensure that you have ffmpeg installed and added to your system's PATH, as pydub relies on it for handling audio files. You can download ffmpeg from https://ffmpeg.org/download.html.
Update the Root Directory:
The script uses the root directory C:\Users\lucas\Downloads\a as specified. If your directory is different, update the root_directory variable accordingly.
Run the Script:
Save the script to a file, for example, mix_audio.py, and run it using Python:
code
python mix_audio.py
What the Script Does:
Traverses All Subdirectories:
The script walks through all directories and subdirectories starting from the root directory you specified.
Checks for Required Files:
In each directory, it checks if both Backing Vocals.mp3 and Synth Voice.mp3 exist.
Mixes the Audio Files:
Loads the Audio Files: Uses AudioSegment.from_mp3() to load the MP3 files.
Matches Durations: Pads the shorter audio with silence so both have the same duration.
Overlays Audio: Uses the overlay() method to mix the two audio segments.
Saves the Mixed Audio:
Exports the mixed audio as other.mp3 in the same directory.
Cleans Up and Renames:
Deletes the original Backing Vocals.mp3 and Synth Voice.mp3 files.
Renames other.mp3 to Backing Vocals.mp3.
Handles Directories Without Both Files:
If a directory does not contain both required files, it skips that directory without performing any action.
Note:
Error Handling:
The script includes a try-except block to handle any exceptions that might occur during processing, such as issues with file access or corrupted audio files. It will print an error message and continue with the next directory if an error occurs.
Adjusting Volume Levels (Optional):
If you need to adjust the volume levels of the individual tracks before mixing, you can apply gain to the audio segments using the + or - operators:
python
backing_audio = backing_audio + 3 # Increase volume by 3 dB
synth_audio = synth_audio - 2 # Decrease volume by 2 dB
Add these adjustments before the overlay operation.
Summary:
This script automates the process of mixing Backing Vocals.mp3 and Synth Voice.mp3 in directories where both files exist, saves the mixed file as other.mp3, deletes the original files, and then renames other.mp3 to Backing Vocals.mp3, all while working recursively through the specified directory structure.