arif670 commited on
Commit
6c5f599
·
verified ·
1 Parent(s): 5679db3

Update video_generator.py

Browse files
Files changed (1) hide show
  1. video_generator.py +37 -23
video_generator.py CHANGED
@@ -1,10 +1,15 @@
1
  import os
2
  import numpy as np
3
  from PIL import Image
4
- from moviepy.editor import ImageSequenceClip, concatenate_videoclips, AudioFileClip
5
  from moviepy.config import change_settings
 
6
 
7
- # Configure MoviePy to use proper ImageMagick path
 
 
 
 
8
  change_settings({"IMAGEMAGICK_BINARY": "/usr/bin/convert"})
9
 
10
  def generate_video_pipeline(
@@ -17,45 +22,54 @@ def generate_video_pipeline(
17
  fps: int = 24
18
  ):
19
  os.makedirs(output_dir, exist_ok=True)
20
-
 
 
21
  try:
22
  # Generate initial image
 
23
  image_result = text_to_image_model(prompt)
24
  image = image_result.images[0]
25
  image_path = os.path.join(output_dir, "frame.png")
26
  image.save(image_path)
27
-
28
  # Generate video frames
 
29
  video_frames = image_to_video_model(
30
  prompt,
31
  image=image,
32
  num_frames=int(duration * fps),
33
  num_inference_steps=25
34
  ).frames
35
-
36
- # Convert frames to video
37
  video_path = os.path.join(output_dir, "output.mp4")
38
  clip = ImageSequenceClip([np.array(frame) for frame in video_frames], fps=fps)
39
-
40
- # Add audio if TTS available
41
  if tts_model and prompt:
42
- try:
43
- audio = tts_model.generate(prompt)
44
- audio_path = os.path.join(output_dir, "audio.wav")
45
- audio.save(audio_path)
46
- audio_clip = AudioFileClip(audio_path)
47
- clip = clip.set_audio(audio_clip)
48
- except Exception as audio_error:
49
- print(f"Audio generation skipped: {str(audio_error)}")
50
-
51
- # Write final video
52
  clip.write_videofile(video_path, codec="libx264", audio_codec="aac", logger=None)
 
53
  return video_path
54
-
55
  except Exception as e:
56
- raise RuntimeError(f"Video pipeline error: {str(e)}") from e
 
57
 
58
  finally:
59
- # Cleanup temporary files
60
- if os.path.exists(image_path):
61
- os.remove(image_path)
 
 
 
 
 
 
1
  import os
2
  import numpy as np
3
  from PIL import Image
4
+ from moviepy.editor import ImageSequenceClip, AudioFileClip
5
  from moviepy.config import change_settings
6
+ import logging
7
 
8
+ # Configure logging
9
+ logging.basicConfig(level=logging.INFO)
10
+ logger = logging.getLogger(__name__)
11
+
12
+ # Configure MoviePy
13
  change_settings({"IMAGEMAGICK_BINARY": "/usr/bin/convert"})
14
 
15
  def generate_video_pipeline(
 
22
  fps: int = 24
23
  ):
24
  os.makedirs(output_dir, exist_ok=True)
25
+ image_path = None # Initialize variable
26
+ audio_path = None
27
+
28
  try:
29
  # Generate initial image
30
+ logger.info(f"Generating image for prompt: {prompt}")
31
  image_result = text_to_image_model(prompt)
32
  image = image_result.images[0]
33
  image_path = os.path.join(output_dir, "frame.png")
34
  image.save(image_path)
35
+
36
  # Generate video frames
37
+ logger.info(f"Generating {duration}s video @ {fps}fps")
38
  video_frames = image_to_video_model(
39
  prompt,
40
  image=image,
41
  num_frames=int(duration * fps),
42
  num_inference_steps=25
43
  ).frames
44
+
45
+ # Create video clip
46
  video_path = os.path.join(output_dir, "output.mp4")
47
  clip = ImageSequenceClip([np.array(frame) for frame in video_frames], fps=fps)
48
+
49
+ # Add audio if available
50
  if tts_model and prompt:
51
+ logger.info("Generating audio track")
52
+ audio = tts_model.generate(prompt)
53
+ audio_path = os.path.join(output_dir, "audio.wav")
54
+ audio.save(audio_path)
55
+ audio_clip = AudioFileClip(audio_path)
56
+ clip = clip.set_audio(audio_clip)
57
+
58
+ # Export final video
 
 
59
  clip.write_videofile(video_path, codec="libx264", audio_codec="aac", logger=None)
60
+ logger.info(f"Video generated at {video_path}")
61
  return video_path
62
+
63
  except Exception as e:
64
+ logger.error(f"Pipeline error: {str(e)}", exc_info=True)
65
+ raise RuntimeError(f"Video generation failed: {str(e)}") from e
66
 
67
  finally:
68
+ # Cleanup temporary files safely
69
+ for path in [image_path, audio_path]:
70
+ if path and os.path.exists(path):
71
+ try:
72
+ os.remove(path)
73
+ logger.debug(f"Cleaned up temporary file: {path}")
74
+ except Exception as cleanup_error:
75
+ logger.warning(f"Failed to clean {path}: {str(cleanup_error)}")