Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import subprocess | |
| import uuid | |
| import os | |
| # Diretório onde os vídeos convertidos serão salvos | |
| OUTPUT_DIR = "outputs" | |
| os.makedirs(OUTPUT_DIR, exist_ok=True) | |
| def webm_to_mp4(video_file): | |
| """ | |
| Recebe um arquivo WebM, converte para MP4 e retorna o caminho do arquivo. | |
| """ | |
| if video_file is None: | |
| return None | |
| # Gera nome único para o MP4 | |
| output_name = f"{uuid.uuid4()}.mp4" | |
| output_path = os.path.join(OUTPUT_DIR, output_name) | |
| # Executa o ffmpeg para converter WebM -> MP4 | |
| subprocess.run( | |
| [ | |
| "ffmpeg", | |
| "-y", # sobrescrever se já existir | |
| "-i", video_file, # arquivo de entrada | |
| "-movflags", "faststart", | |
| "-pix_fmt", "yuv420p", | |
| output_path | |
| ], | |
| stdout=subprocess.DEVNULL, | |
| stderr=subprocess.DEVNULL | |
| ) | |
| return output_path | |
| # Criando o Blocks com API real | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## Conversor WebM → MP4") | |
| with gr.Row(): | |
| video_input = gr.File(label="Envie seu WebM", file_types=[".webm"]) | |
| output_file = gr.File(label="Download MP4") | |
| convert_button = gr.Button("Converter") | |
| loading_text = gr.Textbox(label="Status", interactive=False) | |
| # Função de callback | |
| def convert_and_update(video_file): | |
| loading_text.value = "Convertendo..." | |
| mp4_path = webm_to_mp4(video_file) | |
| loading_text.value = "Concluído!" | |
| return mp4_path | |
| convert_button.click(fn=convert_and_update, inputs=video_input, outputs=output_file) | |
| # Expondo API REST real | |
| demo.api_name = "convert" | |
| # Inicializa o app | |
| demo.launch(server_name="0.0.0.0", server_port=7860) |