Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import tempfile
|
|
4 |
import numpy as np
|
5 |
import cv2
|
6 |
from PIL import Image
|
7 |
-
|
8 |
import random
|
9 |
import time
|
10 |
|
@@ -73,11 +73,27 @@ def add_sparkle_effect(frame, density=0.001):
|
|
73 |
cv2.circle(frame, (int(x), int(y)), size, color, -1)
|
74 |
return frame
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
def create_video(image_path, audio_path, effect_type, output_path, fps=24):
|
77 |
"""Create a video from an image and audio with the selected effect"""
|
78 |
-
#
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
81 |
|
82 |
# Load image
|
83 |
img = cv2.imread(image_path)
|
@@ -128,21 +144,24 @@ def create_video(image_path, audio_path, effect_type, output_path, fps=24):
|
|
128 |
# Release the video writer
|
129 |
out.release()
|
130 |
|
131 |
-
#
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
-
#
|
139 |
-
|
140 |
|
141 |
-
# Clean up temporary file
|
142 |
os.remove(temp_video_path)
|
143 |
-
video_clip.close()
|
144 |
-
audio_clip.close()
|
145 |
-
final_clip.close()
|
146 |
|
147 |
progress_bar.progress(1.0)
|
148 |
return output_path
|
|
|
4 |
import numpy as np
|
5 |
import cv2
|
6 |
from PIL import Image
|
7 |
+
import subprocess
|
8 |
import random
|
9 |
import time
|
10 |
|
|
|
73 |
cv2.circle(frame, (int(x), int(y)), size, color, -1)
|
74 |
return frame
|
75 |
|
76 |
+
def get_audio_duration(audio_path):
|
77 |
+
"""Get the duration of an audio file using ffprobe"""
|
78 |
+
cmd = [
|
79 |
+
'ffprobe',
|
80 |
+
'-v', 'error',
|
81 |
+
'-show_entries', 'format=duration',
|
82 |
+
'-of', 'default=noprint_wrappers=1:nokey=1',
|
83 |
+
audio_path
|
84 |
+
]
|
85 |
+
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
86 |
+
return float(result.stdout.strip())
|
87 |
+
|
88 |
def create_video(image_path, audio_path, effect_type, output_path, fps=24):
|
89 |
"""Create a video from an image and audio with the selected effect"""
|
90 |
+
# Get audio duration
|
91 |
+
try:
|
92 |
+
duration = get_audio_duration(audio_path)
|
93 |
+
except Exception as e:
|
94 |
+
st.error(f"Error getting audio duration: {e}")
|
95 |
+
# Fallback to a default duration if ffprobe fails
|
96 |
+
duration = 30.0
|
97 |
|
98 |
# Load image
|
99 |
img = cv2.imread(image_path)
|
|
|
144 |
# Release the video writer
|
145 |
out.release()
|
146 |
|
147 |
+
# Combine the silent video with audio using ffmpeg directly
|
148 |
+
cmd = [
|
149 |
+
'ffmpeg',
|
150 |
+
'-i', temp_video_path, # Input video file
|
151 |
+
'-i', audio_path, # Input audio file
|
152 |
+
'-map', '0:v', # Use video from first input
|
153 |
+
'-map', '1:a', # Use audio from second input
|
154 |
+
'-c:v', 'copy', # Copy the video codec
|
155 |
+
'-c:a', 'aac', # Use AAC for audio
|
156 |
+
'-shortest', # End when the shortest input ends
|
157 |
+
output_path # Output file
|
158 |
+
]
|
159 |
|
160 |
+
# Run ffmpeg command
|
161 |
+
subprocess.run(cmd, check=True)
|
162 |
|
163 |
+
# Clean up temporary file
|
164 |
os.remove(temp_video_path)
|
|
|
|
|
|
|
165 |
|
166 |
progress_bar.progress(1.0)
|
167 |
return output_path
|