Update video_generator.py
Browse files- video_generator.py +30 -25
video_generator.py
CHANGED
@@ -1,15 +1,13 @@
|
|
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
|
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,54 +20,61 @@ def generate_video_pipeline(
|
|
22 |
fps: int = 24
|
23 |
):
|
24 |
os.makedirs(output_dir, exist_ok=True)
|
25 |
-
image_path = None
|
26 |
audio_path = None
|
27 |
|
28 |
try:
|
29 |
-
# Generate
|
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 |
-
#
|
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
|
46 |
video_path = os.path.join(output_dir, "output.mp4")
|
47 |
-
clip = ImageSequenceClip(
|
|
|
|
|
|
|
48 |
|
49 |
-
#
|
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 |
-
|
56 |
-
clip = clip.set_audio(audio_clip)
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
61 |
return video_path
|
62 |
|
63 |
except Exception as e:
|
64 |
-
logger.error(f"
|
65 |
-
raise
|
66 |
|
67 |
finally:
|
68 |
-
#
|
69 |
for path in [image_path, audio_path]:
|
70 |
if path and os.path.exists(path):
|
71 |
try:
|
72 |
os.remove(path)
|
73 |
-
|
74 |
-
|
75 |
-
logger.warning(f"Failed to clean {path}: {str(cleanup_error)}")
|
|
|
1 |
import os
|
2 |
import numpy as np
|
3 |
+
import logging
|
4 |
from PIL import Image
|
5 |
from moviepy.editor import ImageSequenceClip, AudioFileClip
|
6 |
from moviepy.config import change_settings
|
7 |
+
import torch
|
8 |
|
|
|
9 |
logging.basicConfig(level=logging.INFO)
|
10 |
logger = logging.getLogger(__name__)
|
|
|
|
|
11 |
change_settings({"IMAGEMAGICK_BINARY": "/usr/bin/convert"})
|
12 |
|
13 |
def generate_video_pipeline(
|
|
|
20 |
fps: int = 24
|
21 |
):
|
22 |
os.makedirs(output_dir, exist_ok=True)
|
23 |
+
image_path = None
|
24 |
audio_path = None
|
25 |
|
26 |
try:
|
27 |
+
# Generate image with device awareness
|
|
|
28 |
image_result = text_to_image_model(prompt)
|
29 |
image = image_result.images[0]
|
30 |
+
|
31 |
+
# Convert to CPU-compatible format if needed
|
32 |
+
if image_to_video_model.device.type == "cpu":
|
33 |
+
image = image.float()
|
34 |
+
|
35 |
image_path = os.path.join(output_dir, "frame.png")
|
36 |
image.save(image_path)
|
37 |
|
38 |
+
# Video generation
|
|
|
39 |
video_frames = image_to_video_model(
|
40 |
prompt,
|
41 |
image=image,
|
42 |
num_frames=int(duration * fps),
|
43 |
+
num_inference_steps=25,
|
44 |
+
generator=torch.Generator(
|
45 |
+
device=image_to_video_model.device
|
46 |
+
)
|
47 |
).frames
|
48 |
|
49 |
+
# Create video
|
50 |
video_path = os.path.join(output_dir, "output.mp4")
|
51 |
+
clip = ImageSequenceClip(
|
52 |
+
[np.array(frame) for frame in video_frames],
|
53 |
+
fps=fps
|
54 |
+
)
|
55 |
|
56 |
+
# Audio handling
|
57 |
if tts_model and prompt:
|
|
|
58 |
audio = tts_model.generate(prompt)
|
59 |
audio_path = os.path.join(output_dir, "audio.wav")
|
60 |
audio.save(audio_path)
|
61 |
+
clip = clip.set_audio(AudioFileClip(audio_path))
|
|
|
62 |
|
63 |
+
clip.write_videofile(video_path,
|
64 |
+
codec="libx264",
|
65 |
+
audio_codec="aac",
|
66 |
+
logger=None)
|
67 |
return video_path
|
68 |
|
69 |
except Exception as e:
|
70 |
+
logger.error(f"Generation failed: {str(e)}")
|
71 |
+
raise
|
72 |
|
73 |
finally:
|
74 |
+
# Safe cleanup
|
75 |
for path in [image_path, audio_path]:
|
76 |
if path and os.path.exists(path):
|
77 |
try:
|
78 |
os.remove(path)
|
79 |
+
except Exception as e:
|
80 |
+
logger.warning(f"Cleanup failed: {str(e)}")
|
|