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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -23
app.py CHANGED
@@ -1,48 +1,51 @@
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)
 
 
 
1
  import gradio as gr
2
  import subprocess
 
3
  import tempfile
4
+ import os
5
 
6
  def convert_webm_to_mp4(webm_file):
7
  """
8
+ Recebe um arquivo WebM, converte para MP4 usando ffmpeg,
9
+ e retorna o caminho do MP4 pronto para download.
10
  """
11
  if webm_file is None:
12
  return None
13
 
14
+ # Criar arquivo temporário para o MP4
15
+ tmp_dir = tempfile.mkdtemp()
16
+ mp4_path = os.path.join(tmp_dir, "output.mp4")
17
 
18
+ # Comando ffmpeg para conversão
19
  try:
 
20
  subprocess.run(
21
  [
22
  "ffmpeg",
23
+ "-y", # sobrescreve se existir
24
+ "-i", webm_file, # input
25
+ "-c:v", "libx264", # codec de vídeo
26
  "-preset", "fast",
27
+ "-crf", "23", # qualidade
28
+ mp4_path # output
29
  ],
30
  check=True,
31
  stdout=subprocess.PIPE,
32
  stderr=subprocess.PIPE
33
  )
 
34
  except subprocess.CalledProcessError as e:
35
+ return f"Erro ao converter: {e.stderr.decode()}"
36
+
37
+ return mp4_path
38
 
39
+ # --- Interface Gradio ---
40
+ with gr.Blocks() as demo:
41
+ gr.Markdown("## WebM → MP4 Converter")
42
+ webm_input = gr.Video(label="Upload WebM", type="filepath") # apenas caminho do arquivo
43
+ mp4_output = gr.File(label="Download MP4")
44
+ convert_btn = gr.Button("Convert to MP4")
45
 
46
+ convert_btn.click(fn=convert_webm_to_mp4, inputs=[webm_input], outputs=[mp4_output])
 
47
 
48
+ # Rodar app
49
+ if __name__ == "__main__":
50
+ # share=True cria link público temporário (útil para testes)
51
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=True)