File size: 1,944 Bytes
af1695b
 
 
 
 
 
 
 
2c26e49
 
4436f52
2c26e49
 
4436f52
3f3d364
2c26e49
 
 
 
 
 
 
 
 
 
 
af1695b
 
 
2c26e49
4436f52
2c26e49
4436f52
 
 
af1695b
 
 
2c26e49
3f3d364
2c26e49
3f3d364
 
2c26e49
 
 
 
3f3d364
2c26e49
 
3f3d364
2c26e49
3f3d364
 
2c26e49
 
3f3d364
2c26e49
3f3d364
af1695b
4436f52
3f3d364
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
52
53
54
55
56
57
58
59
60
61
62
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)