gnosticdev commited on
Commit
4aea5de
verified
1 Parent(s): d068ede

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -31
app.py CHANGED
@@ -5,7 +5,7 @@ import tempfile
5
  import os
6
  from pydub import AudioSegment
7
  import math
8
- import ffmpeg
9
 
10
  # Funci贸n para obtener las voces disponibles
11
  async def get_voices():
@@ -54,43 +54,34 @@ def process_video_with_audio(audio_path, video_path):
54
  return audio_path, None
55
 
56
  # Obtener duraci贸n del video
57
- probe = ffmpeg.probe(video_path)
58
- video_duration = float(probe['format']['duration']) * 1000
59
 
60
  # Calcular repeticiones
61
  repetitions = math.ceil(audio_duration_ms / video_duration)
62
- video_stream = ffmpeg.input(video_path)
63
- repeated_stream = ffmpeg.concat(*([video_stream] * repetitions), v=1, a=0)
64
 
65
- # Calcular el punto de inicio del fade out (3 segundos antes del final)
66
- fade_start = max(0, audio_duration_ms / 1000 - 3)
67
- fade_duration = 3
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- # Aplicar el fade out solo al final
70
- filter_complex = f"[0:v]fade=t=out:st={fade_start}:d={fade_duration}[vout]"
71
-
72
- audio_stream = ffmpeg.input(audio_path)
73
-
74
- # Simplificar el mapeo
75
- output = ffmpeg.output(
76
- repeated_stream, # Input 0: video
77
- audio_stream, # Input 1: audio
78
- output_file,
79
- vcodec='libx264',
80
- acodec='aac',
81
- t=audio_duration_ms / 1000,
82
- filter_complex=filter_complex,
83
- map=['[vout]', '1:a'], # Mapear video y audio
84
- strict='experimental'
85
- )
86
-
87
- ffmpeg.run(output)
88
  return audio_path, output_file
89
 
90
- except ffmpeg.Error as e:
91
- error_msg = "Error processing video"
92
- if hasattr(e, 'stderr') and e.stderr is not None:
93
- error_msg += f": {e.stderr.decode('utf-8', errors='ignore')}"
94
  print(error_msg)
95
  return audio_path, None
96
 
 
5
  import os
6
  from pydub import AudioSegment
7
  import math
8
+ import subprocess
9
 
10
  # Funci贸n para obtener las voces disponibles
11
  async def get_voices():
 
54
  return audio_path, None
55
 
56
  # Obtener duraci贸n del video
57
+ probe_command = ["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", video_path]
58
+ video_duration = float(subprocess.check_output(probe_command).decode("utf-8").strip()) * 1000
59
 
60
  # Calcular repeticiones
61
  repetitions = math.ceil(audio_duration_ms / video_duration)
 
 
62
 
63
+ # Crear el comando FFmpeg para repetir el video y combinar con el audio
64
+ command = [
65
+ "ffmpeg",
66
+ "-y", # Sobrescribir el archivo de salida si existe
67
+ "-stream_loop", str(repetitions), # Repetir el video
68
+ "-i", video_path, # Input del video
69
+ "-i", audio_path, # Input del audio
70
+ "-c:v", "libx264", # C贸dec de video
71
+ "-c:a", "aac", # C贸dec de audio
72
+ "-t", str(audio_duration_ms / 1000), # Duraci贸n del video igual a la del audio
73
+ "-vf", f"fade=t=out:st={max(0, audio_duration_ms / 1000 - 3)}:d=3", # Fade out
74
+ "-map", "0:v", # Mapear el stream de video
75
+ "-map", "1:a", # Mapear el stream de audio
76
+ output_file # Archivo de salida
77
+ ]
78
 
79
+ # Ejecutar el comando FFmpeg
80
+ subprocess.run(command, check=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  return audio_path, output_file
82
 
83
+ except subprocess.CalledProcessError as e:
84
+ error_msg = f"Error processing video: {e.stderr.decode('utf-8', errors='ignore')}"
 
 
85
  print(error_msg)
86
  return audio_path, None
87