AlexandreScriptsMT commited on
Commit
1c02b32
·
verified ·
1 Parent(s): 2c26e49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -50
app.py CHANGED
@@ -1,62 +1,43 @@
1
  import gradio as gr
2
- import subprocess
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
62
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
 
 
2
  import os
3
+ import uuid
4
+ import shutil
5
 
6
+ # Pasta de saída
7
  OUTPUT_DIR = "outputs"
8
  os.makedirs(OUTPUT_DIR, exist_ok=True)
9
 
10
+ def process_video(video_file):
 
11
  """
12
+ Função chamada tanto pela UI quanto pela API.
13
+ Recebe um arquivo e devolve um arquivo processado.
14
  """
15
+
16
  if video_file is None:
17
+ raise ValueError("Nenhum arquivo enviado")
18
+
19
+ # Nome único para evitar conflito
20
+ file_id = str(uuid.uuid4())
21
+ input_path = video_file
22
+ output_path = os.path.join(OUTPUT_DIR, f"{file_id}_output.webm")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ # ⚠️ AQUI entra sua lógica real
25
+ # Por enquanto, apenas copiamos o arquivo
26
+ shutil.copy(input_path, output_path)
27
+
28
+ # Retornar caminho do arquivo = link de download
29
  return output_path
30
 
31
+
32
+ # 🔥 INTERFACE COM API EXPLÍCITA
33
+ iface = gr.Interface(
34
+ fn=process_video,
35
+ inputs=gr.File(label="Upload do vídeo"),
36
+ outputs=gr.File(label="Download do resultado"),
37
+ title="Conversor de Vídeo",
38
+ description="Envie um vídeo e receba o arquivo processado.",
39
+ api_name="predict" # 👈 ISSO É O MAIS IMPORTANTE
40
+ )
41
+
42
+ if __name__ == "__main__":
43
+ iface.launch()