Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,57 +3,59 @@ import subprocess
|
|
| 3 |
import uuid
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
# Diretório onde os vídeos convertidos serão salvos
|
| 7 |
OUTPUT_DIR = "outputs"
|
| 8 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
"""
|
| 12 |
-
Recebe um
|
|
|
|
| 13 |
"""
|
| 14 |
if video_file is None:
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
output_name = f"{uuid.uuid4()}.mp4"
|
| 19 |
output_path = os.path.join(OUTPUT_DIR, output_name)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
subprocess.run(
|
| 23 |
-
[
|
| 24 |
-
"ffmpeg",
|
| 25 |
-
"-y", # sobrescrever se já existir
|
| 26 |
-
"-i", video_file, # arquivo de entrada
|
| 27 |
-
"-movflags", "faststart",
|
| 28 |
-
"-pix_fmt", "yuv420p",
|
| 29 |
-
output_path
|
| 30 |
-
],
|
| 31 |
stdout=subprocess.DEVNULL,
|
| 32 |
stderr=subprocess.DEVNULL
|
| 33 |
)
|
| 34 |
|
| 35 |
return output_path
|
| 36 |
|
| 37 |
-
#
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
-
gr.Markdown("## Conversor WebM → MP4")
|
| 40 |
with gr.Row():
|
| 41 |
video_input = gr.File(label="Envie seu WebM", file_types=[".webm"])
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
loading_text.value = "Convertendo..."
|
| 50 |
mp4_path = webm_to_mp4(video_file)
|
| 51 |
-
|
| 52 |
return mp4_path
|
| 53 |
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
-
# Expondo
|
| 57 |
demo.api_name = "convert"
|
| 58 |
|
| 59 |
# Inicializa o app
|
|
|
|
| 3 |
import uuid
|
| 4 |
import os
|
| 5 |
|
|
|
|
| 6 |
OUTPUT_DIR = "outputs"
|
| 7 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 8 |
|
| 9 |
+
# Função de conversão WebM -> MP4
|
| 10 |
+
def webm_to_mp4(video_file=None):
|
| 11 |
"""
|
| 12 |
+
Recebe um WebM e converte para MP4.
|
| 13 |
+
Se não receber arquivo, retorna um MP4 dummy para teste.
|
| 14 |
"""
|
| 15 |
if video_file is None:
|
| 16 |
+
# Arquivo dummy para testes
|
| 17 |
+
dummy_path = os.path.join(OUTPUT_DIR, "dummy.mp4")
|
| 18 |
+
# Cria dummy MP4 de 1s se não existir
|
| 19 |
+
if not os.path.exists(dummy_path):
|
| 20 |
+
subprocess.run([
|
| 21 |
+
"ffmpeg", "-y", "-f", "lavfi", "-i", "color=c=black:s=320x240:d=1",
|
| 22 |
+
"-pix_fmt", "yuv420p", dummy_path
|
| 23 |
+
])
|
| 24 |
+
return dummy_path
|
| 25 |
+
|
| 26 |
+
# Nome único para saída
|
| 27 |
output_name = f"{uuid.uuid4()}.mp4"
|
| 28 |
output_path = os.path.join(OUTPUT_DIR, output_name)
|
| 29 |
|
| 30 |
+
# Converte usando ffmpeg
|
| 31 |
subprocess.run(
|
| 32 |
+
["ffmpeg", "-y", "-i", video_file, "-movflags", "faststart", "-pix_fmt", "yuv420p", output_path],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
stdout=subprocess.DEVNULL,
|
| 34 |
stderr=subprocess.DEVNULL
|
| 35 |
)
|
| 36 |
|
| 37 |
return output_path
|
| 38 |
|
| 39 |
+
# Cria interface Blocks
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("## Conversor WebM → MP4 (App Builder Ready)")
|
| 42 |
with gr.Row():
|
| 43 |
video_input = gr.File(label="Envie seu WebM", file_types=[".webm"])
|
| 44 |
+
video_output = gr.File(label="Download MP4")
|
| 45 |
+
|
| 46 |
+
convert_btn = gr.Button("Converter")
|
| 47 |
+
status_box = gr.Textbox(label="Status", interactive=False)
|
| 48 |
|
| 49 |
+
def convert_and_status(video_file):
|
| 50 |
+
status_box.value = "Processando..."
|
|
|
|
| 51 |
mp4_path = webm_to_mp4(video_file)
|
| 52 |
+
status_box.value = "Concluído!"
|
| 53 |
return mp4_path
|
| 54 |
|
| 55 |
+
# Clique do botão chama a função
|
| 56 |
+
convert_btn.click(fn=convert_and_status, inputs=video_input, outputs=video_output)
|
| 57 |
|
| 58 |
+
# Expondo handler REST para App Builder
|
| 59 |
demo.api_name = "convert"
|
| 60 |
|
| 61 |
# Inicializa o app
|