Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,50 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import subprocess
|
| 3 |
-
import uuid
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
-
def
|
|
|
|
|
|
|
|
|
|
| 7 |
if webm_file is None:
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"-pix_fmt", "yuv420p",
|
| 18 |
-
|
| 19 |
-
],
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import tempfile
|
| 3 |
import subprocess
|
|
|
|
| 4 |
import os
|
| 5 |
+
from pathlib import Path
|
| 6 |
|
| 7 |
+
def convert_webm_to_mp4(webm_file):
|
| 8 |
+
"""
|
| 9 |
+
Recebe um arquivo WebM, converte para MP4 e retorna o caminho do MP4.
|
| 10 |
+
"""
|
| 11 |
if webm_file is None:
|
| 12 |
+
# Se nenhum WebM for enviado, retorna um MP4 dummy de teste
|
| 13 |
+
return "https://sample-videos.com/video123/mp4/240/big_buck_bunny_240p_1mb.mp4"
|
| 14 |
+
|
| 15 |
+
# Cria arquivo temporário para MP4
|
| 16 |
+
tmp_dir = tempfile.mkdtemp()
|
| 17 |
+
mp4_path = Path(tmp_dir) / "output.mp4"
|
| 18 |
+
|
| 19 |
+
# Executa conversão via ffmpeg
|
| 20 |
+
try:
|
| 21 |
+
subprocess.run([
|
| 22 |
+
"ffmpeg",
|
| 23 |
+
"-y",
|
| 24 |
+
"-i", webm_file.name,
|
| 25 |
+
"-c:v", "libx264",
|
| 26 |
+
"-preset", "fast",
|
| 27 |
"-pix_fmt", "yuv420p",
|
| 28 |
+
str(mp4_path)
|
| 29 |
+
], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 30 |
+
except subprocess.CalledProcessError as e:
|
| 31 |
+
return f"Error converting video: {e.stderr.decode()}"
|
| 32 |
+
|
| 33 |
+
return mp4_path
|
|
|
|
| 34 |
|
| 35 |
+
# Interface Gradio
|
| 36 |
+
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("### WebM to MP4 Converter\nUpload a WebM video and get MP4 back.")
|
| 38 |
+
|
| 39 |
+
with gr.Row():
|
| 40 |
+
webm_input = gr.Video(label="WebM Input", type="file")
|
| 41 |
+
mp4_output = gr.Video(label="MP4 Output")
|
| 42 |
+
|
| 43 |
+
convert_btn = gr.Button("Convert")
|
| 44 |
+
|
| 45 |
+
# Este é o handler Index 0
|
| 46 |
+
convert_btn.click(fn=convert_webm_to_mp4, inputs=[webm_input], outputs=[mp4_output])
|
| 47 |
|
| 48 |
+
# Lançamento do app
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|