Build Your Own Audio Editor in Python | GUI + Waveform + Pydub
Demo :
Click Video πππ
✅ Features:
-
Embed the YouTube Shorts
-
Describe code features and benefits
-
Add “Download Source Code” link if you want to share
-
Add internal links to your older Python projects
-
Share the post on LinkedIn & Reddit for more traffic
Code :
import tkinter as tk
from tkinter import filedialog
from pydub import AudioSegment
import matplotlib.pyplot as plt
import pygame
class AudioEditor:
def __init__(self, root):
self.root = root
self.root.title("FuzzuTech - Advanced Audio Editor")
self.root.geometry("920x620")
self.root.configure(bg="#1e1e1e")
self.audio = None
self.filename = None
self.is_paused = False
self.is_playing = False
self.build_ui()
def build_ui(self):
title = tk.Label(self.root, text="π§ Advanced Audio Editor", font=("Segoe UI", 22, "bold"), fg="#ffffff", bg="#1e1e1e")
title.pack(pady=20)
style = {
"font": ("Segoe UI", 12),
"bg": "#2d2d2d",
"fg": "#ffffff",
"activebackground": "#444",
"activeforeground": "#00ffcc",
"bd": 0,
"width": 25,
"height": 2
}
self.load_btn = tk.Button(self.root, text="π Load Audio", command=self.load_audio, **style)
self.load_btn.pack(pady=6)
self.play_btn = tk.Button(self.root, text="▶ Play", command=self.play_audio, state=tk.DISABLED, **style)
self.play_btn.pack(pady=6)
self.pause_btn = tk.Button(self.root, text="⏸ Pause", command=self.pause_audio, state=tk.DISABLED, **style)
self.pause_btn.pack(pady=6)
self.trim_btn = tk.Button(self.root, text="✂ Trim (0-5 sec)", command=self.trim_audio, state=tk.DISABLED, **style)
self.trim_btn.pack(pady=6)
self.volume_btn = tk.Button(self.root, text="π Volume +6dB", command=self.volume_up, state=tk.DISABLED, **style)
self.volume_btn.pack(pady=6)
self.save_btn = tk.Button(self.root, text="πΎ Save", command=self.save_audio, state=tk.DISABLED, **style)
self.save_btn.pack(pady=6)
self.visual_btn = tk.Button(self.root, text="π Visualize", command=self.plot_waveform, state=tk.DISABLED, **style)
self.visual_btn.pack(pady=6)
def load_audio(self):
file_path = filedialog.askopenfilename(filetypes=[("Audio Files", "*.mp3 *.wav *.ogg *.flac")])
if file_path:
self.audio = AudioSegment.from_file(file_path)
self.filename = file_path
self.set_state(tk.NORMAL)
self.pause_btn.config(text="⏸ Pause")
self.is_playing = False
self.is_paused = False
def play_audio(self):
if self.audio and self.filename:
try:
pygame.mixer.init()
pygame.mixer.music.load(self.filename)
pygame.mixer.music.play()
self.is_paused = False
self.is_playing = True
self.pause_btn.config(state=tk.NORMAL, text="⏸ Pause")
except Exception as e:
self.show_warning(f"Error playing audio: {e}")
else:
self.show_warning("Audio file not loaded.")
def pause_audio(self):
if not self.is_playing:
self.show_warning("No audio is playing to pause or resume.")
return
if not self.is_paused:
pygame.mixer.music.pause()
self.is_paused = True
self.pause_btn.config(text="▶ Resume")
else:
pygame.mixer.music.unpause()
self.is_paused = False
self.pause_btn.config(text="⏸ Pause")
def trim_audio(self):
if self.audio:
self.audio = self.audio[:5000]
def volume_up(self):
if self.audio:
self.audio += 6
def save_audio(self):
save_path = filedialog.asksaveasfilename(defaultextension=".mp3")
if save_path:
try:
self.audio.export(save_path, format="mp3")
except Exception as e:
self.show_warning(f"Error saving file: {e}")
def plot_waveform(self):
if self.audio:
samples = self.audio.get_array_of_samples()
plt.figure(figsize=(10, 3))
plt.plot(samples)
plt.title("Waveform")
plt.tight_layout()
plt.show()
else:
self.show_warning("No audio loaded to visualize.")
def set_state(self, state):
self.play_btn.config(state=state)
self.trim_btn.config(state=state)
self.volume_btn.config(state=state)
self.save_btn.config(state=state)
self.visual_btn.config(state=state)
self.pause_btn.config(state=state)
def show_warning(self, msg):
warning = tk.Toplevel(self.root)
warning.title("Warning")
warning.geometry("300x100")
warning.configure(bg="#1e1e1e")
tk.Label(warning, text=msg, bg="#1e1e1e", fg="yellow").pack(expand=True)
tk.Button(warning, text="OK", command=warning.destroy, bg="#444", fg="white").pack(pady=10)
if __name__ == "__main__":
root = tk.Tk()
app = AudioEditor(root)
root.mainloop()
Comments
Post a Comment