PlayerBPlaytime commited on
Commit
9faf044
·
verified ·
1 Parent(s): fe3bdbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -2
app.py CHANGED
@@ -3,14 +3,32 @@ import subprocess
3
  import os
4
  import uuid
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  def generate_video(audio_path, image_path):
7
  output_filename = f"output_{uuid.uuid4().hex[:8]}.mp4"
 
 
 
8
 
9
  try:
10
  cmd = [
11
  "ffmpeg", "-y", "-loop", "1", "-framerate", "1", "-i", image_path,
12
  "-i", audio_path, "-c:v", "libx264", "-preset", "ultrafast", "-tune", "stillimage",
13
- "-c:a", "aac", "-b:a", "192k", "-shortest", "-r", "1", "-pix_fmt", "yuv420p", output_filename
 
 
14
  ]
15
  subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
16
 
@@ -22,7 +40,6 @@ def generate_video(audio_path, image_path):
22
  except Exception as e:
23
  return f"⚠️ Error: {str(e)}"
24
 
25
- # UI with fixed types
26
  with gr.Blocks(theme="Taithrah/Minimal") as app:
27
  gr.Markdown("# 🎶 Audio to Video Tool\nUpload an audio file and an image to generate a simple MP4 video (like TunesToTube)")
28
  with gr.Row():
 
3
  import os
4
  import uuid
5
 
6
+ def get_audio_duration(audio_path):
7
+ """Obtiene duración en segundos del audio usando ffprobe"""
8
+ result = subprocess.run([
9
+ "ffprobe", "-v", "error", "-show_entries",
10
+ "format=duration", "-of",
11
+ "default=noprint_wrappers=1:nokey=1", audio_path
12
+ ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
13
+
14
+ try:
15
+ return float(result.stdout.strip())
16
+ except:
17
+ return None
18
+
19
  def generate_video(audio_path, image_path):
20
  output_filename = f"output_{uuid.uuid4().hex[:8]}.mp4"
21
+ duration = get_audio_duration(audio_path)
22
+ if not duration:
23
+ return "❌ Could not get audio duration. Check your audio file."
24
 
25
  try:
26
  cmd = [
27
  "ffmpeg", "-y", "-loop", "1", "-framerate", "1", "-i", image_path,
28
  "-i", audio_path, "-c:v", "libx264", "-preset", "ultrafast", "-tune", "stillimage",
29
+ "-c:a", "aac", "-b:a", "192k",
30
+ "-t", str(duration), # <- aquí limitamos la duración del video al audio
31
+ "-r", "1", "-pix_fmt", "yuv420p", output_filename
32
  ]
33
  subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
34
 
 
40
  except Exception as e:
41
  return f"⚠️ Error: {str(e)}"
42
 
 
43
  with gr.Blocks(theme="Taithrah/Minimal") as app:
44
  gr.Markdown("# 🎶 Audio to Video Tool\nUpload an audio file and an image to generate a simple MP4 video (like TunesToTube)")
45
  with gr.Row():