-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMR.py
274 lines (179 loc) · 8.69 KB
/
MR.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# https://pytorch.org/get-started/previous-versions/
# https://pypi.org/project/audio-separator/
import os.path
import tkinter.messagebox
import os
import ffmpeg
import sys
with open('mode.txt', 'r') as f:
mode = f.read()
# Dynamically insert the path based on the mode
sys.path.insert(1, f'./{mode}separator/')
from audio_separator import separator
import customtkinter
from tkinter import filedialog
from tkinter import *
from threading import Thread
import tempfile
import logging
import webbrowser
import subprocess
from io import StringIO
from sympy.abc import lamda
temp_dir = tempfile.TemporaryDirectory()
def unload(pkg):
pkg_dir = os.path.abspath(os.path.dirname(pkg.__file__))
def _is_part_of_pkg(module_):
mod_path = getattr(module_, "__file__", os.sep)
mod_dir = os.path.abspath(os.path.dirname(mod_path))
return mod_dir.startswith(pkg_dir)
to_unload = [name for name, module in sys.modules.items() if _is_part_of_pkg(module)]
for name in to_unload:
sys.modules.pop(name)
class StdoutRedirector(object):
def __init__(self, text_area):
self.text_area = text_area
def write(self, str):
global f
f = open('outputraw.txt', 'a', encoding='unicode-escape')
try:
if(rf'{str[0]}' == '\r'):
self.text_area.delete("end-1l", "end")
self.text_area.insert(END, '\n')
except:
pass
self.text_area.insert(END, str)
self.text_area.see(END)
def flush(self):
pass
customtkinter.set_appearance_mode('dark')
customtkinter.set_default_color_theme('dark-blue')
enableOpenGPUWindow = True
GPUWindowEnableClosing = True
root = customtkinter.CTk()
root.geometry(f"{1000}x{600}+{int((root.winfo_screenwidth() - 1000) / 2)}+{int((root.winfo_screenheight() - 600) / 2)}")
root.resizable(0, 0)
root.title('Music Remover')
# root.after(201, lambda :root.iconbitmap("./youtubeWhiteLogo.ico"))
frame = customtkinter.CTkFrame(master=root)
frame.pack(pady=20, padx=60, fill="both", expand=False)
def removeMusic(videoFile, outputLocation):
global enableOpenGPUWindow
enableOpenGPUWindow = False
# Intialize the separator
Separator = separator.Separator(output_single_stem='Vocals', output_dir=temp_dir.name,
log_formatter=logging.Formatter('%(message)s'))
Separator.load_model('UVR-MDX-NET-Inst_HQ_3.onnx')
# Process audio
output_files = Separator.separate(videoFile)
# originalVideoName_withoutmusic.originalExtension
fileName = fr"{os.path.splitext(os.path.basename(videoFile))[0]}_withoutmusic{os.path.splitext(os.path.basename(videoFile))[1]}"
# outputlocation/fileName
output = os.path.join(outputLocation, fileName) # Replace with your desired output file
# Inputting video and audio
video_stream = ffmpeg.input(videoFile)
audio_stream = ffmpeg.input(os.path.join(temp_dir.name, output_files[0]))
print("Combining audio and video")
(
ffmpeg
.output(video_stream['v'], audio_stream['a'], output, vcodec='copy')
.global_args('-loglevel', 'quiet')
.run(overwrite_output=True)
)
print("Done")
enableOpenGPUWindow = True
def browse_button_output():
guiOutputLocation.delete(0, END)
guiOutputLocation.insert(0, filedialog.askdirectory())
def browse_button_input():
# Allow user to select a directory and store it in global var
# called folder_path
guiVideoInput.delete(0, END)
guiVideoInput.insert(0, filedialog.askopenfilename())
def installGPULibraries(textBox, window):
global msg
global GPUWindowEnableClosing
GPUWindowEnableClosing = False
p = subprocess.Popen(
".\python.exe Scripts\pip.exe install torch==2.4.1 torchvision==0.19.1 torchaudio==2.4.1 --index-url https://download.pytorch.org/whl/cu124 -t .\\GPUseparator".split(),
stdout=subprocess.PIPE, bufsize=1, text=True)
while p.poll() is None:
msg = p.stdout.readline().strip() # read a line from the process output
if msg:
window.event_generate("<<Foo>>", when="tail")
p = subprocess.Popen(".\python.exe Scripts\pip.exe install audio-separator[gpu]==0.28.5 -t .\\GPUseparator".split()
,stdout=subprocess.PIPE, bufsize=1, text=True)
while p.poll() is None:
msg = p.stdout.readline().strip() # read a line from the process output
if msg:
window.event_generate("<<Foo>>", when="tail")
with open('mode.txt', 'w') as f:
f.write('GPU')
GPUWindowEnableClosing = True
def insertTextBox(textBox):
textBox.insert(END, f"{msg}\n")
textBox.see('end')
def closingGPUWindow(window, textBox):
if (GPUWindowEnableClosing):
window.destroy()
else:
tkinter.messagebox.showerror('ERROR', "Please wait for the current process to finish")
label = customtkinter.CTkLabel(master=frame, text="Music Remover", font=("Arial", 24))
label.pack(pady=20, padx=10)
guiVideoInput = customtkinter.CTkEntry(master=frame, width=300, placeholder_text="Video Location")
guiVideoInput.pack(pady=5, padx=10)
guiVideoInputButton = customtkinter.CTkButton(master=frame, height=30, text="Select Video Location",
command=browse_button_input)
guiVideoInputButton.pack(pady=5, padx=0)
guiOutputLocation = customtkinter.CTkEntry(master=frame, width=300, placeholder_text="Output Location")
guiOutputLocation.pack(pady=12, padx=10)
guiOutputLocationButton = customtkinter.CTkButton(master=frame, height=30, text="Select Output Location",
command=browse_button_output)
guiOutputLocationButton.pack(pady=5, padx=0)
def guiStart():
t = Thread(target=removeMusic, args=(str(guiVideoInput.get()), str(guiOutputLocation.get())))
t.start()
guiStartButton = customtkinter.CTkButton(master=frame, height=50, text="Start", command=guiStart)
guiStartButton.pack(pady=10, padx=10)
progressText = StringVar()
guiProgressBarText = customtkinter.CTkTextbox(master=frame, font=("Arial", 10), width=750, height=150)
guiProgressBarText.bind("<Key>", lambda e: "break")
guiProgressBarText.pack(pady=20, padx=20)
def guiDownload(textBox, window):
t = Thread(target=installGPULibraries, args=(textBox, window))
t.start()
def guiOpenGPUWindow():
global GPUWindowEnableClosing
GPUWindowEnableClosing = True
GPUWindow = customtkinter.CTkToplevel(root)
GPUWindow.focus_force()
GPUWindow.grab_set()
GPUWindow.geometry(
f"{500}x{400}+{int((root.winfo_screenwidth() - 500) / 2)}+{int((root.winfo_screenheight() - 400) / 2)}")
GPUWindow.title("Using GPU")
GPUWindow.resizable(0, 0)
GPUWindowLabel = customtkinter.CTkLabel(master=GPUWindow, text="How to use the GPU", font=("Arial", 20))
GPUWindowLabel.pack(pady=10, padx=20, side=TOP)
GPUWindowRequirements = customtkinter.CTkLabel(master=GPUWindow,
text="Important notes:\nNvidia GTX 1060 6GB is the minimum requirement for GPU conversions.\nNvidia GPUs with at least 8GBs of V-RAM are recommended.",
font=("Arial", 12))
GPUWindowRequirements.pack(pady=10, padx=20, side=TOP)
GPUWindowCuda = customtkinter.CTkButton(master=GPUWindow, width=200, height=50, text="Download CUDA 12.4",
command=lambda: webbrowser.open(
"https://developer.nvidia.com/cuda-12-4-0-download-archive"))
GPUWindowCuda.pack(pady=0, padx=20)
GPUWindowTextBox = customtkinter.CTkTextbox(master=GPUWindow, font=("Arial", 10), width=400, height=100)
GPUWindowTextBox.bind("<Key>", lambda event: "break")
GPUWindowTextBox.pack(pady=20, padx=20, side=BOTTOM)
GPUWindow.bind("<<Foo>>", lambda event: insertTextBox(GPUWindowTextBox))
GPUWindowLibraries = customtkinter.CTkButton(master=GPUWindow, width=200, height=50,
text="Download GPU Python Libraries",
command=lambda: guiDownload(GPUWindowTextBox, GPUWindow))
GPUWindowLibraries.pack(pady=20, padx=20)
GPUWindow.protocol("WM_DELETE_WINDOW", lambda: closingGPUWindow(GPUWindow, GPUWindowTextBox))
guiGPUHelpButton = customtkinter.CTkButton(master=frame, height=100, text="How to Use GPU", command=lambda: guiOpenGPUWindow() if enableOpenGPUWindow else tkinter.messagebox.showerror(
'ERROR', "Please wait for the current process to finish"))
guiGPUHelpButton.pack(pady=20, padx=21)
sys.stdout = StdoutRedirector(guiProgressBarText)
sys.stderr = StdoutRedirector(guiProgressBarText)
root.mainloop()