Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
-
import os
|
| 4 |
import tempfile
|
|
|
|
| 5 |
|
| 6 |
def convert_webm_to_mp4(webm_file):
|
| 7 |
"""
|
| 8 |
-
Recebe um arquivo WebM, converte para MP4 usando ffmpeg
|
|
|
|
| 9 |
"""
|
| 10 |
if webm_file is None:
|
| 11 |
return None
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
|
|
|
| 17 |
try:
|
| 18 |
-
# Executa a conversão com ffmpeg
|
| 19 |
subprocess.run(
|
| 20 |
[
|
| 21 |
"ffmpeg",
|
| 22 |
-
"-y",
|
| 23 |
-
"-i", webm_file
|
| 24 |
-
"-c:v", "libx264",
|
| 25 |
"-preset", "fast",
|
| 26 |
-
"-
|
| 27 |
-
mp4_path
|
| 28 |
],
|
| 29 |
check=True,
|
| 30 |
stdout=subprocess.PIPE,
|
| 31 |
stderr=subprocess.PIPE
|
| 32 |
)
|
| 33 |
-
return mp4_path
|
| 34 |
except subprocess.CalledProcessError as e:
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
# --- Gradio
|
| 39 |
-
with gr.Blocks() as
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
-
convert_btn
|
| 45 |
-
convert_btn.click(fn=convert_webm_to_mp4, inputs=webm_input, outputs=mp4_output)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
|
|
|
| 3 |
import tempfile
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
def convert_webm_to_mp4(webm_file):
|
| 7 |
"""
|
| 8 |
+
Recebe um arquivo WebM, converte para MP4 usando ffmpeg,
|
| 9 |
+
e retorna o caminho do MP4 pronto para download.
|
| 10 |
"""
|
| 11 |
if webm_file is None:
|
| 12 |
return None
|
| 13 |
|
| 14 |
+
# Criar arquivo temporário para o MP4
|
| 15 |
+
tmp_dir = tempfile.mkdtemp()
|
| 16 |
+
mp4_path = os.path.join(tmp_dir, "output.mp4")
|
| 17 |
|
| 18 |
+
# Comando ffmpeg para conversão
|
| 19 |
try:
|
|
|
|
| 20 |
subprocess.run(
|
| 21 |
[
|
| 22 |
"ffmpeg",
|
| 23 |
+
"-y", # sobrescreve se existir
|
| 24 |
+
"-i", webm_file, # input
|
| 25 |
+
"-c:v", "libx264", # codec de vídeo
|
| 26 |
"-preset", "fast",
|
| 27 |
+
"-crf", "23", # qualidade
|
| 28 |
+
mp4_path # output
|
| 29 |
],
|
| 30 |
check=True,
|
| 31 |
stdout=subprocess.PIPE,
|
| 32 |
stderr=subprocess.PIPE
|
| 33 |
)
|
|
|
|
| 34 |
except subprocess.CalledProcessError as e:
|
| 35 |
+
return f"Erro ao converter: {e.stderr.decode()}"
|
| 36 |
+
|
| 37 |
+
return mp4_path
|
| 38 |
|
| 39 |
+
# --- Interface Gradio ---
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("## WebM → MP4 Converter")
|
| 42 |
+
webm_input = gr.Video(label="Upload WebM", type="filepath") # apenas caminho do arquivo
|
| 43 |
+
mp4_output = gr.File(label="Download MP4")
|
| 44 |
+
convert_btn = gr.Button("Convert to MP4")
|
| 45 |
|
| 46 |
+
convert_btn.click(fn=convert_webm_to_mp4, inputs=[webm_input], outputs=[mp4_output])
|
|
|
|
| 47 |
|
| 48 |
+
# Rodar app
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
# share=True cria link público temporário (útil para testes)
|
| 51 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|