gnosticdev commited on
Commit
c220000
·
verified ·
1 Parent(s): 796d198

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +51 -20
utils.py CHANGED
@@ -71,25 +71,20 @@ SYSTEM PROMPT: Given the Overall Summary and All Chunks of the Text:
71
  - For example, if the theme is a dark jungle, use deep greens, blacks, misty blues, and dim moonlight.
72
  - Ensure that all visual elements (fog, shadows, expressions) support the horror/suspense atmosphere.
73
  8. NEVER generate prompts that could lead to NSFW images or any explicit content. Use safe and appropriate descriptions.
74
-
75
  ### Example:
76
  **Summary:**
77
  This text is a story of a man who ventured into a dark jungle and encountered a mysterious lion.
78
-
79
  **Chunks:**
80
  1. A man enters the dark jungle, mist swirling around him.
81
  2. He comes face-to-face with a majestic yet eerie lion.
82
-
83
  **Combined Context:**
84
  "A man ventures into a dense, eerie jungle and unexpectedly meets a mysterious lion."
85
-
86
  **Generated Prompts:**
87
  - **Chunk 1:**
88
  "[style: 3D | theme: dark jungle] A lone man steps into a dense, eerie jungle at twilight. Thick mist swirls around his feet as towering, twisted trees loom overhead. Dim, bluish moonlight filters through the foliage, casting long, haunting shadows."
89
 
90
  - **Chunk 2:**
91
  "[style: 3D | theme: dark jungle] In a clearing within the jungle, a majestic lion appears with an unsettling aura. Its eyes glow faintly in the dim light, and the surrounding trees seem to lean in, enhancing the mysterious tension."
92
-
93
  TASK: Here is the summary: {summary}\n\n and \n\n Total of {chunks_count} chunks, generate an Image Prompt for each chunk\n\n {chunks}
94
  """
95
  result = extractor.extract(prompt)
@@ -160,56 +155,92 @@ from moviepy.editor import AudioFileClip, ImageClip, concatenate_videoclips
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
 
 
 
 
 
 
 
 
 
 
71
  - For example, if the theme is a dark jungle, use deep greens, blacks, misty blues, and dim moonlight.
72
  - Ensure that all visual elements (fog, shadows, expressions) support the horror/suspense atmosphere.
73
  8. NEVER generate prompts that could lead to NSFW images or any explicit content. Use safe and appropriate descriptions.
 
74
  ### Example:
75
  **Summary:**
76
  This text is a story of a man who ventured into a dark jungle and encountered a mysterious lion.
 
77
  **Chunks:**
78
  1. A man enters the dark jungle, mist swirling around him.
79
  2. He comes face-to-face with a majestic yet eerie lion.
 
80
  **Combined Context:**
81
  "A man ventures into a dense, eerie jungle and unexpectedly meets a mysterious lion."
 
82
  **Generated Prompts:**
83
  - **Chunk 1:**
84
  "[style: 3D | theme: dark jungle] A lone man steps into a dense, eerie jungle at twilight. Thick mist swirls around his feet as towering, twisted trees loom overhead. Dim, bluish moonlight filters through the foliage, casting long, haunting shadows."
85
 
86
  - **Chunk 2:**
87
  "[style: 3D | theme: dark jungle] In a clearing within the jungle, a majestic lion appears with an unsettling aura. Its eyes glow faintly in the dim light, and the surrounding trees seem to lean in, enhancing the mysterious tension."
 
88
  TASK: Here is the summary: {summary}\n\n and \n\n Total of {chunks_count} chunks, generate an Image Prompt for each chunk\n\n {chunks}
89
  """
90
  result = extractor.extract(prompt)
 
155
 
156
  def generate_video(audio_file, images, segments):
157
  try:
158
+ # Save the uploaded audio file to a temporary location
 
 
159
  file_extension = os.path.splitext(audio_file.name)[1]
160
  temp_audio_path = tempfile.NamedTemporaryFile(delete=False, suffix=f"{file_extension}")
161
  temp_audio_path.write(audio_file.read())
162
  temp_audio_path.close()
163
 
164
+ # Load the audio file using MoviePy
165
  audio = AudioFileClip(temp_audio_path.name)
166
 
167
+ # Define YouTube-like dimensions (16:9 aspect ratio)
168
  frame_width = 1280
169
  frame_height = 720
170
 
171
  video_clips = []
172
  total_segments = len(segments)
173
+
174
  for i, current_segment in enumerate(segments):
175
  start_time = current_segment["start"]
176
  end_time = current_segment["end"]
177
+
178
+ # Calculate the actual duration including any gap until the next segment
179
  if i < total_segments - 1:
180
+ # If there's a next segment, extend until it starts
181
  next_segment = segments[i + 1]
182
  actual_end_time = next_segment["start"]
183
  else:
184
+ # For the last segment, use its end time
185
  actual_end_time = end_time
186
+
187
+ # Calculate total duration including any gap
188
  segment_duration = actual_end_time - start_time
189
+
190
+ print(f"\nProcessing segment {i + 1}/{total_segments}:")
191
+ print(f" Start time: {start_time}s")
192
+ print(f" Base end time: {end_time}s")
193
+ print(f" Actual end time: {actual_end_time}s")
194
+ print(f" Total duration: {segment_duration}s")
195
+ print(f" Text: '{current_segment['text']}'")
196
+
197
+ # Ensure the image index is within bounds
198
  image_path = images[min(i, len(images) - 1)]
199
+
200
+ # Create an ImageClip for the current segment
201
  image_clip = ImageClip(image_path)
202
+
203
+ # Resize and pad the image to fit a 16:9 aspect ratio
204
  image_clip = image_clip.resize(height=frame_height).on_color(
205
  size=(frame_width, frame_height),
206
+ color=(0, 0, 0), # Black background
207
+ pos="center" # Center the image
208
  )
209
+
210
+ # Set the duration and start time for the clip
211
+ image_clip = image_clip.set_duration(segment_duration)
212
+ image_clip = image_clip.set_start(start_time) # Set the start time explicitly
213
+
214
  video_clips.append(image_clip)
215
 
216
+ # Concatenate all the image clips to form the video
217
+ print("Concatenating video clips...")
218
  video = concatenate_videoclips(video_clips, method="compose")
 
219
 
220
+ # Add the audio to the video
221
+ video = video.set_audio(audio)
222
 
223
+ # Save the video to a temporary file
224
+ temp_dir = tempfile.gettempdir()
225
+ video_path = os.path.join(temp_dir, "generated_video.mp4")
226
+ print(f"Writing video file to {video_path}...")
227
  video.write_videofile(video_path, fps=30, codec="libx264", audio_codec="aac")
228
+
229
+ # Clean up the temporary audio file
230
  os.remove(temp_audio_path.name)
231
+ print("Temporary audio file removed.")
232
 
233
  return video_path
234
 
235
  except Exception as e:
236
  print(f"Error generating video: {e}")
237
+ return None
238
+
239
+
240
+
241
+
242
+
243
+
244
+ # Example usage:
245
+ if __name__ == "__main__":
246
+ result = generate_images(["a guy in jungle", "a waterfall","greenery"])