Spaces:
Running
Running
King Lazarus
commited on
Commit
·
14e6420
1
Parent(s):
bdc2064
Pending changes exported from your codespace
Browse files- README.md +1 -1
- app.py +188 -126
- functions.py +155 -100
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: 🔥
|
|
4 |
colorFrom: blue
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
tags : ["ffmpeg","converter","media","processing"]
|
10 |
pinned: true
|
|
|
4 |
colorFrom: blue
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.41.0
|
8 |
app_file: app.py
|
9 |
tags : ["ffmpeg","converter","media","processing"]
|
10 |
pinned: true
|
app.py
CHANGED
@@ -6,10 +6,24 @@ from tempfile import _TemporaryFileWrapper
|
|
6 |
from ffmpy import FFmpeg
|
7 |
|
8 |
import gradio as gr
|
9 |
-
from functions import (
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
logging.basicConfig(level=logging.INFO)
|
15 |
|
@@ -17,152 +31,200 @@ logging.basicConfig(level=logging.INFO)
|
|
17 |
logging.info(msg=f"{video_containers}")
|
18 |
|
19 |
|
20 |
-
def convert(file: _TemporaryFileWrapper, options: str,state):
|
21 |
-
stderr=""
|
22 |
-
stdout=""
|
23 |
-
output_file=""
|
24 |
-
video=""
|
25 |
-
ffmpeg=FFmpeg()
|
26 |
try:
|
27 |
logging.info(f"File name: {file.name}")
|
28 |
new_name, _ = file.name.split(".")
|
29 |
logging.info(f"New filename:{new_name}")
|
30 |
output_file = f"{new_name}1.{options.lower()}"
|
31 |
-
ffmpeg = FFmpeg(
|
32 |
-
|
|
|
|
|
|
|
33 |
print(ffmpeg)
|
34 |
print(ffmpeg.cmd)
|
35 |
|
36 |
ffmpeg.run(stderr=subprocess.PIPE)
|
37 |
# pprint(f"{stdout} {stderr}")
|
38 |
-
output=f"{ffmpeg.cmd}"
|
39 |
# video=gr.update(label=output_file,value=output_file)
|
40 |
|
41 |
except Exception as e:
|
42 |
-
stderr=e
|
43 |
-
output=f"{stderr}"
|
44 |
-
return [None,None,None,output]
|
45 |
-
|
46 |
-
state=output_file
|
47 |
-
|
48 |
-
return [output_file,output_file,output_file,output,state]
|
49 |
-
|
50 |
-
|
51 |
-
with gr.Blocks(css="./styles.css") as
|
52 |
-
with gr.Tabs():
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
video_button=gr.Button("Video")
|
77 |
-
audio_button=gr.Button("Audio")
|
78 |
-
file_button=gr.Button("File")
|
79 |
-
media_output_audio = gr.Audio(type="filepath",label="Output",visible=True,interactive=False,source="filepath")
|
80 |
-
media_output_video = gr.Video(label="Output",visible=False)
|
81 |
-
media_output_file = gr.File(label="Output",visible=False)
|
82 |
-
with gr.Row() as command_output:
|
83 |
-
output_textbox = gr.Textbox(label="command",elem_id="outputtext")
|
84 |
-
|
85 |
-
resetFormat=Clear(inputs,inputs_clip)
|
86 |
-
print(inputs_clip.children)
|
87 |
-
print(resetFormat)
|
88 |
-
state=gr.Variable()
|
89 |
-
clearBtn.click(resetFormat.clear, inputs=resetFormat(), outputs=resetFormat())
|
90 |
-
convertBtn.click(convert, inputs=[file_input, options,state], outputs=[
|
91 |
-
media_output_audio,media_output_file,media_output_video, output_textbox,state])
|
92 |
-
|
93 |
-
with gr.TabItem("Video"):
|
94 |
-
with gr.Row() as video_inputs:
|
95 |
-
video_options = gr.Dropdown(
|
96 |
-
label="video", choices=video_codecs,value=video_codecs[-1])
|
97 |
-
preset_options = gr.Dropdown(choices=presets, label="presets",value=presets[-1])
|
98 |
-
|
99 |
-
|
100 |
-
with gr.Row(elem_id="button"):
|
101 |
-
with gr.Column():
|
102 |
clearBtn = gr.Button("Clear")
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
gr.
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
clearBtn = gr.Button("Clear")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
-
filterReset=Clear(filter_inputs,filter_inputs_1)
|
143 |
-
clearBtn.click(filterReset.clear, filterReset(), filterReset())
|
144 |
-
|
145 |
""" Format Tab change functions"""
|
146 |
-
ffmpeg_commands=CommandBuilder(
|
|
|
|
|
147 |
# ffmpeg_commands.do()
|
148 |
-
|
|
|
|
|
149 |
pprint(ffmpeg_commands.commands)
|
150 |
ffmpeg_commands.update(output_textbox)
|
151 |
# file_input.change(fn=updateOutput,inputs=file_input,outputs=output_textbox)
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
options.change(supported_codecs,[options],[video_options,audio_options])
|
156 |
# options.change(mediaChange,[options],[media_output_audio,media_output_video])
|
157 |
# video_button.click(fn=videoChange,inputs=media_output_file,outputs=media_output_video)
|
158 |
-
audio_button.click(
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
# media_output_audio.change(lambda x:gr.update(value=x),[media_output_audio],[media_output_video])
|
161 |
-
file_button.click(
|
|
|
|
|
|
|
|
|
162 |
"""Video Tab change functions"""
|
163 |
-
video_options.change(supported_presets,[video_options],[preset_options])
|
164 |
"""Audio Tab change functions"""
|
165 |
-
audio_bitrate.change(customBitrate,[audio_bitrate],[custom_bitrate])
|
166 |
|
167 |
-
if __name__==
|
168 |
-
|
|
|
6 |
from ffmpy import FFmpeg
|
7 |
|
8 |
import gradio as gr
|
9 |
+
from functions import (
|
10 |
+
Clear,
|
11 |
+
CommandBuilder,
|
12 |
+
audio_channels,
|
13 |
+
audio_codecs,
|
14 |
+
audio_quality,
|
15 |
+
audio_sample_rates,
|
16 |
+
change_clipbox,
|
17 |
+
containers,
|
18 |
+
customBitrate,
|
19 |
+
mediaChange,
|
20 |
+
presets,
|
21 |
+
supported_codecs,
|
22 |
+
supported_presets,
|
23 |
+
video_codecs,
|
24 |
+
video_containers,
|
25 |
+
vf,
|
26 |
+
)
|
27 |
|
28 |
logging.basicConfig(level=logging.INFO)
|
29 |
|
|
|
31 |
logging.info(msg=f"{video_containers}")
|
32 |
|
33 |
|
34 |
+
def convert(file: _TemporaryFileWrapper, options: str, state):
|
35 |
+
stderr = ""
|
36 |
+
# stdout=""
|
37 |
+
output_file = ""
|
38 |
+
# video = ""
|
39 |
+
ffmpeg = FFmpeg()
|
40 |
try:
|
41 |
logging.info(f"File name: {file.name}")
|
42 |
new_name, _ = file.name.split(".")
|
43 |
logging.info(f"New filename:{new_name}")
|
44 |
output_file = f"{new_name}1.{options.lower()}"
|
45 |
+
ffmpeg = FFmpeg(
|
46 |
+
inputs={file.name: None},
|
47 |
+
outputs={output_file: ffmpeg_commands.commands.split()},
|
48 |
+
global_options=["-y", "-hide_banner"],
|
49 |
+
)
|
50 |
print(ffmpeg)
|
51 |
print(ffmpeg.cmd)
|
52 |
|
53 |
ffmpeg.run(stderr=subprocess.PIPE)
|
54 |
# pprint(f"{stdout} {stderr}")
|
55 |
+
output = f"{ffmpeg.cmd}"
|
56 |
# video=gr.update(label=output_file,value=output_file)
|
57 |
|
58 |
except Exception as e:
|
59 |
+
stderr = e
|
60 |
+
output = f"{stderr}"
|
61 |
+
return [None, None, None, output]
|
62 |
+
|
63 |
+
state = output_file
|
64 |
+
|
65 |
+
return [output_file, output_file, output_file, output, state]
|
66 |
+
|
67 |
+
|
68 |
+
with gr.Blocks(css="./styles.css") as demo:
|
69 |
+
# with gr.Tabs():
|
70 |
+
with gr.Tab("Format"):
|
71 |
+
# Input Buttons
|
72 |
+
with gr.Row():
|
73 |
+
with gr.Column() as inputs:
|
74 |
+
file_input = gr.File()
|
75 |
+
options = gr.Radio(
|
76 |
+
label="options", choices=containers, value=containers[0]
|
77 |
+
)
|
78 |
+
with gr.Row():
|
79 |
+
with gr.Row() as inputs_clip:
|
80 |
+
clip = gr.Dropdown(
|
81 |
+
choices=["None", "Enabled"], label="Clip:", value="None"
|
82 |
+
)
|
83 |
+
start_time = gr.Textbox(
|
84 |
+
label="Start Time:",
|
85 |
+
placeholder="00:00",
|
86 |
+
visible=False,
|
87 |
+
interactive=True,
|
88 |
+
)
|
89 |
+
stop_time = gr.Textbox(
|
90 |
+
label="Stop Time:", placeholder="00:00", visible=False
|
91 |
+
)
|
92 |
+
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
clearBtn = gr.Button("Clear")
|
94 |
+
convertBtn = gr.Button("Convert", variant="primary")
|
95 |
+
|
96 |
+
# Output Buttons
|
97 |
+
with gr.Column():
|
98 |
+
# media_output = gr.Audio(label="Output")
|
99 |
+
with gr.Row():
|
100 |
+
video_button = gr.Button("Video")
|
101 |
+
audio_button = gr.Button("Audio")
|
102 |
+
file_button = gr.Button("File")
|
103 |
+
media_output_audio = gr.Audio(
|
104 |
+
type="filepath", label="Output", visible=True, interactive=False
|
105 |
+
)
|
106 |
+
media_output_video = gr.Video(label="Output", visible=False)
|
107 |
+
media_output_file = gr.File(label="Output", visible=False)
|
108 |
+
with gr.Row() as command_output:
|
109 |
+
output_textbox = gr.Textbox(label="command", elem_id="outputtext")
|
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],
|
121 |
+
outputs=[
|
122 |
+
media_output_audio,
|
123 |
+
media_output_file,
|
124 |
+
media_output_video,
|
125 |
+
output_textbox,
|
126 |
+
state,
|
127 |
+
],
|
128 |
+
)
|
129 |
+
|
130 |
+
with gr.Tab("Video"):
|
131 |
+
with gr.Row() as video_inputs:
|
132 |
+
video_options = gr.Dropdown(
|
133 |
+
label="video", choices=video_codecs, value=video_codecs[-1]
|
134 |
+
)
|
135 |
+
preset_options = gr.Dropdown(
|
136 |
+
choices=presets, label="presets", value=presets[-1]
|
137 |
+
)
|
138 |
+
|
139 |
+
with gr.Row(elem_id="button"):
|
140 |
+
with gr.Column():
|
141 |
clearBtn = gr.Button("Clear")
|
142 |
+
videoReset = Clear(video_inputs)
|
143 |
+
clearBtn.click(videoReset.clear, videoReset(), videoReset())
|
144 |
+
|
145 |
+
with gr.Tab("Audio"):
|
146 |
+
with gr.Row() as audio_inputs:
|
147 |
+
# print(names[0])
|
148 |
+
audio_options = gr.Dropdown(
|
149 |
+
label="audio", choices=audio_codecs, value=audio_codecs[-1]
|
150 |
+
)
|
151 |
+
audio_bitrate = gr.Dropdown(
|
152 |
+
choices=audio_quality,
|
153 |
+
label="Audio Qualities",
|
154 |
+
value=audio_quality[0],
|
155 |
+
)
|
156 |
+
custom_bitrate = gr.Number(label="Audio Qualities", visible=False)
|
157 |
+
gr.Dropdown(
|
158 |
+
choices=audio_channels,
|
159 |
+
label="Audio Channels",
|
160 |
+
value=audio_channels[0],
|
161 |
+
)
|
162 |
+
gr.Dropdown(
|
163 |
+
choices=audio_sample_rates,
|
164 |
+
label="Sample Rates",
|
165 |
+
value=audio_sample_rates[0],
|
166 |
+
)
|
167 |
+
|
168 |
+
with gr.Column(elem_id="button"):
|
169 |
+
clearBtn = gr.Button("Clear")
|
170 |
+
audioReset = Clear(audio_inputs)
|
171 |
+
clearBtn.click(audioReset.clear, audioReset(), audioReset())
|
172 |
+
|
173 |
+
with gr.Tab("Filters") as filter_inputs:
|
174 |
+
gr.Markdown("## Video")
|
175 |
+
# equal_height=True
|
176 |
+
with gr.Row(equal_height=True) as filter_inputs:
|
177 |
+
for i in vf:
|
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(label=list(i.keys()), choices=choices, value=choices[0])
|
183 |
+
gr.Markdown("## Audio")
|
184 |
+
with gr.Row(elem_id="acontrast") as filter_inputs_1:
|
185 |
+
acontrastSlider = gr.Slider(label="Acontrast", elem_id="acontrast")
|
186 |
+
|
187 |
+
with gr.Column(elem_id="button"):
|
188 |
+
clearBtn = gr.Button("Clear")
|
189 |
+
|
190 |
+
filterReset = Clear(filter_inputs, filter_inputs_1)
|
191 |
+
clearBtn.click(filterReset.clear, filterReset(), filterReset())
|
192 |
|
|
|
|
|
|
|
193 |
""" Format Tab change functions"""
|
194 |
+
ffmpeg_commands = CommandBuilder(
|
195 |
+
inputs_clip, video_inputs, audio_inputs, filter_inputs, filter_inputs_1
|
196 |
+
)
|
197 |
# ffmpeg_commands.do()
|
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 |
+
|
205 |
+
options.change(supported_codecs, [options], [video_options, audio_options])
|
|
|
|
|
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 |
+
mediaChange,
|
210 |
+
[audio_button, state],
|
211 |
+
[media_output_audio, media_output_video, media_output_file],
|
212 |
+
)
|
213 |
+
video_button.click(
|
214 |
+
mediaChange,
|
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 |
+
mediaChange,
|
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(customBitrate, [audio_bitrate], [custom_bitrate])
|
228 |
|
229 |
+
if __name__ == "__main__":
|
230 |
+
demo.launch(debug=True)
|
functions.py
CHANGED
@@ -1,14 +1,15 @@
|
|
1 |
"""
|
2 |
util functions and classes
|
3 |
"""
|
|
|
4 |
import json
|
5 |
-
from pprint import pprint
|
6 |
from tempfile import _TemporaryFileWrapper
|
7 |
from typing import List
|
8 |
|
9 |
import gradio as gr
|
10 |
from gradio.components import Component
|
11 |
|
|
|
12 |
def parse(param: json) -> dict:
|
13 |
with open(param) as file:
|
14 |
return json.load(file)
|
@@ -18,8 +19,7 @@ data = parse("./data.json")
|
|
18 |
codecs = parse("./codecs.json")
|
19 |
|
20 |
"""Video"""
|
21 |
-
containers = [j.get("name") for i in data["containers"]
|
22 |
-
for j in data["containers"][i]]
|
23 |
video_containers = [i.get("name") for i in data["containers"]["video"]]
|
24 |
video_codecs = [i.get("value") for i in data["codecs"]["video"]]
|
25 |
video_aspect_ratio = [i.get("name") for i in data["aspects"]]
|
@@ -38,10 +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 |
-
filters = ["deband", "deflicker", "deshake",
|
42 |
-
|
43 |
-
vf = [{vFilter: names} for vFilter in filters for names in [
|
44 |
-
[i for i in data[vFilter]]]]
|
45 |
|
46 |
presets = [i.get("name") for i in data["presets"]]
|
47 |
profiles = [i.get("name") for i in data["profiles"]]
|
@@ -51,12 +49,12 @@ speeds = [i.get("name") for i in data["speeds"]]
|
|
51 |
outputMap = parse("./mappings.json")
|
52 |
newoutputMap = parse("./new_mappings.json")
|
53 |
"""Output Mappings of commands to value
|
54 |
-
audioQuality -b:a 128k
|
55 |
"""
|
56 |
|
57 |
|
58 |
-
class CommandBuilder
|
59 |
-
"""Takes a collection of gradio layout elements and attaches
|
60 |
a function to each component in the context
|
61 |
to build an array of ffmpeg commands"""
|
62 |
|
@@ -70,7 +68,7 @@ class CommandBuilder():
|
|
70 |
|
71 |
def reset(self):
|
72 |
self.outputDict = {"vf": {}, "af": {}}
|
73 |
-
self.commands=""
|
74 |
self.vf, self.af, self.extra = ([] for _ in range(3))
|
75 |
|
76 |
def __init__(self, *inputs: gr.Blocks) -> None:
|
@@ -92,17 +90,21 @@ class CommandBuilder():
|
|
92 |
for i in inputs:
|
93 |
self._component += self._get_component_instance(i)
|
94 |
for comp in self._component:
|
95 |
-
state = gr.
|
96 |
-
state2 = gr.
|
97 |
if comp.label is not None:
|
98 |
state.value = comp
|
99 |
state2.value = comp.label
|
100 |
-
comp.change(
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
106 |
label += "".join(_).title()
|
107 |
key = newoutputMap.get(label)
|
108 |
lst_extra, vf, af = ([] for _ in range(3))
|
@@ -132,8 +134,8 @@ class CommandBuilder():
|
|
132 |
|
133 |
print(self.vf, self.af, self.extra)
|
134 |
|
135 |
-
def setVf(self, label:str, newValue:"str| int"):
|
136 |
-
"""Sets Video filters
|
137 |
|
138 |
Args:
|
139 |
label : label of components
|
@@ -149,7 +151,7 @@ class CommandBuilder():
|
|
149 |
self.outputDict["vf"].update({key: key})
|
150 |
|
151 |
def setF(self, label, newValue):
|
152 |
-
"""
|
153 |
Args:
|
154 |
label : label of components
|
155 |
newValue : value of component
|
@@ -157,31 +159,36 @@ class CommandBuilder():
|
|
157 |
if newoutputMap.get(label):
|
158 |
key = newoutputMap.get(label)
|
159 |
if label in ["video", "audio"]:
|
160 |
-
value=codecs.get(label).get(newValue,newValue)
|
161 |
print(value)
|
162 |
-
self.outputDict.update({key:value})
|
163 |
elif label in ["startTime", "stopTime"]:
|
164 |
self.outputDict.update({key: newValue})
|
165 |
else:
|
166 |
-
value = "".join(
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
168 |
self.outputDict.update({key: value})
|
169 |
|
170 |
-
def setAf(self, label:str, newValue:"str|int"):
|
171 |
-
"""
|
172 |
Args:
|
173 |
label : label of components
|
174 |
newValue : value of component
|
175 |
"""
|
176 |
if newoutputMap["af"].get(label):
|
177 |
-
value = int(newValue)/100
|
178 |
arg = f"{label}={value}"
|
179 |
self.outputDict["af"].update({label: arg})
|
180 |
|
181 |
def update(self, Component: Component):
|
182 |
for comp in self._component:
|
183 |
-
comp
|
184 |
-
|
185 |
|
186 |
def _get_component_instance(self, inputs: gr.Blocks) -> List[Component]:
|
187 |
"""
|
@@ -189,15 +196,15 @@ class CommandBuilder():
|
|
189 |
Parameters:
|
190 |
inputs: layout block
|
191 |
"""
|
192 |
-
res=[]
|
193 |
for i in inputs.children:
|
194 |
# print(i,hasattr(i,"children"))
|
195 |
-
if not (hasattr(i,"children")):
|
196 |
# res.append(gr.components.get_component_instance(i,render=True))
|
197 |
-
res+=[gr.components.get_component_instance(i,render=True)]
|
198 |
# print(res)
|
199 |
-
elif hasattr(i,"children"):
|
200 |
-
res+=self._get_component_instance(i)
|
201 |
# print(res)
|
202 |
return res
|
203 |
# return [gr.components.get_component_instance(i, render=True) for i in inputs.children if not hasattr(i, "children")]
|
@@ -222,7 +229,7 @@ class CommandBuilder():
|
|
222 |
def setAudioFilters(self, options):
|
223 |
value = self.outputDict.get(options, "-")
|
224 |
if options in ["acontrast"]:
|
225 |
-
value = int(value)/100
|
226 |
arg = f"{options}={value}"
|
227 |
|
228 |
self.outputDict["af"].update({options: arg})
|
@@ -233,18 +240,28 @@ class CommandBuilder():
|
|
233 |
value = self.outputDict.get(options, "-")
|
234 |
filters = newoutputMap.get(options, None)
|
235 |
if options in ["video", "audio"]:
|
236 |
-
value = "".join(
|
237 |
-
|
|
|
|
|
|
|
|
|
|
|
238 |
arg = f"{filters} {value}"
|
239 |
self.outputDict.update({options: arg})
|
240 |
return True
|
241 |
-
elif data.get(options)
|
242 |
arg = f"{filters} {value}"
|
243 |
self.outputDict.update({options: arg})
|
244 |
return True
|
245 |
elif options != "clip":
|
246 |
-
value = "".join(
|
247 |
-
|
|
|
|
|
|
|
|
|
|
|
248 |
arg = f"{filters} {value}"
|
249 |
self.outputDict.update({options: arg})
|
250 |
|
@@ -274,9 +291,12 @@ class CommandBuilder():
|
|
274 |
self.extra = " ".join(lst_extra)
|
275 |
self.commands = f"{self.vf} {self.af} {self.extra}"
|
276 |
|
277 |
-
def startfunc(self, input: gr.components.
|
278 |
-
label, *_ =
|
279 |
-
|
|
|
|
|
|
|
280 |
label += "".join(_).title()
|
281 |
if newValue not in [None, "Source", "Auto", "", "None", 0]:
|
282 |
self.outputDict["vf"].update({label: newValue})
|
@@ -321,7 +341,7 @@ class CommandBuilder():
|
|
321 |
# return [no_,no_]
|
322 |
|
323 |
|
324 |
-
def mediaChange(option:str,state)-> List[Component]:
|
325 |
"""
|
326 |
Allows playing the media in various options,
|
327 |
Video, Audio or File
|
@@ -332,28 +352,29 @@ def mediaChange(option:str,state)-> List[Component]:
|
|
332 |
Returns:
|
333 |
List[Component]: list of toggled output components to display
|
334 |
"""
|
335 |
-
|
336 |
-
|
337 |
-
|
|
|
|
|
|
|
|
|
338 |
|
339 |
-
def chosen(x): return x.get(option, gr.update(visible=False))
|
340 |
return [chosen(ops), chosen(ops2), chosen(ops3)]
|
341 |
|
342 |
|
343 |
# def videoChange(value):
|
344 |
# print(value.name)
|
345 |
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
|
358 |
|
359 |
"""Helper Functions for Processing """
|
@@ -369,9 +390,10 @@ def mediaChange(option:str,state)-> List[Component]:
|
|
369 |
# print(clear_func(input))
|
370 |
# return clear_func(input)
|
371 |
|
372 |
-
|
|
|
373 |
"""
|
374 |
-
Toggle a component for custom Audio Quality
|
375 |
visible/none
|
376 |
Args:
|
377 |
choice : Custom audio quality
|
@@ -385,10 +407,10 @@ def customBitrate(choice:int)-> Component:
|
|
385 |
return gr.update(visible=False, value=0)
|
386 |
|
387 |
|
388 |
-
def supported_codecs(format: str)-> List[Component]:
|
389 |
"""
|
390 |
-
Changes video and audio components with appropriate
|
391 |
-
options according to passed format
|
392 |
|
393 |
Args:
|
394 |
format: passed media codec (x264,x265)
|
@@ -398,54 +420,77 @@ def supported_codecs(format: str)-> List[Component]:
|
|
398 |
"""
|
399 |
if format:
|
400 |
format = format.lower()
|
401 |
-
video_lst = [
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
411 |
options according to passed format
|
412 |
Args:
|
413 |
format: passed media codec (x264,x265)
|
414 |
|
415 |
Returns:
|
416 |
Component: component with updated choice list (video codecs)
|
417 |
-
"""
|
418 |
if format:
|
419 |
format = format.lower()
|
420 |
-
|
421 |
-
|
422 |
-
|
|
|
|
|
|
|
|
|
423 |
|
424 |
|
425 |
-
def change_clipbox(choice:str)-> List[Component]:
|
426 |
"""
|
427 |
-
Toggles the clipping Textbox
|
428 |
-
|
429 |
Args:
|
430 |
choice: Enabled/None
|
431 |
|
432 |
Returns:
|
433 |
List[Component]: list of components with visible state of the clip components
|
434 |
"""
|
|
|
435 |
if choice == "Enabled":
|
436 |
-
return [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
437 |
else:
|
438 |
-
return [
|
|
|
|
|
|
|
439 |
|
440 |
|
441 |
-
def updateOutput(file: _TemporaryFileWrapper)-> Component:
|
442 |
if file:
|
443 |
print(file.name)
|
444 |
return gr.update(value=file.name)
|
445 |
|
446 |
|
447 |
-
def get_component_instance(inputs: gr.Blocks)-> List[Component]:
|
448 |
-
"""
|
449 |
|
450 |
Args:
|
451 |
inputs: layout elements
|
@@ -453,12 +498,13 @@ def get_component_instance(inputs: gr.Blocks)-> List[Component]:
|
|
453 |
Returns:
|
454 |
List[Component]: components
|
455 |
"""
|
456 |
-
return [
|
|
|
|
|
457 |
|
458 |
|
459 |
class Clear(CommandBuilder):
|
460 |
-
"""
|
461 |
-
"""
|
462 |
|
463 |
def __call__(self, *args, **kwds):
|
464 |
return self._component
|
@@ -469,7 +515,7 @@ class Clear(CommandBuilder):
|
|
469 |
def __repr__(self):
|
470 |
return self._component
|
471 |
|
472 |
-
def __init__(self, *input_component: gr.Blocks
|
473 |
"""
|
474 |
Parameters:
|
475 |
*input_component: A tuple of layout blocks containing components
|
@@ -482,17 +528,17 @@ class Clear(CommandBuilder):
|
|
482 |
|
483 |
def __get_component_instance(self, inputs: gr.Blocks) -> list:
|
484 |
# print(inputs, " class instance")
|
485 |
-
res=[]
|
486 |
# print(*inputs.children)
|
487 |
for i in inputs.children:
|
488 |
# print(i,hasattr(i,"children"))
|
489 |
-
if not (hasattr(i,"children")):
|
490 |
# res.append(gr.components.get_component_instance(i,render=True))
|
491 |
-
res+=[gr.components.get_component_instance(i,render=True)]
|
492 |
# print(i)
|
493 |
-
elif hasattr(i,"children"):
|
494 |
# print(*i.children)
|
495 |
-
res+=self.__get_component_instance(i)
|
496 |
# res=[gr.components.get_component_instance(i, render=True) for i in inputs.children if not hasattr(i, "children")]
|
497 |
# print(res,"__ result")
|
498 |
# print(res)
|
@@ -503,13 +549,22 @@ class Clear(CommandBuilder):
|
|
503 |
print(args, type(args))
|
504 |
if args is not None:
|
505 |
for i in args:
|
506 |
-
self._component +=
|
507 |
return self._component
|
508 |
|
509 |
def clear(self, *args):
|
510 |
"""
|
511 |
Function to clear components from a Block in the class instance
|
512 |
"""
|
513 |
-
|
514 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
515 |
return clear_func(self._component)
|
|
|
1 |
"""
|
2 |
util functions and classes
|
3 |
"""
|
4 |
+
|
5 |
import json
|
|
|
6 |
from tempfile import _TemporaryFileWrapper
|
7 |
from typing import List
|
8 |
|
9 |
import gradio as gr
|
10 |
from gradio.components import Component
|
11 |
|
12 |
+
|
13 |
def parse(param: json) -> dict:
|
14 |
with open(param) as file:
|
15 |
return json.load(file)
|
|
|
19 |
codecs = parse("./codecs.json")
|
20 |
|
21 |
"""Video"""
|
22 |
+
containers = [j.get("name") for i in data["containers"] for j in data["containers"][i]]
|
|
|
23 |
video_containers = [i.get("name") for i in data["containers"]["video"]]
|
24 |
video_codecs = [i.get("value") for i in data["codecs"]["video"]]
|
25 |
video_aspect_ratio = [i.get("name") for i in data["aspects"]]
|
|
|
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 |
+
filters = ["deband", "deflicker", "deshake", "dejudder", "denoise", "deinterlace"]
|
42 |
+
vf = [{vFilter: names} for vFilter in filters for names in [[i for i in data[vFilter]]]]
|
|
|
|
|
43 |
|
44 |
presets = [i.get("name") for i in data["presets"]]
|
45 |
profiles = [i.get("name") for i in data["profiles"]]
|
|
|
49 |
outputMap = parse("./mappings.json")
|
50 |
newoutputMap = parse("./new_mappings.json")
|
51 |
"""Output Mappings of commands to value
|
52 |
+
audioQuality -b:a 128k
|
53 |
"""
|
54 |
|
55 |
|
56 |
+
class CommandBuilder:
|
57 |
+
"""Takes a collection of gradio layout elements and attaches
|
58 |
a function to each component in the context
|
59 |
to build an array of ffmpeg commands"""
|
60 |
|
|
|
68 |
|
69 |
def reset(self):
|
70 |
self.outputDict = {"vf": {}, "af": {}}
|
71 |
+
self.commands = ""
|
72 |
self.vf, self.af, self.extra = ([] for _ in range(3))
|
73 |
|
74 |
def __init__(self, *inputs: gr.Blocks) -> None:
|
|
|
90 |
for i in inputs:
|
91 |
self._component += self._get_component_instance(i)
|
92 |
for comp in self._component:
|
93 |
+
state = gr.State()
|
94 |
+
state2 = gr.State()
|
95 |
if comp.label is not None:
|
96 |
state.value = comp
|
97 |
state2.value = comp.label
|
98 |
+
comp.change(
|
99 |
+
fn=self.changefunc, inputs=[state, state2, comp], outputs=[]
|
100 |
+
)
|
101 |
+
|
102 |
+
def changefunc(self, input: gr.components.Component, c_label="", newValue=""):
|
103 |
+
label, *_ = (
|
104 |
+
input.label.strip(": \n").lower().split()
|
105 |
+
if not isinstance(input.label, list)
|
106 |
+
else "".join(input.label).strip(": ").lower().split()
|
107 |
+
)
|
108 |
label += "".join(_).title()
|
109 |
key = newoutputMap.get(label)
|
110 |
lst_extra, vf, af = ([] for _ in range(3))
|
|
|
134 |
|
135 |
print(self.vf, self.af, self.extra)
|
136 |
|
137 |
+
def setVf(self, label: str, newValue: "str| int"):
|
138 |
+
"""Sets Video filters
|
139 |
|
140 |
Args:
|
141 |
label : label of components
|
|
|
151 |
self.outputDict["vf"].update({key: key})
|
152 |
|
153 |
def setF(self, label, newValue):
|
154 |
+
"""Sets Extra filters
|
155 |
Args:
|
156 |
label : label of components
|
157 |
newValue : value of component
|
|
|
159 |
if newoutputMap.get(label):
|
160 |
key = newoutputMap.get(label)
|
161 |
if label in ["video", "audio"]:
|
162 |
+
value = codecs.get(label).get(newValue, newValue)
|
163 |
print(value)
|
164 |
+
self.outputDict.update({key: value})
|
165 |
elif label in ["startTime", "stopTime"]:
|
166 |
self.outputDict.update({key: newValue})
|
167 |
else:
|
168 |
+
value = "".join(
|
169 |
+
[
|
170 |
+
i.get("value", "None")
|
171 |
+
for i in data.get(label)
|
172 |
+
if i.get("name", None) == newValue
|
173 |
+
]
|
174 |
+
)
|
175 |
self.outputDict.update({key: value})
|
176 |
|
177 |
+
def setAf(self, label: str, newValue: "str|int"):
|
178 |
+
"""Sets Extra filters
|
179 |
Args:
|
180 |
label : label of components
|
181 |
newValue : value of component
|
182 |
"""
|
183 |
if newoutputMap["af"].get(label):
|
184 |
+
value = int(newValue) / 100
|
185 |
arg = f"{label}={value}"
|
186 |
self.outputDict["af"].update({label: arg})
|
187 |
|
188 |
def update(self, Component: Component):
|
189 |
for comp in self._component:
|
190 |
+
print(comp, "comp")
|
191 |
+
comp.change(lambda: gr.update(value=self.outputDict), [], [Component])
|
192 |
|
193 |
def _get_component_instance(self, inputs: gr.Blocks) -> List[Component]:
|
194 |
"""
|
|
|
196 |
Parameters:
|
197 |
inputs: layout block
|
198 |
"""
|
199 |
+
res = []
|
200 |
for i in inputs.children:
|
201 |
# print(i,hasattr(i,"children"))
|
202 |
+
if not (hasattr(i, "children")):
|
203 |
# res.append(gr.components.get_component_instance(i,render=True))
|
204 |
+
res += [gr.components.get_component_instance(i, render=True)]
|
205 |
# print(res)
|
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")]
|
|
|
229 |
def setAudioFilters(self, options):
|
230 |
value = self.outputDict.get(options, "-")
|
231 |
if options in ["acontrast"]:
|
232 |
+
value = int(value) / 100
|
233 |
arg = f"{options}={value}"
|
234 |
|
235 |
self.outputDict["af"].update({options: arg})
|
|
|
240 |
value = self.outputDict.get(options, "-")
|
241 |
filters = newoutputMap.get(options, None)
|
242 |
if options in ["video", "audio"]:
|
243 |
+
value = "".join(
|
244 |
+
[
|
245 |
+
i.get("value", "None")
|
246 |
+
for i in data.get("codecs").get(options)
|
247 |
+
if i.get("name", None) == value
|
248 |
+
]
|
249 |
+
)
|
250 |
arg = f"{filters} {value}"
|
251 |
self.outputDict.update({options: arg})
|
252 |
return True
|
253 |
+
elif data.get(options) is None:
|
254 |
arg = f"{filters} {value}"
|
255 |
self.outputDict.update({options: arg})
|
256 |
return True
|
257 |
elif options != "clip":
|
258 |
+
value = "".join(
|
259 |
+
[
|
260 |
+
i.get("value", "None")
|
261 |
+
for i in data.get(options)
|
262 |
+
if i.get("name", None) == value
|
263 |
+
]
|
264 |
+
)
|
265 |
arg = f"{filters} {value}"
|
266 |
self.outputDict.update({options: arg})
|
267 |
|
|
|
291 |
self.extra = " ".join(lst_extra)
|
292 |
self.commands = f"{self.vf} {self.af} {self.extra}"
|
293 |
|
294 |
+
def startfunc(self, input: gr.components.Component, c_label="", newValue=""):
|
295 |
+
label, *_ = (
|
296 |
+
input.label.strip(": ").lower().split()
|
297 |
+
if not isinstance(input.label, list)
|
298 |
+
else "".join(input.label).strip(": ").lower().split()
|
299 |
+
)
|
300 |
label += "".join(_).title()
|
301 |
if newValue not in [None, "Source", "Auto", "", "None", 0]:
|
302 |
self.outputDict["vf"].update({label: newValue})
|
|
|
341 |
# return [no_,no_]
|
342 |
|
343 |
|
344 |
+
def mediaChange(option: str, state) -> List[Component]:
|
345 |
"""
|
346 |
Allows playing the media in various options,
|
347 |
Video, Audio or File
|
|
|
352 |
Returns:
|
353 |
List[Component]: list of toggled output components to display
|
354 |
"""
|
355 |
+
print(state, "state")
|
356 |
+
ops = {"Audio": gr.update(visible=True, value=state)}
|
357 |
+
ops2 = {"Video": gr.update(visible=True, value=state)}
|
358 |
+
ops3 = {"File": gr.update(visible=True, value=state, interactive=False)}
|
359 |
+
|
360 |
+
def chosen(x):
|
361 |
+
return x.get(option, gr.update(visible=False))
|
362 |
|
|
|
363 |
return [chosen(ops), chosen(ops2), chosen(ops3)]
|
364 |
|
365 |
|
366 |
# def videoChange(value):
|
367 |
# print(value.name)
|
368 |
|
369 |
+
# if option in video_containers:
|
370 |
+
# output=gr.update(visible=True)
|
371 |
+
# return [no_,output]
|
372 |
+
# elif option in audio_containers:
|
373 |
+
# output=gr.update(visible=True)
|
374 |
+
# return [output,no_]
|
375 |
+
# else:
|
376 |
+
# output=gr.update(visible=False)
|
377 |
+
# return [no_,no_]
|
|
|
|
|
378 |
|
379 |
|
380 |
"""Helper Functions for Processing """
|
|
|
390 |
# print(clear_func(input))
|
391 |
# return clear_func(input)
|
392 |
|
393 |
+
|
394 |
+
def customBitrate(choice: int) -> Component:
|
395 |
"""
|
396 |
+
Toggle a component for custom Audio Quality
|
397 |
visible/none
|
398 |
Args:
|
399 |
choice : Custom audio quality
|
|
|
407 |
return gr.update(visible=False, value=0)
|
408 |
|
409 |
|
410 |
+
def supported_codecs(format: str) -> List[Component]:
|
411 |
"""
|
412 |
+
Changes video and audio components with appropriate
|
413 |
+
options according to passed format
|
414 |
|
415 |
Args:
|
416 |
format: passed media codec (x264,x265)
|
|
|
420 |
"""
|
421 |
if format:
|
422 |
format = format.lower()
|
423 |
+
video_lst = [
|
424 |
+
val.get("value")
|
425 |
+
for val in data["codecs"]["video"]
|
426 |
+
if val.get("supported") is None or format in val["supported"]
|
427 |
+
]
|
428 |
+
audio_lst = [
|
429 |
+
val.get("value")
|
430 |
+
for val in data["codecs"]["audio"]
|
431 |
+
if val.get("supported") is None or format in val["supported"]
|
432 |
+
]
|
433 |
+
return [gr.Dropdown(choices=video_lst), gr.Dropdown(choices=audio_lst)]
|
434 |
+
|
435 |
+
|
436 |
+
def supported_presets(format: str) -> Component:
|
437 |
+
"""
|
438 |
+
Changes presets component with appropriate
|
439 |
options according to passed format
|
440 |
Args:
|
441 |
format: passed media codec (x264,x265)
|
442 |
|
443 |
Returns:
|
444 |
Component: component with updated choice list (video codecs)
|
445 |
+
"""
|
446 |
if format:
|
447 |
format = format.lower()
|
448 |
+
print(format, "preset")
|
449 |
+
video_lst = [
|
450 |
+
val.get("name")
|
451 |
+
for val in data["presets"]
|
452 |
+
if val.get("supported") is None or format in val["supported"]
|
453 |
+
]
|
454 |
+
return gr.Dropdown(choices=video_lst)
|
455 |
|
456 |
|
457 |
+
def change_clipbox(choice: str) -> List[Component]:
|
458 |
"""
|
459 |
+
Toggles the clipping Textbox
|
460 |
+
|
461 |
Args:
|
462 |
choice: Enabled/None
|
463 |
|
464 |
Returns:
|
465 |
List[Component]: list of components with visible state of the clip components
|
466 |
"""
|
467 |
+
print(choice, " now choice")
|
468 |
if choice == "Enabled":
|
469 |
+
return [
|
470 |
+
# gr.update(visible=True, value="00:00"),
|
471 |
+
# gr.update(visible=True, value="00:10"),
|
472 |
+
gr.Textbox(
|
473 |
+
label="Start Time:", placeholder="00:00", visible=True, value="00:00"
|
474 |
+
),
|
475 |
+
gr.Textbox(
|
476 |
+
label="Stop Time:", placeholder="00:00", visible=True, value="00:10"
|
477 |
+
),
|
478 |
+
]
|
479 |
else:
|
480 |
+
return [
|
481 |
+
gr.Textbox(visible=False, value=""),
|
482 |
+
gr.Textbox(visible=False, value=""),
|
483 |
+
]
|
484 |
|
485 |
|
486 |
+
def updateOutput(file: _TemporaryFileWrapper) -> Component:
|
487 |
if file:
|
488 |
print(file.name)
|
489 |
return gr.update(value=file.name)
|
490 |
|
491 |
|
492 |
+
def get_component_instance(inputs: gr.Blocks) -> List[Component]:
|
493 |
+
"""returns only components
|
494 |
|
495 |
Args:
|
496 |
inputs: layout elements
|
|
|
498 |
Returns:
|
499 |
List[Component]: components
|
500 |
"""
|
501 |
+
return [
|
502 |
+
gr.components.get_component_instance(i, render=True) for i in inputs.children
|
503 |
+
]
|
504 |
|
505 |
|
506 |
class Clear(CommandBuilder):
|
507 |
+
"""Class for clearing components in layouts"""
|
|
|
508 |
|
509 |
def __call__(self, *args, **kwds):
|
510 |
return self._component
|
|
|
515 |
def __repr__(self):
|
516 |
return self._component
|
517 |
|
518 |
+
def __init__(self, *input_component: gr.Blocks) -> None:
|
519 |
"""
|
520 |
Parameters:
|
521 |
*input_component: A tuple of layout blocks containing components
|
|
|
528 |
|
529 |
def __get_component_instance(self, inputs: gr.Blocks) -> list:
|
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 (hasattr(i, "children")):
|
536 |
# res.append(gr.components.get_component_instance(i,render=True))
|
537 |
+
res += [gr.components.get_component_instance(i, render=True)]
|
538 |
# print(i)
|
539 |
+
elif hasattr(i, "children"):
|
540 |
# print(*i.children)
|
541 |
+
res += self.__get_component_instance(i)
|
542 |
# res=[gr.components.get_component_instance(i, render=True) for i in inputs.children if not hasattr(i, "children")]
|
543 |
# print(res,"__ result")
|
544 |
# print(res)
|
|
|
549 |
print(args, type(args))
|
550 |
if args is not None:
|
551 |
for i in args:
|
552 |
+
self._component += self.__get_component_instance(i)
|
553 |
return self._component
|
554 |
|
555 |
def clear(self, *args):
|
556 |
"""
|
557 |
Function to clear components from a Block in the class instance
|
558 |
"""
|
559 |
+
|
560 |
+
def clear_func(x):
|
561 |
+
return [
|
562 |
+
(
|
563 |
+
component.cleared_value
|
564 |
+
if hasattr(component, "cleared_value")
|
565 |
+
else component.value
|
566 |
+
)
|
567 |
+
for component in x
|
568 |
+
]
|
569 |
+
|
570 |
return clear_func(self._component)
|