-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_operations.py
57 lines (42 loc) · 2.48 KB
/
audio_operations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from tkinter import filedialog, messagebox, Toplevel, Button, Label, Scale
from moviepy.editor import AudioFileClip
import os
class AudioOperations:
def __init__(self, controller):
self.controller = controller
def add_audio(self):
file_path = filedialog.askopenfilename(filetypes=[("Audio files", "*.mp3;*.wav")])
if file_path:
try:
self.controller.audio_file = AudioFileClip(file_path)
messagebox.showinfo("Success", f"Audio track added: {os.path.basename(file_path)}")
except Exception as e:
messagebox.showerror("Error", f"Failed to load audio file: {e}")
def trim_audio(self):
file_path = filedialog.askopenfilename(filetypes=[("Audio files", "*.mp3;*.wav")])
if file_path:
try:
audio_clip = AudioFileClip(file_path)
trim_window = Toplevel(self.controller.root)
trim_window.title("Trim Audio")
trim_window.geometry("400x300")
Label(trim_window, text="Select Start and End Points for Trimming").pack(pady=10)
start_scale = Scale(trim_window, from_=0, to=audio_clip.duration, orient="horizontal", length=300, label="Start Time (seconds)")
start_scale.pack(pady=10)
end_scale = Scale(trim_window, from_=0, to=audio_clip.duration, orient="horizontal", length=300, label="End Time (seconds)")
end_scale.set(audio_clip.duration)
end_scale.pack(pady=10)
def apply_trim():
start_time = start_scale.get()
end_time = end_scale.get()
if start_time < end_time:
trimmed_audio = audio_clip.subclip(start_time, end_time)
self.controller.audio_file = trimmed_audio
messagebox.showinfo("Success", f"Trimmed audio track added from {start_time} to {end_time} seconds")
trim_window.destroy()
else:
messagebox.showerror("Error", "End time must be greater than start time")
trim_button = Button(trim_window, text="Trim Audio", command=apply_trim)
trim_button.pack(pady=20)
except Exception as e:
messagebox.showerror("Error", f"Failed to trim audio file: {e}")