-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioTrackExtractor.py
More file actions
101 lines (76 loc) · 3.88 KB
/
AudioTrackExtractor.py
File metadata and controls
101 lines (76 loc) · 3.88 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Audio Track Extractor by Aggermor
# Version 1.0
import os
import subprocess
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
def extract_audio_tracks(video_file, output_dir):
command = ['ffprobe', '-v', 'error', '-select_streams', 'a', '-show_entries', 'stream=index', '-of', 'csv=p=0', video_file]
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
num_tracks = len(result.stdout.decode().split('\n')) - 1
overwrite_all = None
existing_files = []
for i in range(num_tracks):
output_file = os.path.join(output_dir, f"audio_track_{i}.mp3")
if os.path.exists(output_file):
existing_files.append(output_file)
if existing_files and overwrite_all is not True:
if overwrite_all is None:
result = messagebox.askyesnocancel("Files exist", "Some files already exist. Do you want to overwrite them?", icon='warning')
if result is None: # Cancel
return
elif result: # Yes
overwrite_all = messagebox.askyesno("Apply to all", "Apply this action to all existing files?", icon='info')
if not overwrite_all: # No
return
for i in range(num_tracks):
output_file = os.path.join(output_dir, f"audio_track_{i}.mp3")
if os.path.exists(output_file) and overwrite_all != True:
continue
command = ['ffmpeg', '-y', '-i', video_file, '-map', f'0:a:{i}', output_file]
subprocess.run(command)
return True
def main():
root = tk.Tk()
root.title("Audio Extractor")
video_file = tk.StringVar()
output_dir = tk.StringVar()
def browse_file():
filename = filedialog.askopenfilename(filetypes=[("MKV files", "*.mkv")])
video_file.set(filename)
def browse_folder():
foldername = filedialog.askdirectory()
output_dir.set(foldername)
def start_extraction():
if video_file.get() and output_dir.get():
success = extract_audio_tracks(video_file.get(), output_dir.get())
if success:
messagebox.showinfo("Extraction Complete", "All audio tracks extracted into separate files.")
else:
messagebox.showinfo("Extraction Cancelled", "The audio track extraction was cancelled.")
button_width = 20
title_label = tk.Label(root, text="Audio Track Extractor\nby Aggermor", font=("Arial", 12))
title_label.grid(row=0, column=0, columnspan=2)
browse_file_button = tk.Button(root, text="Browse Video File", command=browse_file, anchor='w', width=button_width)
browse_file_button.grid(row=1, column=0, sticky='w')
video_file_label = tk.Label(root, textvariable=video_file, anchor='w')
video_file_label.grid(row=1, column=1, sticky='w')
browse_folder_button = tk.Button(root, text="Browse Destination Folder", command=browse_folder, anchor='w', width=button_width)
browse_folder_button.grid(row=2, column=0, sticky='w')
output_dir_label = tk.Label(root, textvariable=output_dir, anchor='w')
output_dir_label.grid(row=2, column=1, sticky='w')
convert_button = tk.Button(root, text="Convert", command=start_extraction, state=tk.DISABLED, anchor='w', width=button_width)
convert_button.grid(row=3, column=0, sticky='w')
def check_convert_button_state(*args):
if video_file.get() and output_dir.get():
convert_button.config(state=tk.NORMAL)
else:
convert_button.config(state=tk.DISABLED)
video_file.trace("w", check_convert_button_state)
output_dir.trace("w", check_convert_button_state)
exit_button = tk.Button(root, text="Exit", command=root.destroy, anchor='w', width=button_width)
exit_button.grid(row=4, column=0, sticky='w')
root.mainloop()
if __name__ == "__main__":
main()