Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import tempfile
|
|
5 |
import os
|
6 |
from pydub import AudioSegment
|
7 |
import math
|
8 |
-
import
|
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 |
-
|
58 |
-
video_duration = float(
|
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 |
-
#
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
#
|
70 |
-
|
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
|
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 |
|