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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -27
app.py CHANGED
@@ -3,57 +3,59 @@ import subprocess
3
  import uuid
4
  import os
5
 
6
- # Diretório onde os vídeos convertidos serão salvos
7
  OUTPUT_DIR = "outputs"
8
  os.makedirs(OUTPUT_DIR, exist_ok=True)
9
 
10
- def webm_to_mp4(video_file):
 
11
  """
12
- Recebe um arquivo WebM, converte para MP4 e retorna o caminho do arquivo.
 
13
  """
14
  if video_file is None:
15
- return None
16
-
17
- # Gera nome único para o MP4
 
 
 
 
 
 
 
 
18
  output_name = f"{uuid.uuid4()}.mp4"
19
  output_path = os.path.join(OUTPUT_DIR, output_name)
20
 
21
- # Executa o ffmpeg para converter WebM -> MP4
22
  subprocess.run(
23
- [
24
- "ffmpeg",
25
- "-y", # sobrescrever se já existir
26
- "-i", video_file, # arquivo de entrada
27
- "-movflags", "faststart",
28
- "-pix_fmt", "yuv420p",
29
- output_path
30
- ],
31
  stdout=subprocess.DEVNULL,
32
  stderr=subprocess.DEVNULL
33
  )
34
 
35
  return output_path
36
 
37
- # Criando o Blocks com API real
38
  with gr.Blocks() as demo:
39
- gr.Markdown("## Conversor WebM → MP4")
40
  with gr.Row():
41
  video_input = gr.File(label="Envie seu WebM", file_types=[".webm"])
42
- output_file = gr.File(label="Download MP4")
43
-
44
- convert_button = gr.Button("Converter")
45
- loading_text = gr.Textbox(label="Status", interactive=False)
46
 
47
- # Função de callback
48
- def convert_and_update(video_file):
49
- loading_text.value = "Convertendo..."
50
  mp4_path = webm_to_mp4(video_file)
51
- loading_text.value = "Concluído!"
52
  return mp4_path
53
 
54
- convert_button.click(fn=convert_and_update, inputs=video_input, outputs=output_file)
 
55
 
56
- # Expondo API REST real
57
  demo.api_name = "convert"
58
 
59
  # Inicializa o app
 
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