Spaces:
Running
Running
Lazarus
commited on
Commit
·
2e7384f
1
Parent(s):
14e6420
upgrade to gradio 4
Browse files- app.py +28 -25
- functions.py +140 -112
app.py
CHANGED
@@ -15,14 +15,14 @@ from functions import (
|
|
15 |
audio_sample_rates,
|
16 |
change_clipbox,
|
17 |
containers,
|
18 |
-
|
19 |
-
|
20 |
presets,
|
21 |
supported_codecs,
|
22 |
supported_presets,
|
23 |
video_codecs,
|
24 |
video_containers,
|
25 |
-
|
26 |
)
|
27 |
|
28 |
logging.basicConfig(level=logging.INFO)
|
@@ -31,17 +31,17 @@ logging.basicConfig(level=logging.INFO)
|
|
31 |
logging.info(msg=f"{video_containers}")
|
32 |
|
33 |
|
34 |
-
def convert(file: _TemporaryFileWrapper,
|
35 |
stderr = ""
|
36 |
# stdout=""
|
37 |
output_file = ""
|
38 |
# video = ""
|
39 |
ffmpeg = FFmpeg()
|
40 |
try:
|
41 |
-
logging.info(
|
42 |
new_name, _ = file.name.split(".")
|
43 |
-
logging.info(
|
44 |
-
output_file = f"{new_name}1.{
|
45 |
ffmpeg = FFmpeg(
|
46 |
inputs={file.name: None},
|
47 |
outputs={output_file: ffmpeg_commands.commands.split()},
|
@@ -58,11 +58,11 @@ def convert(file: _TemporaryFileWrapper, options: str, state):
|
|
58 |
except Exception as e:
|
59 |
stderr = e
|
60 |
output = f"{stderr}"
|
61 |
-
return [None, None, None, output]
|
62 |
|
63 |
-
|
64 |
|
65 |
-
return [output_file, output_file, output_file, output,
|
66 |
|
67 |
|
68 |
with gr.Blocks(css="./styles.css") as demo:
|
@@ -87,7 +87,10 @@ with gr.Blocks(css="./styles.css") as demo:
|
|
87 |
interactive=True,
|
88 |
)
|
89 |
stop_time = gr.Textbox(
|
90 |
-
label="Stop Time:",
|
|
|
|
|
|
|
91 |
)
|
92 |
with gr.Row():
|
93 |
clearBtn = gr.Button("Clear")
|
@@ -110,11 +113,9 @@ with gr.Blocks(css="./styles.css") as demo:
|
|
110 |
|
111 |
resetFormat = Clear(inputs, inputs_clip)
|
112 |
print(inputs_clip.children)
|
113 |
-
print(resetFormat)
|
114 |
state = gr.State()
|
115 |
-
clearBtn.click(
|
116 |
-
resetFormat.clear, inputs=resetFormat(), outputs=resetFormat()
|
117 |
-
)
|
118 |
convertBtn.click(
|
119 |
convert,
|
120 |
inputs=[file_input, options, state],
|
@@ -139,8 +140,8 @@ with gr.Blocks(css="./styles.css") as demo:
|
|
139 |
with gr.Row(elem_id="button"):
|
140 |
with gr.Column():
|
141 |
clearBtn = gr.Button("Clear")
|
142 |
-
|
143 |
-
|
144 |
|
145 |
with gr.Tab("Audio"):
|
146 |
with gr.Row() as audio_inputs:
|
@@ -174,12 +175,14 @@ with gr.Blocks(css="./styles.css") as demo:
|
|
174 |
gr.Markdown("## Video")
|
175 |
# equal_height=True
|
176 |
with gr.Row(equal_height=True) as filter_inputs:
|
177 |
-
for i in
|
178 |
# print(i.values())
|
179 |
# values = list(i.values())
|
180 |
values = list(i.values())[0]
|
181 |
choices = [j for lst in values for j in [lst.get("name")]]
|
182 |
-
a = gr.Dropdown(
|
|
|
|
|
183 |
gr.Markdown("## Audio")
|
184 |
with gr.Row(elem_id="acontrast") as filter_inputs_1:
|
185 |
acontrastSlider = gr.Slider(label="Acontrast", elem_id="acontrast")
|
@@ -194,11 +197,11 @@ with gr.Blocks(css="./styles.css") as demo:
|
|
194 |
ffmpeg_commands = CommandBuilder(
|
195 |
inputs_clip, video_inputs, audio_inputs, filter_inputs, filter_inputs_1
|
196 |
)
|
197 |
-
|
198 |
|
199 |
# demo.load(fn=ffmpeg_commands.reset, inputs=[], outputs=[])
|
200 |
clip.change(fn=change_clipbox, inputs=clip, outputs=[start_time, stop_time])
|
201 |
-
pprint(ffmpeg_commands.commands)
|
202 |
ffmpeg_commands.update(output_textbox)
|
203 |
# file_input.change(fn=updateOutput,inputs=file_input,outputs=output_textbox)
|
204 |
|
@@ -206,25 +209,25 @@ with gr.Blocks(css="./styles.css") as demo:
|
|
206 |
# options.change(mediaChange,[options],[media_output_audio,media_output_video])
|
207 |
# video_button.click(fn=videoChange,inputs=media_output_file,outputs=media_output_video)
|
208 |
audio_button.click(
|
209 |
-
|
210 |
[audio_button, state],
|
211 |
[media_output_audio, media_output_video, media_output_file],
|
212 |
)
|
213 |
video_button.click(
|
214 |
-
|
215 |
[video_button, state],
|
216 |
[media_output_audio, media_output_video, media_output_file],
|
217 |
)
|
218 |
# media_output_audio.change(lambda x:gr.update(value=x),[media_output_audio],[media_output_video])
|
219 |
file_button.click(
|
220 |
-
|
221 |
[file_button, state],
|
222 |
[media_output_audio, media_output_video, media_output_file],
|
223 |
)
|
224 |
"""Video Tab change functions"""
|
225 |
video_options.change(supported_presets, [video_options], [preset_options])
|
226 |
"""Audio Tab change functions"""
|
227 |
-
audio_bitrate.change(
|
228 |
|
229 |
if __name__ == "__main__":
|
230 |
demo.launch(debug=True)
|
|
|
15 |
audio_sample_rates,
|
16 |
change_clipbox,
|
17 |
containers,
|
18 |
+
set_custom_bitrate,
|
19 |
+
media_change,
|
20 |
presets,
|
21 |
supported_codecs,
|
22 |
supported_presets,
|
23 |
video_codecs,
|
24 |
video_containers,
|
25 |
+
VF,
|
26 |
)
|
27 |
|
28 |
logging.basicConfig(level=logging.INFO)
|
|
|
31 |
logging.info(msg=f"{video_containers}")
|
32 |
|
33 |
|
34 |
+
def convert(file: _TemporaryFileWrapper, container_format: str, new_state: str):
|
35 |
stderr = ""
|
36 |
# stdout=""
|
37 |
output_file = ""
|
38 |
# video = ""
|
39 |
ffmpeg = FFmpeg()
|
40 |
try:
|
41 |
+
logging.info("File name: %s", file.name)
|
42 |
new_name, _ = file.name.split(".")
|
43 |
+
logging.info("New filename:%s", new_name)
|
44 |
+
output_file = f"{new_name}1.{container_format.lower()}"
|
45 |
ffmpeg = FFmpeg(
|
46 |
inputs={file.name: None},
|
47 |
outputs={output_file: ffmpeg_commands.commands.split()},
|
|
|
58 |
except Exception as e:
|
59 |
stderr = e
|
60 |
output = f"{stderr}"
|
61 |
+
return [None, None, None, output, new_state]
|
62 |
|
63 |
+
new_state = output_file
|
64 |
|
65 |
+
return [output_file, output_file, output_file, output, new_state]
|
66 |
|
67 |
|
68 |
with gr.Blocks(css="./styles.css") as demo:
|
|
|
87 |
interactive=True,
|
88 |
)
|
89 |
stop_time = gr.Textbox(
|
90 |
+
label="Stop Time:",
|
91 |
+
placeholder="00:00",
|
92 |
+
visible=False,
|
93 |
+
interactive=True,
|
94 |
)
|
95 |
with gr.Row():
|
96 |
clearBtn = gr.Button("Clear")
|
|
|
113 |
|
114 |
resetFormat = Clear(inputs, inputs_clip)
|
115 |
print(inputs_clip.children)
|
116 |
+
# print(resetFormat)
|
117 |
state = gr.State()
|
118 |
+
clearBtn.click(resetFormat.clear, resetFormat(), resetFormat())
|
|
|
|
|
119 |
convertBtn.click(
|
120 |
convert,
|
121 |
inputs=[file_input, options, state],
|
|
|
140 |
with gr.Row(elem_id="button"):
|
141 |
with gr.Column():
|
142 |
clearBtn = gr.Button("Clear")
|
143 |
+
videoReset = Clear(video_inputs)
|
144 |
+
clearBtn.click(videoReset.clear, videoReset(), videoReset())
|
145 |
|
146 |
with gr.Tab("Audio"):
|
147 |
with gr.Row() as audio_inputs:
|
|
|
175 |
gr.Markdown("## Video")
|
176 |
# equal_height=True
|
177 |
with gr.Row(equal_height=True) as filter_inputs:
|
178 |
+
for i in VF:
|
179 |
# print(i.values())
|
180 |
# values = list(i.values())
|
181 |
values = list(i.values())[0]
|
182 |
choices = [j for lst in values for j in [lst.get("name")]]
|
183 |
+
a = gr.Dropdown(
|
184 |
+
label=str(list(i.keys())[0]), choices=choices, value=choices[0]
|
185 |
+
)
|
186 |
gr.Markdown("## Audio")
|
187 |
with gr.Row(elem_id="acontrast") as filter_inputs_1:
|
188 |
acontrastSlider = gr.Slider(label="Acontrast", elem_id="acontrast")
|
|
|
197 |
ffmpeg_commands = CommandBuilder(
|
198 |
inputs_clip, video_inputs, audio_inputs, filter_inputs, filter_inputs_1
|
199 |
)
|
200 |
+
ffmpeg_commands.do()
|
201 |
|
202 |
# demo.load(fn=ffmpeg_commands.reset, inputs=[], outputs=[])
|
203 |
clip.change(fn=change_clipbox, inputs=clip, outputs=[start_time, stop_time])
|
204 |
+
# pprint(ffmpeg_commands.commands)
|
205 |
ffmpeg_commands.update(output_textbox)
|
206 |
# file_input.change(fn=updateOutput,inputs=file_input,outputs=output_textbox)
|
207 |
|
|
|
209 |
# options.change(mediaChange,[options],[media_output_audio,media_output_video])
|
210 |
# video_button.click(fn=videoChange,inputs=media_output_file,outputs=media_output_video)
|
211 |
audio_button.click(
|
212 |
+
media_change,
|
213 |
[audio_button, state],
|
214 |
[media_output_audio, media_output_video, media_output_file],
|
215 |
)
|
216 |
video_button.click(
|
217 |
+
media_change,
|
218 |
[video_button, state],
|
219 |
[media_output_audio, media_output_video, media_output_file],
|
220 |
)
|
221 |
# media_output_audio.change(lambda x:gr.update(value=x),[media_output_audio],[media_output_video])
|
222 |
file_button.click(
|
223 |
+
media_change,
|
224 |
[file_button, state],
|
225 |
[media_output_audio, media_output_video, media_output_file],
|
226 |
)
|
227 |
"""Video Tab change functions"""
|
228 |
video_options.change(supported_presets, [video_options], [preset_options])
|
229 |
"""Audio Tab change functions"""
|
230 |
+
audio_bitrate.change(set_custom_bitrate, [audio_bitrate], [custom_bitrate])
|
231 |
|
232 |
if __name__ == "__main__":
|
233 |
demo.launch(debug=True)
|
functions.py
CHANGED
@@ -10,8 +10,8 @@ import gradio as gr
|
|
10 |
from gradio.components import Component
|
11 |
|
12 |
|
13 |
-
def parse(param:
|
14 |
-
with open(param) as file:
|
15 |
return json.load(file)
|
16 |
|
17 |
|
@@ -38,8 +38,8 @@ audio_sample_rates = [i.get("name") for i in data["sampleRates"]]
|
|
38 |
# dejudder=[i.get("name") for i in data["dejudder"]]
|
39 |
# denoise=[i.get("name") for i in data["denoise"]]
|
40 |
# deinterlace=[i.get("name") for i in data["deinterlace"]]
|
41 |
-
|
42 |
-
|
43 |
|
44 |
presets = [i.get("name") for i in data["presets"]]
|
45 |
profiles = [i.get("name") for i in data["profiles"]]
|
@@ -64,21 +64,28 @@ class CommandBuilder:
|
|
64 |
def do(self, *inputs, **kwds):
|
65 |
for comp in self._component:
|
66 |
if comp.label is not None:
|
67 |
-
self.changefunc(comp,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
def reset(self):
|
70 |
-
self.
|
71 |
self.commands = ""
|
72 |
self.vf, self.af, self.extra = ([] for _ in range(3))
|
73 |
|
74 |
-
def __init__(self, *inputs: gr.
|
75 |
"""
|
76 |
Parameters:
|
77 |
*inputs: A tuple of layout blocks containing components(Textbox,Button...).
|
78 |
"""
|
79 |
|
80 |
-
self.
|
81 |
-
self.
|
82 |
# state=gr.Variable()
|
83 |
# state2=gr.Variable()
|
84 |
|
@@ -89,52 +96,55 @@ class CommandBuilder:
|
|
89 |
return None
|
90 |
for i in inputs:
|
91 |
self._component += self._get_component_instance(i)
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
def changefunc(self,
|
|
|
103 |
label, *_ = (
|
104 |
-
|
105 |
-
if
|
106 |
-
else ""
|
|
|
|
|
107 |
)
|
108 |
label += "".join(_).title()
|
109 |
key = newoutputMap.get(label)
|
110 |
lst_extra, vf, af = ([] for _ in range(3))
|
111 |
-
if
|
112 |
-
self.
|
113 |
-
self.
|
114 |
-
self.
|
115 |
-
for val in self.
|
116 |
if val == "vf":
|
117 |
-
vf = self.
|
118 |
vf = ",".join(list(vf))
|
119 |
elif val == "af":
|
120 |
-
af = self.
|
121 |
af = ",".join(list(af))
|
122 |
-
pass
|
123 |
else:
|
124 |
-
lst_extra.extend([val, self.
|
125 |
|
126 |
else:
|
127 |
-
self.
|
128 |
-
self.
|
129 |
-
self.
|
130 |
self.vf = f"-vf '{vf}'" if vf else ""
|
131 |
self.af = f"-af '{af}'" if af else ""
|
132 |
self.extra = " ".join(lst_extra)
|
133 |
self.commands = f"{self.vf} {self.af} {self.extra}"
|
134 |
|
135 |
-
print(self.vf, self.af, self.extra)
|
|
|
136 |
|
137 |
-
def
|
138 |
"""Sets Video filters
|
139 |
|
140 |
Args:
|
@@ -144,13 +154,13 @@ class CommandBuilder:
|
|
144 |
if newoutputMap["vf"].get(label):
|
145 |
key = newoutputMap["vf"].get(label)
|
146 |
if label in ["deinterlace", "denoise"]:
|
147 |
-
value = "_".join(
|
148 |
arg = key.get(value, None)
|
149 |
-
self.
|
150 |
else:
|
151 |
-
self.
|
152 |
|
153 |
-
def
|
154 |
"""Sets Extra filters
|
155 |
Args:
|
156 |
label : label of components
|
@@ -159,38 +169,46 @@ class CommandBuilder:
|
|
159 |
if newoutputMap.get(label):
|
160 |
key = newoutputMap.get(label)
|
161 |
if label in ["video", "audio"]:
|
162 |
-
|
|
|
|
|
|
|
163 |
print(value)
|
164 |
-
self.
|
165 |
elif label in ["startTime", "stopTime"]:
|
166 |
-
self.
|
167 |
else:
|
168 |
value = "".join(
|
169 |
[
|
170 |
i.get("value", "None")
|
171 |
for i in data.get(label)
|
172 |
-
if i.get("name", None) ==
|
173 |
]
|
174 |
)
|
175 |
-
self.
|
176 |
|
177 |
-
def
|
178 |
-
"""Sets
|
179 |
Args:
|
180 |
label : label of components
|
181 |
newValue : value of component
|
182 |
"""
|
183 |
if newoutputMap["af"].get(label):
|
184 |
-
value = int(
|
185 |
arg = f"{label}={value}"
|
186 |
-
self.
|
187 |
|
188 |
-
def update(self,
|
189 |
for comp in self._component:
|
190 |
-
print(comp, "comp")
|
191 |
-
comp.change(
|
|
|
|
|
|
|
|
|
|
|
192 |
|
193 |
-
def _get_component_instance(self, inputs: gr.
|
194 |
"""
|
195 |
returns components present in a layout block
|
196 |
Parameters:
|
@@ -199,45 +217,55 @@ class CommandBuilder:
|
|
199 |
res = []
|
200 |
for i in inputs.children:
|
201 |
# print(i,hasattr(i,"children"))
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
# res.append(gr.components.get_component_instance(i,render=True))
|
|
|
|
|
204 |
res += [gr.components.get_component_instance(i, render=True)]
|
205 |
-
|
206 |
elif hasattr(i, "children"):
|
|
|
207 |
res += self._get_component_instance(i)
|
208 |
# print(res)
|
209 |
return res
|
210 |
# return [gr.components.get_component_instance(i, render=True) for i in inputs.children if not hasattr(i, "children")]
|
211 |
|
212 |
-
def
|
213 |
-
value = self.
|
214 |
filters = newoutputMap.get(options, None)
|
215 |
arg = ""
|
216 |
if options in ["deinterlace", "denoise"]:
|
217 |
value = "_".join(value.lower().split())
|
218 |
arg = filters.get(value, None)
|
219 |
# self.vf.append(arg)
|
220 |
-
self.
|
221 |
return True
|
222 |
if options in ["deband", "deflicker", "deshake", "dejudder"]:
|
223 |
arg = filters
|
224 |
-
self.
|
225 |
return True
|
226 |
|
227 |
-
return
|
228 |
|
229 |
-
def
|
230 |
-
value = self.
|
231 |
if options in ["acontrast"]:
|
232 |
value = int(value) / 100
|
233 |
arg = f"{options}={value}"
|
234 |
|
235 |
-
self.
|
236 |
return True
|
237 |
return
|
238 |
|
239 |
-
def
|
240 |
-
value = self.
|
241 |
filters = newoutputMap.get(options, None)
|
242 |
if options in ["video", "audio"]:
|
243 |
value = "".join(
|
@@ -248,11 +276,11 @@ class CommandBuilder:
|
|
248 |
]
|
249 |
)
|
250 |
arg = f"{filters} {value}"
|
251 |
-
self.
|
252 |
return True
|
253 |
elif data.get(options) is None:
|
254 |
arg = f"{filters} {value}"
|
255 |
-
self.
|
256 |
return True
|
257 |
elif options != "clip":
|
258 |
value = "".join(
|
@@ -263,26 +291,26 @@ class CommandBuilder:
|
|
263 |
]
|
264 |
)
|
265 |
arg = f"{filters} {value}"
|
266 |
-
self.
|
267 |
|
268 |
def build(self):
|
269 |
-
for i in self.
|
270 |
-
if self.
|
271 |
continue
|
272 |
-
elif self.
|
273 |
continue
|
274 |
else:
|
275 |
-
self.
|
276 |
lst_extra, vf, af = ([] for _ in range(3))
|
277 |
-
for val in self.
|
278 |
if val == "vf":
|
279 |
-
vf = self.
|
280 |
vf = ",".join(list(vf))
|
281 |
elif val == "af":
|
282 |
-
af = self.
|
283 |
af = ",".join(list(af))
|
284 |
else:
|
285 |
-
lst_extra.append(self.
|
286 |
# print(lst_extra, "temp x")
|
287 |
# if vf:self.vf=f"-vf '{vf}'"
|
288 |
# if af:self.af=f"-af '{af}'"
|
@@ -291,25 +319,25 @@ class CommandBuilder:
|
|
291 |
self.extra = " ".join(lst_extra)
|
292 |
self.commands = f"{self.vf} {self.af} {self.extra}"
|
293 |
|
294 |
-
def startfunc(self,
|
295 |
label, *_ = (
|
296 |
-
|
297 |
-
if not isinstance(
|
298 |
-
else "".join(
|
299 |
)
|
300 |
label += "".join(_).title()
|
301 |
-
if
|
302 |
-
self.
|
303 |
-
self.
|
304 |
-
self.
|
305 |
else:
|
306 |
-
self.
|
307 |
-
self.
|
308 |
-
self.
|
309 |
# self.formatOutputDict["vf"].pop(label, "Key is None or similar")
|
310 |
# self.formatOutputDict["af"].pop(label, "Key is None or similar")
|
311 |
# self.formatOutputDict.pop(label, "Key is None or similar")
|
312 |
-
print(self.
|
313 |
self.build()
|
314 |
|
315 |
|
@@ -341,7 +369,7 @@ class CommandBuilder:
|
|
341 |
# return [no_,no_]
|
342 |
|
343 |
|
344 |
-
def
|
345 |
"""
|
346 |
Allows playing the media in various options,
|
347 |
Video, Audio or File
|
@@ -353,11 +381,11 @@ def mediaChange(option: str, state) -> List[Component]:
|
|
353 |
List[Component]: list of toggled output components to display
|
354 |
"""
|
355 |
print(state, "state")
|
356 |
-
ops = {"Audio": gr.
|
357 |
-
ops2 = {"Video": gr.
|
358 |
-
ops3 = {"File": gr.
|
359 |
|
360 |
-
def chosen(x):
|
361 |
return x.get(option, gr.update(visible=False))
|
362 |
|
363 |
return [chosen(ops), chosen(ops2), chosen(ops3)]
|
@@ -391,7 +419,7 @@ def mediaChange(option: str, state) -> List[Component]:
|
|
391 |
# return clear_func(input)
|
392 |
|
393 |
|
394 |
-
def
|
395 |
"""
|
396 |
Toggle a component for custom Audio Quality
|
397 |
visible/none
|
@@ -402,12 +430,11 @@ def customBitrate(choice: int) -> Component:
|
|
402 |
Component: component toggle state
|
403 |
"""
|
404 |
if choice == "Custom":
|
405 |
-
return gr.
|
406 |
-
|
407 |
-
return gr.update(visible=False, value=0)
|
408 |
|
409 |
|
410 |
-
def supported_codecs(
|
411 |
"""
|
412 |
Changes video and audio components with appropriate
|
413 |
options according to passed format
|
@@ -418,38 +445,38 @@ def supported_codecs(format: str) -> List[Component]:
|
|
418 |
Returns:
|
419 |
List[Component]: list of components with updated choices
|
420 |
"""
|
421 |
-
if
|
422 |
-
|
423 |
video_lst = [
|
424 |
val.get("value")
|
425 |
for val in data["codecs"]["video"]
|
426 |
-
if val.get("supported") is None or
|
427 |
]
|
428 |
audio_lst = [
|
429 |
val.get("value")
|
430 |
for val in data["codecs"]["audio"]
|
431 |
-
if val.get("supported") is None or
|
432 |
]
|
433 |
return [gr.Dropdown(choices=video_lst), gr.Dropdown(choices=audio_lst)]
|
434 |
|
435 |
|
436 |
-
def supported_presets(
|
437 |
"""
|
438 |
Changes presets component with appropriate
|
439 |
options according to passed format
|
440 |
Args:
|
441 |
-
format: passed media codec (
|
442 |
|
443 |
Returns:
|
444 |
Component: component with updated choice list (video codecs)
|
445 |
"""
|
446 |
-
if
|
447 |
-
|
448 |
-
print(
|
449 |
video_lst = [
|
450 |
val.get("name")
|
451 |
for val in data["presets"]
|
452 |
-
if val.get("supported") is None or
|
453 |
]
|
454 |
return gr.Dropdown(choices=video_lst)
|
455 |
|
@@ -515,24 +542,25 @@ class Clear(CommandBuilder):
|
|
515 |
def __repr__(self):
|
516 |
return self._component
|
517 |
|
518 |
-
def __init__(self, *input_component: gr.
|
519 |
"""
|
520 |
Parameters:
|
521 |
*input_component: A tuple of layout blocks containing components
|
522 |
"""
|
|
|
523 |
self._component = []
|
524 |
if input_component is not None:
|
525 |
for i in input_component:
|
526 |
# self._component += super()._get_component_instance(i)
|
527 |
self._component += self.__get_component_instance(i)
|
528 |
|
529 |
-
def __get_component_instance(self, inputs: gr.
|
530 |
# print(inputs, " class instance")
|
531 |
res = []
|
532 |
# print(*inputs.children)
|
533 |
for i in inputs.children:
|
534 |
# print(i,hasattr(i,"children"))
|
535 |
-
if not
|
536 |
# res.append(gr.components.get_component_instance(i,render=True))
|
537 |
res += [gr.components.get_component_instance(i, render=True)]
|
538 |
# print(i)
|
|
|
10 |
from gradio.components import Component
|
11 |
|
12 |
|
13 |
+
def parse(param: str) -> dict:
|
14 |
+
with open(param, encoding="utf-8") as file:
|
15 |
return json.load(file)
|
16 |
|
17 |
|
|
|
38 |
# dejudder=[i.get("name") for i in data["dejudder"]]
|
39 |
# denoise=[i.get("name") for i in data["denoise"]]
|
40 |
# deinterlace=[i.get("name") for i in data["deinterlace"]]
|
41 |
+
video_filters = ["deband", "deflicker", "deshake", "dejudder", "denoise", "deinterlace"]
|
42 |
+
VF = [{vFilter: names} for vFilter in video_filters for names in [list(data[vFilter])]]
|
43 |
|
44 |
presets = [i.get("name") for i in data["presets"]]
|
45 |
profiles = [i.get("name") for i in data["profiles"]]
|
|
|
64 |
def do(self, *inputs, **kwds):
|
65 |
for comp in self._component:
|
66 |
if comp.label is not None:
|
67 |
+
# self.changefunc(comp, comp.value)
|
68 |
+
# print(
|
69 |
+
# comp.label,
|
70 |
+
# comp,
|
71 |
+
# )
|
72 |
+
comp.change(
|
73 |
+
fn=self.changefunc, inputs=[gr.State(comp.label), comp], outputs=[]
|
74 |
+
)
|
75 |
|
76 |
def reset(self):
|
77 |
+
self.output_dict = {"vf": {}, "af": {}}
|
78 |
self.commands = ""
|
79 |
self.vf, self.af, self.extra = ([] for _ in range(3))
|
80 |
|
81 |
+
def __init__(self, *inputs: gr.Row | gr.Column) -> None:
|
82 |
"""
|
83 |
Parameters:
|
84 |
*inputs: A tuple of layout blocks containing components(Textbox,Button...).
|
85 |
"""
|
86 |
|
87 |
+
self.output_dict = {"vf": {}, "af": {}}
|
88 |
+
self.formatoutputdict = {"vf": {}, "af": {}}
|
89 |
# state=gr.Variable()
|
90 |
# state2=gr.Variable()
|
91 |
|
|
|
96 |
return None
|
97 |
for i in inputs:
|
98 |
self._component += self._get_component_instance(i)
|
99 |
+
# print(self._component, "component")
|
100 |
+
# print(len(self._component), "length")
|
101 |
+
# for comp in self._component:
|
102 |
+
# state = gr.State()
|
103 |
+
# if comp.label is not None:
|
104 |
+
# state.value = comp.label
|
105 |
+
# comp.change(fn=self.changefunc, inputs=[state, comp], outputs=[])
|
106 |
+
# self.changefunc(state.value, comp.value)
|
107 |
+
# comp.change(fn=self.changefunc, inputs=[state, comp], outputs=[])
|
108 |
+
|
109 |
+
def changefunc(self, component_label: str | None, new_value=""):
|
110 |
+
print(component_label, "typaaaaaaaa", new_value)
|
111 |
label, *_ = (
|
112 |
+
component_label.strip(": \n").lower().split()
|
113 |
+
if component_label
|
114 |
+
else ""
|
115 |
+
if not isinstance(component_label, list)
|
116 |
+
else "".join(component_label).strip(": ").lower().split()
|
117 |
)
|
118 |
label += "".join(_).title()
|
119 |
key = newoutputMap.get(label)
|
120 |
lst_extra, vf, af = ([] for _ in range(3))
|
121 |
+
if new_value not in [None, "Source", "Auto", "", "None", "none", 0]:
|
122 |
+
self.set_vf(label, new_value)
|
123 |
+
self.set_af(label, new_value)
|
124 |
+
self.set_f(label, new_value)
|
125 |
+
for val in self.output_dict:
|
126 |
if val == "vf":
|
127 |
+
vf = self.output_dict.get(val, {}).values()
|
128 |
vf = ",".join(list(vf))
|
129 |
elif val == "af":
|
130 |
+
af = self.output_dict.get(val, {}).values()
|
131 |
af = ",".join(list(af))
|
|
|
132 |
else:
|
133 |
+
lst_extra.extend([val, self.output_dict.get(val)])
|
134 |
|
135 |
else:
|
136 |
+
self.output_dict.pop(key, "No Key Exists")
|
137 |
+
self.output_dict["vf"].pop(label, "No Key Exists")
|
138 |
+
self.output_dict["af"].pop(label, "No Key Exists")
|
139 |
self.vf = f"-vf '{vf}'" if vf else ""
|
140 |
self.af = f"-af '{af}'" if af else ""
|
141 |
self.extra = " ".join(lst_extra)
|
142 |
self.commands = f"{self.vf} {self.af} {self.extra}"
|
143 |
|
144 |
+
print(label, self.vf, self.af, self.extra)
|
145 |
+
print(self.output_dict, "out")
|
146 |
|
147 |
+
def set_vf(self, label: str, new_value: "str| int"):
|
148 |
"""Sets Video filters
|
149 |
|
150 |
Args:
|
|
|
154 |
if newoutputMap["vf"].get(label):
|
155 |
key = newoutputMap["vf"].get(label)
|
156 |
if label in ["deinterlace", "denoise"]:
|
157 |
+
value = "_".join(str(new_value).lower().split())
|
158 |
arg = key.get(value, None)
|
159 |
+
self.output_dict["vf"].update({label: arg})
|
160 |
else:
|
161 |
+
self.output_dict["vf"].update({key: key})
|
162 |
|
163 |
+
def set_f(self, label, new_value):
|
164 |
"""Sets Extra filters
|
165 |
Args:
|
166 |
label : label of components
|
|
|
169 |
if newoutputMap.get(label):
|
170 |
key = newoutputMap.get(label)
|
171 |
if label in ["video", "audio"]:
|
172 |
+
codec_label = codecs.get(label)
|
173 |
+
value = (
|
174 |
+
codec_label.get(new_value, new_value) if codec_label else new_value
|
175 |
+
)
|
176 |
print(value)
|
177 |
+
self.output_dict.update({key: value})
|
178 |
elif label in ["startTime", "stopTime"]:
|
179 |
+
self.output_dict.update({key: new_value})
|
180 |
else:
|
181 |
value = "".join(
|
182 |
[
|
183 |
i.get("value", "None")
|
184 |
for i in data.get(label)
|
185 |
+
if i.get("name", None) == new_value
|
186 |
]
|
187 |
)
|
188 |
+
self.output_dict.update({key: value})
|
189 |
|
190 |
+
def set_af(self, label: str, new_value: "str|int"):
|
191 |
+
"""Sets Audio filters
|
192 |
Args:
|
193 |
label : label of components
|
194 |
newValue : value of component
|
195 |
"""
|
196 |
if newoutputMap["af"].get(label):
|
197 |
+
value = int(new_value) / 100
|
198 |
arg = f"{label}={value}"
|
199 |
+
self.output_dict["af"].update({label: arg})
|
200 |
|
201 |
+
def update(self, component: Component):
|
202 |
for comp in self._component:
|
203 |
+
# print(comp, "comp")
|
204 |
+
comp.change(
|
205 |
+
lambda: gr.Textbox(value=self.output_dict),
|
206 |
+
[],
|
207 |
+
[component],
|
208 |
+
trigger_mode="once",
|
209 |
+
)
|
210 |
|
211 |
+
def _get_component_instance(self, inputs: gr.Row | gr.Column) -> List[Component]:
|
212 |
"""
|
213 |
returns components present in a layout block
|
214 |
Parameters:
|
|
|
217 |
res = []
|
218 |
for i in inputs.children:
|
219 |
# print(i,hasattr(i,"children"))
|
220 |
+
# print(
|
221 |
+
# type(i),
|
222 |
+
# "type",
|
223 |
+
# # isinstance(i, gr.components.Component),
|
224 |
+
# # isinstance(i, gr.Blocks),
|
225 |
+
# hasattr(i, "children"),
|
226 |
+
# )
|
227 |
+
if not hasattr(i, "children"):
|
228 |
# res.append(gr.components.get_component_instance(i,render=True))
|
229 |
+
# if isinstance(i, gr.components.Component):
|
230 |
+
# print(gr.components.get_component_instance(i, render=True))
|
231 |
res += [gr.components.get_component_instance(i, render=True)]
|
232 |
+
# print(res)
|
233 |
elif hasattr(i, "children"):
|
234 |
+
# if isinstance(i, gr.Blocks):
|
235 |
res += self._get_component_instance(i)
|
236 |
# print(res)
|
237 |
return res
|
238 |
# return [gr.components.get_component_instance(i, render=True) for i in inputs.children if not hasattr(i, "children")]
|
239 |
|
240 |
+
def set_video_filters(self, options):
|
241 |
+
value = self.output_dict.get(options, "-")
|
242 |
filters = newoutputMap.get(options, None)
|
243 |
arg = ""
|
244 |
if options in ["deinterlace", "denoise"]:
|
245 |
value = "_".join(value.lower().split())
|
246 |
arg = filters.get(value, None)
|
247 |
# self.vf.append(arg)
|
248 |
+
self.output_dict["vf"].update({options: arg})
|
249 |
return True
|
250 |
if options in ["deband", "deflicker", "deshake", "dejudder"]:
|
251 |
arg = filters
|
252 |
+
self.output_dict["vf"].update({options: arg})
|
253 |
return True
|
254 |
|
255 |
+
return False
|
256 |
|
257 |
+
def set_audio_filters(self, options):
|
258 |
+
value = self.output_dict.get(options, "-")
|
259 |
if options in ["acontrast"]:
|
260 |
value = int(value) / 100
|
261 |
arg = f"{options}={value}"
|
262 |
|
263 |
+
self.output_dict["af"].update({options: arg})
|
264 |
return True
|
265 |
return
|
266 |
|
267 |
+
def set_format(self, options):
|
268 |
+
value = self.output_dict.get(options, "-")
|
269 |
filters = newoutputMap.get(options, None)
|
270 |
if options in ["video", "audio"]:
|
271 |
value = "".join(
|
|
|
276 |
]
|
277 |
)
|
278 |
arg = f"{filters} {value}"
|
279 |
+
self.output_dict.update({options: arg})
|
280 |
return True
|
281 |
elif data.get(options) is None:
|
282 |
arg = f"{filters} {value}"
|
283 |
+
self.output_dict.update({options: arg})
|
284 |
return True
|
285 |
elif options != "clip":
|
286 |
value = "".join(
|
|
|
291 |
]
|
292 |
)
|
293 |
arg = f"{filters} {value}"
|
294 |
+
self.output_dict.update({options: arg})
|
295 |
|
296 |
def build(self):
|
297 |
+
for i in self.output_dict:
|
298 |
+
if self.set_video_filters(i):
|
299 |
continue
|
300 |
+
elif self.set_audio_filters(i):
|
301 |
continue
|
302 |
else:
|
303 |
+
self.set_format(i)
|
304 |
lst_extra, vf, af = ([] for _ in range(3))
|
305 |
+
for val in self.output_dict:
|
306 |
if val == "vf":
|
307 |
+
vf = self.output_dict.get(val).values()
|
308 |
vf = ",".join(list(vf))
|
309 |
elif val == "af":
|
310 |
+
af = self.output_dict.get(val).values()
|
311 |
af = ",".join(list(af))
|
312 |
else:
|
313 |
+
lst_extra.append(self.output_dict.get(val))
|
314 |
# print(lst_extra, "temp x")
|
315 |
# if vf:self.vf=f"-vf '{vf}'"
|
316 |
# if af:self.af=f"-af '{af}'"
|
|
|
319 |
self.extra = " ".join(lst_extra)
|
320 |
self.commands = f"{self.vf} {self.af} {self.extra}"
|
321 |
|
322 |
+
def startfunc(self, component: gr.components.Component, new_value=""):
|
323 |
label, *_ = (
|
324 |
+
component.label.strip(": ").lower().split()
|
325 |
+
if not isinstance(component.label, list)
|
326 |
+
else "".join(component.label).strip(": ").lower().split()
|
327 |
)
|
328 |
label += "".join(_).title()
|
329 |
+
if new_value not in [None, "Source", "Auto", "", "None", 0]:
|
330 |
+
self.output_dict["vf"].update({label: new_value})
|
331 |
+
self.output_dict["af"].update({label: new_value})
|
332 |
+
self.output_dict.update({label: new_value})
|
333 |
else:
|
334 |
+
self.output_dict.pop(label, "No Key Exists")
|
335 |
+
self.output_dict["vf"].pop(label, "No Key Exists")
|
336 |
+
self.output_dict["af"].pop(label, "No Key Exists")
|
337 |
# self.formatOutputDict["vf"].pop(label, "Key is None or similar")
|
338 |
# self.formatOutputDict["af"].pop(label, "Key is None or similar")
|
339 |
# self.formatOutputDict.pop(label, "Key is None or similar")
|
340 |
+
print(self.output_dict)
|
341 |
self.build()
|
342 |
|
343 |
|
|
|
369 |
# return [no_,no_]
|
370 |
|
371 |
|
372 |
+
def media_change(option: str, state) -> List[Component]:
|
373 |
"""
|
374 |
Allows playing the media in various options,
|
375 |
Video, Audio or File
|
|
|
381 |
List[Component]: list of toggled output components to display
|
382 |
"""
|
383 |
print(state, "state")
|
384 |
+
ops = {"Audio": gr.Audio(visible=True, value=state)}
|
385 |
+
ops2 = {"Video": gr.Video(visible=True, value=state)}
|
386 |
+
ops3 = {"File": gr.File(visible=True, value=state, interactive=False)}
|
387 |
|
388 |
+
def chosen(x: dict) -> Component:
|
389 |
return x.get(option, gr.update(visible=False))
|
390 |
|
391 |
return [chosen(ops), chosen(ops2), chosen(ops3)]
|
|
|
419 |
# return clear_func(input)
|
420 |
|
421 |
|
422 |
+
def set_custom_bitrate(choice: int) -> Component:
|
423 |
"""
|
424 |
Toggle a component for custom Audio Quality
|
425 |
visible/none
|
|
|
430 |
Component: component toggle state
|
431 |
"""
|
432 |
if choice == "Custom":
|
433 |
+
return gr.Number(visible=True)
|
434 |
+
return gr.Number(visible=False, value=0)
|
|
|
435 |
|
436 |
|
437 |
+
def supported_codecs(codec: str) -> List[Component]:
|
438 |
"""
|
439 |
Changes video and audio components with appropriate
|
440 |
options according to passed format
|
|
|
445 |
Returns:
|
446 |
List[Component]: list of components with updated choices
|
447 |
"""
|
448 |
+
if codec:
|
449 |
+
codec = codec.lower()
|
450 |
video_lst = [
|
451 |
val.get("value")
|
452 |
for val in data["codecs"]["video"]
|
453 |
+
if val.get("supported") is None or codec in val["supported"]
|
454 |
]
|
455 |
audio_lst = [
|
456 |
val.get("value")
|
457 |
for val in data["codecs"]["audio"]
|
458 |
+
if val.get("supported") is None or codec in val["supported"]
|
459 |
]
|
460 |
return [gr.Dropdown(choices=video_lst), gr.Dropdown(choices=audio_lst)]
|
461 |
|
462 |
|
463 |
+
def supported_presets(preset: str) -> Component:
|
464 |
"""
|
465 |
Changes presets component with appropriate
|
466 |
options according to passed format
|
467 |
Args:
|
468 |
+
format: passed media codec (slow,fast,ultrafast)
|
469 |
|
470 |
Returns:
|
471 |
Component: component with updated choice list (video codecs)
|
472 |
"""
|
473 |
+
if preset:
|
474 |
+
preset = preset.lower()
|
475 |
+
print(preset, "preset")
|
476 |
video_lst = [
|
477 |
val.get("name")
|
478 |
for val in data["presets"]
|
479 |
+
if val.get("supported") is None or preset in val["supported"]
|
480 |
]
|
481 |
return gr.Dropdown(choices=video_lst)
|
482 |
|
|
|
542 |
def __repr__(self):
|
543 |
return self._component
|
544 |
|
545 |
+
def __init__(self, *input_component: gr.Row | gr.Column) -> None:
|
546 |
"""
|
547 |
Parameters:
|
548 |
*input_component: A tuple of layout blocks containing components
|
549 |
"""
|
550 |
+
super().__init__(*input_component)
|
551 |
self._component = []
|
552 |
if input_component is not None:
|
553 |
for i in input_component:
|
554 |
# self._component += super()._get_component_instance(i)
|
555 |
self._component += self.__get_component_instance(i)
|
556 |
|
557 |
+
def __get_component_instance(self, inputs: gr.Row | gr.Column) -> list:
|
558 |
# print(inputs, " class instance")
|
559 |
res = []
|
560 |
# print(*inputs.children)
|
561 |
for i in inputs.children:
|
562 |
# print(i,hasattr(i,"children"))
|
563 |
+
if not hasattr(i, "children"):
|
564 |
# res.append(gr.components.get_component_instance(i,render=True))
|
565 |
res += [gr.components.get_component_instance(i, render=True)]
|
566 |
# print(i)
|