AlexandreScriptsMT commited on
Commit
69005d5
·
verified ·
1 Parent(s): ad45296

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -28
app.py CHANGED
@@ -1,42 +1,48 @@
1
  import gradio as gr
2
- import tempfile
3
  import subprocess
4
- from pathlib import Path
 
5
 
6
  def convert_webm_to_mp4(webm_file):
7
  """
8
- Recebe um arquivo WebM, converte para MP4 e retorna o caminho do MP4.
9
  """
10
  if webm_file is None:
11
- # MP4 dummy se nenhum WebM for enviado
12
- return "https://sample-videos.com/video123/mp4/240/big_buck_bunny_240p_1mb.mp4"
13
 
14
- tmp_dir = tempfile.mkdtemp()
15
- mp4_path = Path(tmp_dir) / "output.mp4"
 
16
 
17
  try:
18
- subprocess.run([
19
- "ffmpeg",
20
- "-y",
21
- "-i", webm_file.name,
22
- "-c:v", "libx264",
23
- "-preset", "fast",
24
- "-pix_fmt", "yuv420p",
25
- str(mp4_path)
26
- ], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
 
 
 
 
 
 
27
  except subprocess.CalledProcessError as e:
28
- return f"Error converting video: {e.stderr.decode()}"
 
29
 
30
- return mp4_path
 
 
 
 
31
 
32
- # Interface explícita, compatível com App Builder
33
- iface = gr.Interface(
34
- fn=convert_webm_to_mp4,
35
- inputs=gr.Video(label="WebM Input"),
36
- outputs=gr.Video(label="MP4 Output"),
37
- allow_flagging="never"
38
- )
39
 
40
- if __name__ == "__main__":
41
- # Garantir que a primeira função do Space (Index 0) seja essa
42
- iface.launch(server_name="0.0.0.0", server_port=7860, share=True)
 
1
  import gradio as gr
 
2
  import subprocess
3
+ import os
4
+ import tempfile
5
 
6
  def convert_webm_to_mp4(webm_file):
7
  """
8
+ Recebe um arquivo WebM, converte para MP4 usando ffmpeg e retorna o caminho do MP4.
9
  """
10
  if webm_file is None:
11
+ return None
 
12
 
13
+ # Cria um arquivo temporário para o MP4
14
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp:
15
+ mp4_path = tmp.name
16
 
17
  try:
18
+ # Executa a conversão com ffmpeg
19
+ subprocess.run(
20
+ [
21
+ "ffmpeg",
22
+ "-y", # sobrescrever se existir
23
+ "-i", webm_file.name,
24
+ "-c:v", "libx264",
25
+ "-preset", "fast",
26
+ "-pix_fmt", "yuv420p",
27
+ mp4_path
28
+ ],
29
+ check=True,
30
+ stdout=subprocess.PIPE,
31
+ stderr=subprocess.PIPE
32
+ )
33
+ return mp4_path
34
  except subprocess.CalledProcessError as e:
35
+ print("Erro ao converter vídeo:", e.stderr.decode())
36
+ return None
37
 
38
+ # --- Gradio Blocks ---
39
+ with gr.Blocks() as app:
40
+ with gr.Row():
41
+ webm_input = gr.Video(label="WebM Input")
42
+ mp4_output = gr.Video(label="MP4 Output")
43
 
44
+ convert_btn = gr.Button("Convert WebM to MP4")
45
+ convert_btn.click(fn=convert_webm_to_mp4, inputs=webm_input, outputs=mp4_output)
 
 
 
 
 
46
 
47
+ # Executa o app
48
+ app.launch(server_name="0.0.0.0", server_port=7860, share=False)