Update utils.py
Browse files
utils.py
CHANGED
@@ -160,94 +160,56 @@ from moviepy.editor import AudioFileClip, ImageClip, concatenate_videoclips
|
|
160 |
|
161 |
def generate_video(audio_file, images, segments):
|
162 |
try:
|
163 |
-
|
|
|
|
|
164 |
file_extension = os.path.splitext(audio_file.name)[1]
|
165 |
temp_audio_path = tempfile.NamedTemporaryFile(delete=False, suffix=f"{file_extension}")
|
166 |
temp_audio_path.write(audio_file.read())
|
167 |
temp_audio_path.close()
|
168 |
|
169 |
-
# Load the audio file using MoviePy
|
170 |
audio = AudioFileClip(temp_audio_path.name)
|
171 |
|
172 |
-
# Define YouTube-like dimensions (16:9 aspect ratio)
|
173 |
frame_width = 1280
|
174 |
frame_height = 720
|
175 |
|
176 |
video_clips = []
|
177 |
total_segments = len(segments)
|
178 |
-
|
179 |
for i, current_segment in enumerate(segments):
|
180 |
start_time = current_segment["start"]
|
181 |
end_time = current_segment["end"]
|
182 |
-
|
183 |
-
# Calculate the actual duration including any gap until the next segment
|
184 |
if i < total_segments - 1:
|
185 |
-
# If there's a next segment, extend until it starts
|
186 |
next_segment = segments[i + 1]
|
187 |
actual_end_time = next_segment["start"]
|
188 |
else:
|
189 |
-
# For the last segment, use its end time
|
190 |
actual_end_time = end_time
|
191 |
-
|
192 |
-
# Calculate total duration including any gap
|
193 |
segment_duration = actual_end_time - start_time
|
194 |
-
|
195 |
-
print(f"\nProcessing segment {i + 1}/{total_segments}:")
|
196 |
-
print(f" Start time: {start_time}s")
|
197 |
-
print(f" Base end time: {end_time}s")
|
198 |
-
print(f" Actual end time: {actual_end_time}s")
|
199 |
-
print(f" Total duration: {segment_duration}s")
|
200 |
-
print(f" Text: '{current_segment['text']}'")
|
201 |
-
|
202 |
-
# Ensure the image index is within bounds
|
203 |
image_path = images[min(i, len(images) - 1)]
|
204 |
-
|
205 |
-
# Create an ImageClip for the current segment
|
206 |
image_clip = ImageClip(image_path)
|
207 |
-
|
208 |
-
# Resize and pad the image to fit a 16:9 aspect ratio
|
209 |
image_clip = image_clip.resize(height=frame_height).on_color(
|
210 |
size=(frame_width, frame_height),
|
211 |
-
color=(0, 0, 0),
|
212 |
-
pos="center"
|
213 |
)
|
214 |
-
|
215 |
-
|
216 |
-
image_clip = image_clip.set_duration(segment_duration)
|
217 |
-
image_clip = image_clip.set_start(start_time) # Set the start time explicitly
|
218 |
-
|
219 |
video_clips.append(image_clip)
|
220 |
|
221 |
-
# Concatenate all the image clips to form the video
|
222 |
-
print("Concatenating video clips...")
|
223 |
video = concatenate_videoclips(video_clips, method="compose")
|
224 |
-
|
225 |
-
# Add the audio to the video
|
226 |
video = video.set_audio(audio)
|
227 |
|
228 |
-
|
229 |
-
|
230 |
-
video_path = os.path.join(temp_dir, "generated_video.mp4")
|
231 |
-
print(f"Writing video file to {video_path}...")
|
232 |
-
video.write_videofile(video_path, fps=30, codec="libx264", audio_codec="aac")
|
233 |
|
234 |
-
|
235 |
os.remove(temp_audio_path.name)
|
236 |
-
print("Temporary audio file removed.")
|
237 |
|
238 |
return video_path
|
239 |
|
240 |
except Exception as e:
|
241 |
print(f"Error generating video: {e}")
|
242 |
-
return None
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
# Example usage:
|
250 |
-
if __name__ == "__main__":
|
251 |
-
result = generate_images(["a guy in jungle", "a waterfall","greenery"])
|
252 |
-
|
253 |
-
|
|
|
160 |
|
161 |
def generate_video(audio_file, images, segments):
|
162 |
try:
|
163 |
+
video_dir = "./generated_videos"
|
164 |
+
os.makedirs(video_dir, exist_ok=True)
|
165 |
+
|
166 |
file_extension = os.path.splitext(audio_file.name)[1]
|
167 |
temp_audio_path = tempfile.NamedTemporaryFile(delete=False, suffix=f"{file_extension}")
|
168 |
temp_audio_path.write(audio_file.read())
|
169 |
temp_audio_path.close()
|
170 |
|
|
|
171 |
audio = AudioFileClip(temp_audio_path.name)
|
172 |
|
|
|
173 |
frame_width = 1280
|
174 |
frame_height = 720
|
175 |
|
176 |
video_clips = []
|
177 |
total_segments = len(segments)
|
|
|
178 |
for i, current_segment in enumerate(segments):
|
179 |
start_time = current_segment["start"]
|
180 |
end_time = current_segment["end"]
|
181 |
+
|
|
|
182 |
if i < total_segments - 1:
|
|
|
183 |
next_segment = segments[i + 1]
|
184 |
actual_end_time = next_segment["start"]
|
185 |
else:
|
|
|
186 |
actual_end_time = end_time
|
187 |
+
|
|
|
188 |
segment_duration = actual_end_time - start_time
|
189 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
image_path = images[min(i, len(images) - 1)]
|
191 |
+
|
|
|
192 |
image_clip = ImageClip(image_path)
|
|
|
|
|
193 |
image_clip = image_clip.resize(height=frame_height).on_color(
|
194 |
size=(frame_width, frame_height),
|
195 |
+
color=(0, 0, 0),
|
196 |
+
pos="center"
|
197 |
)
|
198 |
+
|
199 |
+
image_clip = image_clip.set_duration(segment_duration).set_start(start_time)
|
|
|
|
|
|
|
200 |
video_clips.append(image_clip)
|
201 |
|
|
|
|
|
202 |
video = concatenate_videoclips(video_clips, method="compose")
|
|
|
|
|
203 |
video = video.set_audio(audio)
|
204 |
|
205 |
+
video_filename = f"generated_video_{uuid.uuid4().hex}.mp4"
|
206 |
+
video_path = os.path.join(video_dir, video_filename)
|
|
|
|
|
|
|
207 |
|
208 |
+
video.write_videofile(video_path, fps=30, codec="libx264", audio_codec="aac")
|
209 |
os.remove(temp_audio_path.name)
|
|
|
210 |
|
211 |
return video_path
|
212 |
|
213 |
except Exception as e:
|
214 |
print(f"Error generating video: {e}")
|
215 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|