Certainly! Below is a Python script that accomplishes both tasks:
- A script to remove 0dB intros and outros from songs.
- A GUI program to increase or decrease the volume of songs and handle multiple songs via drag-and-drop.
Part 1: Script to Remove 0dB Intros and Outros
We'll use the pydub library to handle audio processing. You can install it using pip install pydub.
from pydub import AudioSegment
from pydub.silence import detect_leading_silence, detect_nonsilent
import os
def remove_0db_intro_outro(file_path, threshold_db=0):
sound = AudioSegment.from_file(file_path)
# Convert to mono to simplify dB measurement
sound = sound.set_channels(1)
# Detect leading silence
leading_silence = detect_leading_silent(sound)
# Detect non-silent ranges
non_silent_ranges = detect_nonsilent(sound, min_silence_len=100, silence_thresh=threshold_db)
if not non_silent_ranges:
print("No non-silent ranges detected. Skipping file:", file_path)
return sound
# Calculate start and end points
start_point = max(non_silent_ranges[0][0] - leading_silence, 0)
end_point = non_silent_ranges[-1][1]
# Trim the audio
trimmed_sound = sound[start_point:end_point]
return trimmed_sound
def process_directory(directory):
for root, _, files in os.walk(directory):
for file in files:
if file.lower().endswith(('.mp3', '.wav', '.flac')):
file_path = os.path.join(root, file)
trimmed_sound = remove_0db_intro_outro(file_path)
output_path = file_path.replace(file, f"trimmed_{file}")
trimmed_sound.export(output_path, format=file_path.split('.')[-1])
print(f"Processed and saved: {output_path}")
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
process_directory(sys.argv[1])
else:
print("Usage: python script.py <directory>")
Part 2: GUI Program to Adjust Volume
We'll use tkinter for the GUI and pydub for audio processing.
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter.ttk import Progressbar
from pydub import AudioSegment
import os
import threading
class AudioProcessor:
def __init__(self):
self.files = []
def change_volume(self, sound, change_in_db):
return sound.apply_gain(change_in_db)
def process_files(self, change_in_db, output_dir):
total_files = len(self.files)
processed_files = 0
for file in self.files:
try:
sound = AudioSegment.from_file(file)
adjusted_sound = self.change_volume(sound, change_in_db)
output_path = os.path.join(output_dir, os.path.basename(file))
adjusted_sound.export(output_path, format=file.split('.')[-1])
processed_files += 1
self.progress_var.set((processed_files / total_files) * 100)
except Exception as e:
messagebox.showerror("Error", f"Failed to process {file}: {e}")
messagebox.showinfo("Success", "All files have been processed.")
class Application(tk.Tk):
def __init__(self):
super().__init__()
self.title("Audio Volume Adjuster")
self.geometry("400x300")
self.processor = AudioProcessor()
self.label = tk.Label(self, text="Drag and drop files here")
self.label.pack(pady=20)
self.label.drop_target_register(DND_FILES)
self.label.dnd_bind('<<Drop>>', self.drop)
self.volume_var = tk.StringVar(value="0")
self.volume_entry = tk.Entry(self, textvariable=self.volume_var)
self.volume_entry.pack(pady=10)
self.process_button = tk.Button(self, text="Process Files", command=self.process_files)
self.process_button.pack(pady=10)
self.progress_var = tk.DoubleVar()
self.progress_bar = Progressbar(self, variable=self.progress_var, maximum=100)
self.progress_bar.pack(pady=10)
def drop(self, event):
files = event.data.strip('{}').split(' ')
self.processor.files = [f for f in files if os.path.isfile(f)]
self.label.config(text=f"{len(self.processor.files)} files dropped")
def process_files(self):
try:
change_in_db = float(self.volume_var.get())
except ValueError:
messagebox.showerror("Error", "Please enter a valid number for volume change.")
return
output_dir = filedialog.askdirectory(title="Select Output Directory")
if not output_dir:
return
def run_processing():
self.processor.process_files(change_in_db, output_dir)
threading.Thread(target=run_processing).start()
if __name__ == "__main__":
app = Application()
app.mainloop()
Instructions
-
Remove 0dB Intros and Outros:
- Save the first script as
trim_audio.py.
- Run it using
python trim_audio.py <directory>, replacing <directory> with the path to your audio files.
-
GUI for Volume Adjustment:
- Save the second script as
volume_adjuster.py.
- Run it using
python volume_adjuster.py.
- Drag and drop your audio files onto the GUI.
- Enter the desired volume change in dB.
- Select an output directory and click "Process Files".
These scripts should help you manage your audio files effectively.