Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import subprocess | |
| import uuid | |
| import os | |
| OUTPUT_DIR = "outputs" | |
| os.makedirs(OUTPUT_DIR, exist_ok=True) | |
| # Função de conversão WebM -> MP4 | |
| def webm_to_mp4(video_file=None): | |
| """ | |
| Recebe um WebM e converte para MP4. | |
| Se não receber arquivo, retorna um MP4 dummy para teste. | |
| """ | |
| if video_file is None: | |
| # Arquivo dummy para testes | |
| dummy_path = os.path.join(OUTPUT_DIR, "dummy.mp4") | |
| # Cria dummy MP4 de 1s se não existir | |
| if not os.path.exists(dummy_path): | |
| subprocess.run([ | |
| "ffmpeg", "-y", "-f", "lavfi", "-i", "color=c=black:s=320x240:d=1", | |
| "-pix_fmt", "yuv420p", dummy_path | |
| ]) | |
| return dummy_path | |
| # Nome único para saída | |
| output_name = f"{uuid.uuid4()}.mp4" | |
| output_path = os.path.join(OUTPUT_DIR, output_name) | |
| # Converte usando ffmpeg | |
| subprocess.run( | |
| ["ffmpeg", "-y", "-i", video_file, "-movflags", "faststart", "-pix_fmt", "yuv420p", output_path], | |
| stdout=subprocess.DEVNULL, | |
| stderr=subprocess.DEVNULL | |
| ) | |
| return output_path | |
| # Cria interface Blocks | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## Conversor WebM → MP4 (App Builder Ready)") | |
| with gr.Row(): | |
| video_input = gr.File(label="Envie seu WebM", file_types=[".webm"]) | |
| video_output = gr.File(label="Download MP4") | |
| convert_btn = gr.Button("Converter") | |
| status_box = gr.Textbox(label="Status", interactive=False) | |
| def convert_and_status(video_file): | |
| status_box.value = "Processando..." | |
| mp4_path = webm_to_mp4(video_file) | |
| status_box.value = "Concluído!" | |
| return mp4_path | |
| # Clique do botão chama a função | |
| convert_btn.click(fn=convert_and_status, inputs=video_input, outputs=video_output) | |
| # Expondo handler REST para App Builder | |
| demo.api_name = "convert" | |
| # Inicializa o app | |
| demo.launch(server_name="0.0.0.0", server_port=7860) |