AlexandreScriptsMT commited on
Commit
37d5502
·
verified ·
1 Parent(s): 9a470b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -27
app.py CHANGED
@@ -1,35 +1,50 @@
1
  import gradio as gr
 
2
  import subprocess
3
- import uuid
4
  import os
 
5
 
6
- def convert_video(webm_file):
 
 
 
7
  if webm_file is None:
8
- return None
9
-
10
- output_path = f"/tmp/{uuid.uuid4()}.mp4"
11
-
12
- subprocess.run(
13
- [
14
- "ffmpeg", "-y",
15
- "-i", webm_file,
16
- "-movflags", "faststart",
 
 
 
 
 
 
17
  "-pix_fmt", "yuv420p",
18
- output_path
19
- ],
20
- check=True
21
- )
22
-
23
- return output_path
24
-
25
 
26
- iface = gr.Interface(
27
- fn=convert_video,
28
- inputs=gr.File(file_types=[".webm"], label="Upload WebM"),
29
- outputs=gr.File(label="MP4 Output"),
30
- title="WebM → MP4 Converter",
31
- api_name="convert" # 🔥 AGORA É API DE VERDADE
32
- )
 
 
 
 
 
33
 
34
- iface.queue()
35
- iface.launch()
 
 
1
  import gradio as gr
2
+ import tempfile
3
  import subprocess
 
4
  import os
5
+ from pathlib import Path
6
 
7
+ def convert_webm_to_mp4(webm_file):
8
+ """
9
+ Recebe um arquivo WebM, converte para MP4 e retorna o caminho do MP4.
10
+ """
11
  if webm_file is None:
12
+ # Se nenhum WebM for enviado, retorna um MP4 dummy de teste
13
+ return "https://sample-videos.com/video123/mp4/240/big_buck_bunny_240p_1mb.mp4"
14
+
15
+ # Cria arquivo temporário para MP4
16
+ tmp_dir = tempfile.mkdtemp()
17
+ mp4_path = Path(tmp_dir) / "output.mp4"
18
+
19
+ # Executa conversão via ffmpeg
20
+ try:
21
+ subprocess.run([
22
+ "ffmpeg",
23
+ "-y",
24
+ "-i", webm_file.name,
25
+ "-c:v", "libx264",
26
+ "-preset", "fast",
27
  "-pix_fmt", "yuv420p",
28
+ str(mp4_path)
29
+ ], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
30
+ except subprocess.CalledProcessError as e:
31
+ return f"Error converting video: {e.stderr.decode()}"
32
+
33
+ return mp4_path
 
34
 
35
+ # Interface Gradio
36
+ with gr.Blocks() as demo:
37
+ gr.Markdown("### WebM to MP4 Converter\nUpload a WebM video and get MP4 back.")
38
+
39
+ with gr.Row():
40
+ webm_input = gr.Video(label="WebM Input", type="file")
41
+ mp4_output = gr.Video(label="MP4 Output")
42
+
43
+ convert_btn = gr.Button("Convert")
44
+
45
+ # Este é o handler Index 0
46
+ convert_btn.click(fn=convert_webm_to_mp4, inputs=[webm_input], outputs=[mp4_output])
47
 
48
+ # Lançamento do app
49
+ if __name__ == "__main__":
50
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=True)