File size: 1,552 Bytes
af1695b
37d5502
33c4afa
9a470b2
37d5502
af1695b
37d5502
 
 
 
33c4afa
37d5502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a470b2
37d5502
 
 
 
 
 
1c02b32
37d5502
 
 
 
 
 
 
 
 
 
 
 
1c02b32
37d5502
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import gradio as gr
import tempfile
import subprocess
import os
from pathlib import Path

def convert_webm_to_mp4(webm_file):
    """
    Recebe um arquivo WebM, converte para MP4 e retorna o caminho do MP4.
    """
    if webm_file is None:
        # Se nenhum WebM for enviado, retorna um MP4 dummy de teste
        return "https://sample-videos.com/video123/mp4/240/big_buck_bunny_240p_1mb.mp4"
    
    # Cria arquivo temporário para MP4
    tmp_dir = tempfile.mkdtemp()
    mp4_path = Path(tmp_dir) / "output.mp4"
    
    # Executa conversão via ffmpeg
    try:
        subprocess.run([
            "ffmpeg",
            "-y",
            "-i", webm_file.name,
            "-c:v", "libx264",
            "-preset", "fast",
            "-pix_fmt", "yuv420p",
            str(mp4_path)
        ], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    except subprocess.CalledProcessError as e:
        return f"Error converting video: {e.stderr.decode()}"
    
    return mp4_path

# Interface Gradio
with gr.Blocks() as demo:
    gr.Markdown("### WebM to MP4 Converter\nUpload a WebM video and get MP4 back.")
    
    with gr.Row():
        webm_input = gr.Video(label="WebM Input", type="file")
        mp4_output = gr.Video(label="MP4 Output")
    
    convert_btn = gr.Button("Convert")
    
    # Este é o handler Index 0
    convert_btn.click(fn=convert_webm_to_mp4, inputs=[webm_input], outputs=[mp4_output])

# Lançamento do app
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860, share=True)