Spaces:
Running
Running
Lazarus
commited on
Commit
·
2c2188f
1
Parent(s):
4414da3
theme update and code view
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import subprocess
|
|
3 |
from pprint import pprint
|
4 |
from tempfile import _TemporaryFileWrapper
|
5 |
|
6 |
-
from ffmpy import FFmpeg
|
7 |
|
8 |
import gradio as gr
|
9 |
from functions import (
|
@@ -21,21 +21,18 @@ from functions import (
|
|
21 |
supported_codecs,
|
22 |
supported_presets,
|
23 |
video_codecs,
|
24 |
-
video_containers,
|
25 |
VF,
|
26 |
)
|
27 |
|
28 |
logging.basicConfig(level=logging.INFO)
|
29 |
|
30 |
|
31 |
-
logging.info(msg=f"{video_containers}")
|
32 |
-
|
33 |
-
|
34 |
def convert(file: _TemporaryFileWrapper, container_format: str, new_state: str):
|
35 |
-
|
36 |
-
|
|
|
|
|
37 |
output_file = ""
|
38 |
-
# video = ""
|
39 |
ffmpeg = FFmpeg()
|
40 |
try:
|
41 |
logging.info("File name: %s", file.name)
|
@@ -47,165 +44,176 @@ def convert(file: _TemporaryFileWrapper, container_format: str, new_state: str):
|
|
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
|
59 |
-
|
60 |
-
|
61 |
return [None, None, None, output, new_state]
|
62 |
|
63 |
new_state = output_file
|
64 |
|
65 |
-
return [output_file, output_file, output_file,
|
66 |
|
67 |
|
68 |
with gr.Blocks(
|
69 |
css="./styles.css",
|
70 |
-
theme=gr.themes.
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
) as demo:
|
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 |
-
file_button = gr.Button("File")
|
109 |
-
media_output_audio = gr.Audio(
|
110 |
-
type="filepath", label="Output", visible=True, interactive=False
|
111 |
)
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
state = gr.State()
|
121 |
-
clearBtn.click(resetFormat.clear, resetFormat(), resetFormat())
|
122 |
-
convertBtn.click(
|
123 |
-
convert,
|
124 |
-
inputs=[file_input, options, state],
|
125 |
-
outputs=[
|
126 |
-
media_output_audio,
|
127 |
-
media_output_file,
|
128 |
-
media_output_video,
|
129 |
-
output_textbox,
|
130 |
-
state,
|
131 |
-
],
|
132 |
-
)
|
133 |
-
|
134 |
-
with gr.Tab("Video", id="video"):
|
135 |
-
with gr.Row() as video_inputs:
|
136 |
-
video_options = gr.Dropdown(
|
137 |
-
label="video", choices=video_codecs, value=video_codecs[-1]
|
138 |
-
)
|
139 |
-
preset_options = gr.Dropdown(
|
140 |
-
choices=presets, label="presets", value=presets[-1]
|
141 |
-
)
|
142 |
-
|
143 |
-
with gr.Row(elem_id="button"):
|
144 |
-
with gr.Column():
|
145 |
-
clearBtn = gr.Button("Clear")
|
146 |
-
videoReset = Clear(video_inputs)
|
147 |
-
clearBtn.click(videoReset.clear, videoReset(), videoReset())
|
148 |
-
|
149 |
-
with gr.Tab("Audio", id="audio"):
|
150 |
-
with gr.Row() as audio_inputs:
|
151 |
-
# print(names[0])
|
152 |
-
audio_options = gr.Dropdown(
|
153 |
-
label="audio", choices=audio_codecs, value=audio_codecs[-1]
|
154 |
-
)
|
155 |
-
audio_bitrate = gr.Dropdown(
|
156 |
-
choices=audio_quality,
|
157 |
-
label="Audio Qualities",
|
158 |
-
value=audio_quality[0],
|
159 |
-
)
|
160 |
-
custom_bitrate = gr.Number(label="Audio Qualities", visible=False)
|
161 |
-
gr.Dropdown(
|
162 |
-
choices=audio_channels,
|
163 |
-
label="Audio Channels",
|
164 |
-
value=audio_channels[0],
|
165 |
-
)
|
166 |
-
gr.Dropdown(
|
167 |
-
choices=audio_sample_rates,
|
168 |
-
label="Sample Rates",
|
169 |
-
value=audio_sample_rates[0],
|
170 |
-
)
|
171 |
-
|
172 |
-
with gr.Column(elem_id="button"):
|
173 |
-
clearBtn = gr.Button("Clear")
|
174 |
-
audioReset = Clear(audio_inputs)
|
175 |
-
clearBtn.click(audioReset.clear, audioReset(), audioReset())
|
176 |
-
|
177 |
-
with gr.Tab("Filters", id="filters") as filter_inputs:
|
178 |
-
gr.Markdown("## Video")
|
179 |
-
# equal_height=True
|
180 |
-
with gr.Row(equal_height=True) as filter_inputs:
|
181 |
-
for i in VF:
|
182 |
-
# print(i.values())
|
183 |
-
# values = list(i.values())
|
184 |
-
values = list(i.values())[0]
|
185 |
-
choices = [j for lst in values for j in [lst.get("name")]]
|
186 |
-
a = gr.Dropdown(
|
187 |
-
label=str(list(i.keys())[0]), choices=choices, value=choices[0]
|
188 |
)
|
189 |
-
gr.Markdown("## Audio")
|
190 |
-
with gr.Row(elem_id="acontrast") as filter_inputs_1:
|
191 |
-
acontrastSlider = gr.Slider(label="Acontrast", elem_id="acontrast")
|
192 |
|
193 |
-
|
194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
|
196 |
-
|
197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
-
|
200 |
-
|
201 |
-
inputs_clip, video_inputs, audio_inputs, filter_inputs, filter_inputs_1
|
202 |
-
)
|
203 |
-
ffmpeg_commands.do()
|
204 |
|
205 |
# demo.load(fn=ffmpeg_commands.reset, inputs=[], outputs=[])
|
206 |
clip.change(fn=change_clipbox, inputs=clip, outputs=[start_time, stop_time])
|
207 |
-
# pprint(ffmpeg_commands.commands)
|
208 |
-
ffmpeg_commands.update(output_textbox)
|
209 |
# file_input.change(fn=updateOutput,inputs=file_input,outputs=output_textbox)
|
210 |
|
211 |
options.change(supported_codecs, [options], [video_options, audio_options])
|
@@ -232,5 +240,13 @@ with gr.Blocks(
|
|
232 |
"""Audio Tab change functions"""
|
233 |
audio_bitrate.change(set_custom_bitrate, [audio_bitrate], [custom_bitrate])
|
234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
if __name__ == "__main__":
|
236 |
-
demo.launch(
|
|
|
3 |
from pprint import pprint
|
4 |
from tempfile import _TemporaryFileWrapper
|
5 |
|
6 |
+
from ffmpy import FFmpeg, FFRuntimeError
|
7 |
|
8 |
import gradio as gr
|
9 |
from functions import (
|
|
|
21 |
supported_codecs,
|
22 |
supported_presets,
|
23 |
video_codecs,
|
|
|
24 |
VF,
|
25 |
)
|
26 |
|
27 |
logging.basicConfig(level=logging.INFO)
|
28 |
|
29 |
|
|
|
|
|
|
|
30 |
def convert(file: _TemporaryFileWrapper, container_format: str, new_state: str):
|
31 |
+
if file is None:
|
32 |
+
logging.error("No file provided for conversion.")
|
33 |
+
return [None, None, None, "No file provided", new_state]
|
34 |
+
|
35 |
output_file = ""
|
|
|
36 |
ffmpeg = FFmpeg()
|
37 |
try:
|
38 |
logging.info("File name: %s", file.name)
|
|
|
44 |
outputs={output_file: ffmpeg_commands.commands.split()},
|
45 |
global_options=["-y", "-hide_banner"],
|
46 |
)
|
47 |
+
ffmpeg_wo = FFmpeg(
|
48 |
+
inputs={"input_file": None},
|
49 |
+
outputs={
|
50 |
+
f"output_file.{container_format.lower()}": ffmpeg_commands.commands.split()
|
51 |
+
},
|
52 |
+
global_options=["-y", "-hide_banner"],
|
53 |
+
)
|
54 |
print(ffmpeg)
|
55 |
print(ffmpeg.cmd)
|
56 |
|
57 |
ffmpeg.run(stderr=subprocess.PIPE)
|
58 |
# pprint(f"{stdout} {stderr}")
|
59 |
output = f"{ffmpeg.cmd}"
|
|
|
60 |
|
61 |
+
except FFRuntimeError as e:
|
62 |
+
output = e.stderr.decode()
|
63 |
+
print(str(e.stderr), flush=True)
|
64 |
return [None, None, None, output, new_state]
|
65 |
|
66 |
new_state = output_file
|
67 |
|
68 |
+
return [output_file, output_file, output_file, ffmpeg_wo.cmd, new_state]
|
69 |
|
70 |
|
71 |
with gr.Blocks(
|
72 |
css="./styles.css",
|
73 |
+
theme=gr.themes.Soft(
|
74 |
+
primary_hue=gr.themes.colors.green,
|
75 |
+
secondary_hue=gr.themes.colors.amber,
|
76 |
+
neutral_hue=gr.themes.colors.slate,
|
77 |
+
font=["sans-serif"],
|
78 |
+
# font=["ui-sans-serif", "system-ui", "sans-serif"],
|
79 |
+
),
|
80 |
) as demo:
|
81 |
+
with gr.Tabs(selected="format"):
|
82 |
+
with gr.Tab("Format", id="format"):
|
83 |
+
# Input Buttons
|
84 |
+
with gr.Row():
|
85 |
+
with gr.Column() as inputs:
|
86 |
+
file_input = gr.File()
|
87 |
+
options = gr.Radio(
|
88 |
+
label="options", choices=containers, value=containers[0]
|
89 |
+
)
|
90 |
+
with gr.Row():
|
91 |
+
with gr.Row() as inputs_clip:
|
92 |
+
clip = gr.Dropdown(
|
93 |
+
choices=["None", "Enabled"], label="Clip:", value="None"
|
94 |
+
)
|
95 |
+
start_time = gr.Textbox(
|
96 |
+
label="Start Time:",
|
97 |
+
placeholder="00:00",
|
98 |
+
visible=False,
|
99 |
+
interactive=True,
|
100 |
+
)
|
101 |
+
stop_time = gr.Textbox(
|
102 |
+
label="Stop Time:",
|
103 |
+
placeholder="00:00",
|
104 |
+
visible=False,
|
105 |
+
interactive=True,
|
106 |
+
)
|
107 |
+
with gr.Row():
|
108 |
+
clearBtn = gr.Button("Clear")
|
109 |
+
convertBtn = gr.Button("Convert", variant="primary")
|
110 |
+
|
111 |
+
# Output Buttons
|
112 |
+
with gr.Column():
|
113 |
+
# media_output = gr.Audio(label="Output")
|
114 |
+
with gr.Row():
|
115 |
+
video_button = gr.Button("Video")
|
116 |
+
audio_button = gr.Button("Audio")
|
117 |
+
file_button = gr.Button("File")
|
118 |
+
media_output_audio = gr.Audio(
|
119 |
+
type="filepath", label="Output", visible=True, interactive=False
|
120 |
+
)
|
121 |
+
media_output_video = gr.Video(
|
122 |
+
label="Output", visible=False, height=300
|
123 |
+
)
|
124 |
+
media_output_file = gr.File(label="Output", visible=False)
|
125 |
+
with gr.Row() as command_output:
|
126 |
+
output_textbox = gr.Code(
|
127 |
+
value="$ echo 'Hello, World!'",
|
128 |
+
label="command",
|
129 |
+
language="shell",
|
130 |
+
elem_id="outputtext",
|
131 |
)
|
132 |
+
|
133 |
+
resetFormat = Clear(inputs, inputs_clip)
|
134 |
+
# print(inputs_clip.children)
|
135 |
+
# print(resetFormat)
|
136 |
+
state = gr.State()
|
137 |
+
clearBtn.click(resetFormat.clear, resetFormat(), resetFormat())
|
138 |
+
convertBtn.click(
|
139 |
+
convert,
|
140 |
+
inputs=[file_input, options, state],
|
141 |
+
outputs=[
|
142 |
+
media_output_audio,
|
143 |
+
media_output_file,
|
144 |
+
media_output_video,
|
145 |
+
output_textbox,
|
146 |
+
state,
|
147 |
+
],
|
|
|
|
|
|
|
148 |
)
|
149 |
+
|
150 |
+
with gr.Tab("Video", id="video"):
|
151 |
+
with gr.Row() as video_inputs:
|
152 |
+
video_options = gr.Dropdown(
|
153 |
+
label="video", choices=video_codecs, value=video_codecs[-1]
|
154 |
+
)
|
155 |
+
preset_options = gr.Dropdown(
|
156 |
+
choices=presets, label="presets", value=presets[-1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
)
|
|
|
|
|
|
|
158 |
|
159 |
+
with gr.Row(elem_id="button"):
|
160 |
+
with gr.Column():
|
161 |
+
clearBtn = gr.Button("Clear")
|
162 |
+
videoReset = Clear(video_inputs)
|
163 |
+
clearBtn.click(videoReset.clear, videoReset(), videoReset())
|
164 |
+
|
165 |
+
with gr.Tab("Audio", id="audio"):
|
166 |
+
with gr.Row() as audio_inputs:
|
167 |
+
# print(names[0])
|
168 |
+
audio_options = gr.Dropdown(
|
169 |
+
label="audio", choices=audio_codecs, value=audio_codecs[-1]
|
170 |
+
)
|
171 |
+
audio_bitrate = gr.Dropdown(
|
172 |
+
choices=audio_quality,
|
173 |
+
label="Audio Qualities",
|
174 |
+
value=audio_quality[0],
|
175 |
+
)
|
176 |
+
custom_bitrate = gr.Number(label="Audio Qualities", visible=False)
|
177 |
+
gr.Dropdown(
|
178 |
+
choices=audio_channels,
|
179 |
+
label="Audio Channels",
|
180 |
+
value=audio_channels[0],
|
181 |
+
)
|
182 |
+
gr.Dropdown(
|
183 |
+
choices=audio_sample_rates,
|
184 |
+
label="Sample Rates",
|
185 |
+
value=audio_sample_rates[0],
|
186 |
+
)
|
187 |
|
188 |
+
with gr.Column(elem_id="button"):
|
189 |
+
clearBtn = gr.Button("Clear")
|
190 |
+
audioReset = Clear(audio_inputs)
|
191 |
+
clearBtn.click(audioReset.clear, audioReset(), audioReset())
|
192 |
+
|
193 |
+
with gr.Tab("Filters", id="filters") as filter_inputs:
|
194 |
+
gr.Markdown("## Video")
|
195 |
+
# equal_height=True
|
196 |
+
with gr.Row(equal_height=True) as filter_inputs:
|
197 |
+
for i in VF:
|
198 |
+
# print(i.values())
|
199 |
+
# values = list(i.values())
|
200 |
+
values = list(i.values())[0]
|
201 |
+
choices = [j for lst in values for j in [lst.get("name")]]
|
202 |
+
a = gr.Dropdown(
|
203 |
+
label=str(list(i.keys())[0]), choices=choices, value=choices[0]
|
204 |
+
)
|
205 |
+
gr.Markdown("## Audio")
|
206 |
+
with gr.Row(elem_id="acontrast") as filter_inputs_1:
|
207 |
+
acontrastSlider = gr.Slider(label="Acontrast", elem_id="acontrast")
|
208 |
+
|
209 |
+
with gr.Column(elem_id="button"):
|
210 |
+
clearBtn = gr.Button("Clear")
|
211 |
|
212 |
+
filterReset = Clear(filter_inputs, filter_inputs_1)
|
213 |
+
clearBtn.click(filterReset.clear, filterReset(), filterReset())
|
|
|
|
|
|
|
214 |
|
215 |
# demo.load(fn=ffmpeg_commands.reset, inputs=[], outputs=[])
|
216 |
clip.change(fn=change_clipbox, inputs=clip, outputs=[start_time, stop_time])
|
|
|
|
|
217 |
# file_input.change(fn=updateOutput,inputs=file_input,outputs=output_textbox)
|
218 |
|
219 |
options.change(supported_codecs, [options], [video_options, audio_options])
|
|
|
240 |
"""Audio Tab change functions"""
|
241 |
audio_bitrate.change(set_custom_bitrate, [audio_bitrate], [custom_bitrate])
|
242 |
|
243 |
+
""" Format Tab change functions"""
|
244 |
+
ffmpeg_commands = CommandBuilder(
|
245 |
+
inputs_clip, video_inputs, audio_inputs, filter_inputs, filter_inputs_1
|
246 |
+
)
|
247 |
+
ffmpeg_commands.do()
|
248 |
+
# pprint(ffmpeg_commands.commands)
|
249 |
+
ffmpeg_commands.update(output_textbox)
|
250 |
+
|
251 |
if __name__ == "__main__":
|
252 |
+
demo.launch(show_error=True)
|