Update core/visual_engine.py
Browse files- core/visual_engine.py +664 -448
core/visual_engine.py
CHANGED
@@ -1,41 +1,39 @@
|
|
1 |
# core/visual_engine.py
|
2 |
from PIL import Image, ImageDraw, ImageFont, ImageOps
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import numpy as np
|
6 |
import os
|
7 |
-
import openai
|
8 |
import requests
|
9 |
import io
|
10 |
import time
|
11 |
import random
|
12 |
import logging
|
13 |
|
14 |
-
# --- MoviePy Imports ---
|
15 |
-
from moviepy.editor import (ImageClip, VideoFileClip, concatenate_videoclips, TextClip,
|
16 |
-
CompositeVideoClip, AudioFileClip)
|
17 |
-
import moviepy.video.fx.all as vfx
|
18 |
-
|
19 |
-
# --- MONKEY PATCH for Pillow/MoviePy compatibility ---
|
20 |
-
try:
|
21 |
-
if hasattr(Image, 'Resampling') and hasattr(Image.Resampling, 'LANCZOS'): # Pillow 9+
|
22 |
-
if not hasattr(Image, 'ANTIALIAS'): Image.ANTIALIAS = Image.Resampling.LANCZOS
|
23 |
-
elif hasattr(Image, 'LANCZOS'): # Pillow 8
|
24 |
-
if not hasattr(Image, 'ANTIALIAS'): Image.ANTIALIAS = Image.LANCZOS
|
25 |
-
elif not hasattr(Image, 'ANTIALIAS'):
|
26 |
-
print("WARNING: Pillow version lacks common Resampling attributes or ANTIALIAS. MoviePy effects might fail or look different.")
|
27 |
-
except Exception as e_monkey_patch:
|
28 |
-
print(f"WARNING: An unexpected error occurred during Pillow ANTIALIAS monkey-patch: {e_monkey_patch}")
|
29 |
-
|
30 |
logger = logging.getLogger(__name__)
|
31 |
-
|
32 |
-
# logger.setLevel(logging.DEBUG) # Uncomment for very verbose output during development
|
33 |
|
34 |
-
# ---
|
35 |
ELEVENLABS_CLIENT_IMPORTED = False
|
36 |
-
ElevenLabsAPIClient = None
|
37 |
-
Voice = None
|
38 |
-
VoiceSettings = None
|
39 |
try:
|
40 |
from elevenlabs.client import ElevenLabs as ImportedElevenLabsClient
|
41 |
from elevenlabs import Voice as ImportedVoice, VoiceSettings as ImportedVoiceSettings
|
@@ -43,476 +41,694 @@ try:
|
|
43 |
Voice = ImportedVoice
|
44 |
VoiceSettings = ImportedVoiceSettings
|
45 |
ELEVENLABS_CLIENT_IMPORTED = True
|
46 |
-
logger.info("ElevenLabs client components
|
47 |
-
except
|
48 |
-
logger.warning("ElevenLabs
|
49 |
-
except Exception as e_eleven_import_general:
|
50 |
-
logger.warning(f"General error importing ElevenLabs client components: {e_eleven_import_general}. Audio generation disabled.")
|
51 |
|
|
|
52 |
RUNWAYML_SDK_IMPORTED = False
|
53 |
-
|
54 |
try:
|
55 |
-
|
56 |
-
RunwayMLAPIClientClass = ImportedRunwayMLAPIClientClass
|
57 |
-
RUNWAYML_SDK_IMPORTED = True
|
58 |
-
logger.info("RunwayML SDK (runwayml) imported successfully.")
|
59 |
except ImportError:
|
60 |
-
logger.warning("RunwayML SDK not found
|
61 |
-
except Exception as
|
62 |
-
logger.warning(f"
|
63 |
|
64 |
|
65 |
class VisualEngine:
|
66 |
-
DEFAULT_FONT_SIZE_PIL = 10
|
67 |
-
PREFERRED_FONT_SIZE_PIL = 20
|
68 |
-
VIDEO_OVERLAY_FONT_SIZE = 30
|
69 |
-
VIDEO_OVERLAY_FONT_COLOR = 'white'
|
70 |
-
DEFAULT_MOVIEPY_FONT = 'DejaVu-Sans-Bold' # Common ImageMagick font name
|
71 |
-
PREFERRED_MOVIEPY_FONT = 'Liberation-Sans-Bold'
|
72 |
-
|
73 |
def __init__(self, output_dir="temp_cinegen_media", default_elevenlabs_voice_id="Rachel"):
|
74 |
self.output_dir = output_dir
|
75 |
os.makedirs(self.output_dir, exist_ok=True)
|
76 |
-
|
77 |
-
self.font_filename_pil_preference = "DejaVuSans-Bold.ttf" # More standard Linux font
|
78 |
font_paths_to_try = [
|
79 |
-
self.
|
80 |
-
f"/usr/share/fonts/truetype/dejavu/
|
81 |
-
f"/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",
|
82 |
f"/System/Library/Fonts/Supplemental/Arial.ttf",
|
83 |
f"C:/Windows/Fonts/arial.ttf",
|
84 |
-
f"/usr/local/share/fonts/truetype/mycustomfonts/arial.ttf"
|
85 |
]
|
86 |
-
self.
|
87 |
-
|
88 |
-
self.
|
89 |
-
self.
|
90 |
-
self.
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
self.
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
self.
|
107 |
-
self.dalle_model = "dall-e-3"
|
108 |
-
self.
|
109 |
-
|
110 |
-
self.elevenlabs_api_key = None
|
|
|
|
|
111 |
self.elevenlabs_voice_id = default_elevenlabs_voice_id
|
112 |
if VoiceSettings and ELEVENLABS_CLIENT_IMPORTED:
|
113 |
-
self.
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
logger.error(f"Initial RunwayML client initialization failed (env var RUNWAYML_API_SECRET might be invalid or SDK issue): {e_runway_init_at_startup}")
|
127 |
-
self.USE_RUNWAYML = False # Ensure it's disabled if init fails
|
128 |
-
|
129 |
logger.info("VisualEngine initialized.")
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
self.
|
134 |
-
logger.info(f"DALL-E ({self.dalle_model})
|
135 |
|
136 |
-
def set_elevenlabs_api_key(self,
|
137 |
-
self.elevenlabs_api_key =
|
138 |
-
if voice_id_from_secret:
|
139 |
-
|
140 |
-
if
|
141 |
try:
|
142 |
-
self.
|
143 |
-
self.USE_ELEVENLABS = bool(self.
|
144 |
-
logger.info(f"ElevenLabs Client
|
145 |
-
except Exception as
|
146 |
-
logger.error(f"ElevenLabs client
|
147 |
-
self.USE_ELEVENLABS = False
|
148 |
else:
|
149 |
self.USE_ELEVENLABS = False
|
150 |
-
logger.info(
|
151 |
|
152 |
-
def set_pexels_api_key(self,
|
153 |
-
self.pexels_api_key =
|
154 |
-
|
|
|
155 |
|
156 |
-
def set_runway_api_key(self,
|
157 |
-
self.runway_api_key =
|
158 |
-
if
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
if not original_env_secret: # Clean up: remove env var if we set it
|
173 |
-
del os.environ["RUNWAYML_API_SECRET"]
|
174 |
-
logger.info("Cleared temporary RUNWAYML_API_SECRET environment variable.")
|
175 |
-
except Exception as e_runway_client_setkey_init:
|
176 |
-
logger.error(f"RunwayML Client initialization via set_runway_api_key failed: {e_runway_client_setkey_init}", exc_info=True)
|
177 |
-
self.USE_RUNWAYML = False; self.runway_ml_sdk_client_instance = None # Ensure it's disabled
|
178 |
-
else: # Client was already initialized
|
179 |
-
self.USE_RUNWAYML = True # Service is usable
|
180 |
-
logger.info("RunwayML Client was already initialized (likely from environment variable). API key stored.")
|
181 |
-
else: # SDK not imported
|
182 |
-
logger.warning("RunwayML SDK not imported. API key has been stored, but the current integration relies on the SDK. Service effectively disabled.")
|
183 |
-
self.USE_RUNWAYML = False # Can't use without SDK if that's the implemented path
|
184 |
-
else: # No API key provided
|
185 |
-
self.USE_RUNWAYML = False; self.runway_ml_sdk_client_instance = None
|
186 |
-
logger.info("RunwayML Service Disabled (no API key provided to set_runway_api_key).")
|
187 |
-
|
188 |
-
# --- Helper Methods (_image_to_data_uri, _map_resolution_to_runway_ratio, etc.) ---
|
189 |
-
def _image_to_data_uri(self, image_path):
|
190 |
-
try:
|
191 |
-
mime_type, _ = mimetypes.guess_type(image_path)
|
192 |
-
if not mime_type:
|
193 |
-
ext = os.path.splitext(image_path)[1].lower()
|
194 |
-
mime_map = {".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".webp": "image/webp"}
|
195 |
-
mime_type = mime_map.get(ext, "application/octet-stream")
|
196 |
-
if mime_type == "application/octet-stream": logger.warning(f"Could not determine MIME type for {image_path}, using default.")
|
197 |
-
with open(image_path, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
|
198 |
-
data_uri = f"data:{mime_type};base64,{encoded_string}"
|
199 |
-
logger.debug(f"Generated data URI for {os.path.basename(image_path)} (first 100 chars): {data_uri[:100]}...")
|
200 |
-
return data_uri
|
201 |
-
except FileNotFoundError: logger.error(f"Image file not found at {image_path} for data URI conversion."); return None
|
202 |
-
except Exception as e: logger.error(f"Error converting image {image_path} to data URI: {e}", exc_info=True); return None
|
203 |
-
|
204 |
-
def _map_resolution_to_runway_ratio(self, width, height):
|
205 |
-
ratio_str = f"{width}:{height}"
|
206 |
-
supported_ratios_gen4 = ["1280:720", "720:1280", "1104:832", "832:1104", "960:960", "1584:672"]
|
207 |
-
if ratio_str in supported_ratios_gen4: return ratio_str
|
208 |
-
logger.warning(f"Resolution {ratio_str} not directly in Gen-4 supported list. Defaulting to 1280:720 for RunwayML.")
|
209 |
-
return "1280:720"
|
210 |
|
211 |
-
def _get_text_dimensions(self, text_content,
|
212 |
-
|
213 |
-
if not text_content:
|
|
|
214 |
try:
|
215 |
-
if hasattr(
|
216 |
-
|
217 |
-
|
218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
|
220 |
def _create_placeholder_image_content(self, text_description, filename, size=None):
|
221 |
-
|
222 |
-
|
223 |
-
img = Image.new('RGB', size, color=(20, 20, 40))
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
|
|
|
|
|
|
234 |
else:
|
235 |
-
if
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
271 |
try:
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
data_px = response_px.json()
|
278 |
-
if data_px.get("photos") and len(data_px["photos"]) > 0:
|
279 |
-
photo_details_px = data_px["photos"][0]
|
280 |
-
photo_url_px = photo_details_px.get("src", {}).get("large2x")
|
281 |
-
if not photo_url_px: logger.warning(f"Pexels: 'large2x' URL missing for '{eff_query_px}'. Details: {photo_details_px}"); return None
|
282 |
-
image_response_px = requests.get(photo_url_px, timeout=60); image_response_px.raise_for_status()
|
283 |
-
img_pil_data_px = Image.open(io.BytesIO(image_response_px.content))
|
284 |
-
if img_pil_data_px.mode != 'RGB': img_pil_data_px = img_pil_data_px.convert('RGB')
|
285 |
-
img_pil_data_px.save(file_path_px); logger.info(f"Pexels: Image saved to {file_path_px}"); return file_path_px
|
286 |
-
else: logger.info(f"Pexels: No photos for '{eff_query_px}'."); return None
|
287 |
-
except requests.exceptions.RequestException as e_req_px: logger.error(f"Pexels: RequestException for '{query_str}': {e_req_px}", exc_info=False); return None
|
288 |
-
except Exception as e_px_gen: logger.error(f"Pexels: General error for '{query_str}': {e_px_gen}", exc_info=True); return None
|
289 |
|
290 |
-
def
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
base_name_for_runway_vid, _ = os.path.splitext(scene_identifier_filename_base); output_vid_fn = base_name_for_runway_vid + f"_runway_gen4_d{runway_dur}s.mp4" # Renamed
|
299 |
-
output_vid_fp = os.path.join(self.output_dir, output_vid_fn) # Renamed
|
300 |
-
logger.info(f"Runway Gen-4 task: motion='{text_prompt_for_motion[:100]}...', img='{os.path.basename(input_image_path)}', dur={runway_dur}s, ratio='{runway_ratio}'")
|
301 |
try:
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
328 |
try:
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
336 |
return None
|
337 |
finally:
|
338 |
-
if
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
if
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
else:current_em_px=asset_info_result.get('error_message',"");asset_info_result['error_message']=(current_em_px+" Pexels failed for base.").strip() # Renamed
|
380 |
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
385 |
|
386 |
-
if
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
394 |
|
395 |
-
def generate_narration_audio(self,
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
416 |
|
417 |
def assemble_animatic_from_assets(self, asset_data_list, overall_narration_path=None, output_filename="final_video.mp4", fps=24):
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
if
|
435 |
-
|
436 |
-
|
|
|
|
|
|
|
|
|
|
|
437 |
try:
|
438 |
-
if
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
460 |
try:
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
|
|
|
|
|
|
|
|
|
|
465 |
else:
|
466 |
-
if
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
471 |
finally:
|
472 |
-
if
|
473 |
-
|
474 |
-
|
475 |
-
|
|
|
|
|
|
|
476 |
try:
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
486 |
finally:
|
487 |
-
if
|
488 |
-
try:
|
489 |
-
|
|
|
|
|
490 |
|
491 |
-
if not
|
492 |
-
|
|
|
|
|
493 |
try:
|
494 |
-
logger.info(f"Concatenating {len(
|
495 |
-
if len(
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
if
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
512 |
finally:
|
513 |
-
logger.debug("Closing all MoviePy clips in `assemble_animatic_from_assets`
|
514 |
-
|
515 |
-
for
|
516 |
-
if
|
517 |
-
try:
|
518 |
-
|
|
|
|
|
|
1 |
# core/visual_engine.py
|
2 |
from PIL import Image, ImageDraw, ImageFont, ImageOps
|
3 |
+
# --- MONKEY PATCH FOR Image.ANTIALIAS ---
|
4 |
+
try:
|
5 |
+
if hasattr(Image, 'Resampling') and hasattr(Image.Resampling, 'LANCZOS'): # Pillow 9+
|
6 |
+
if not hasattr(Image, 'ANTIALIAS'):
|
7 |
+
Image.ANTIALIAS = Image.Resampling.LANCZOS
|
8 |
+
elif hasattr(Image, 'LANCZOS'): # Pillow 8
|
9 |
+
if not hasattr(Image, 'ANTIALIAS'):
|
10 |
+
Image.ANTIALIAS = Image.LANCZOS
|
11 |
+
elif not hasattr(Image, 'ANTIALIAS'):
|
12 |
+
print("WARNING: Pillow version lacks common Resampling attributes or ANTIALIAS. Video effects might fail.")
|
13 |
+
except Exception as e_mp:
|
14 |
+
print(f"WARNING: ANTIALIAS monkey-patch error: {e_mp}")
|
15 |
+
# --- END MONKEY PATCH ---
|
16 |
+
|
17 |
+
from moviepy.editor import (ImageClip, VideoFileClip, concatenate_videoclips, TextClip,
|
18 |
+
CompositeVideoClip, AudioFileClip)
|
19 |
+
import moviepy.video.fx.all as vfx
|
20 |
import numpy as np
|
21 |
import os
|
22 |
+
import openai
|
23 |
import requests
|
24 |
import io
|
25 |
import time
|
26 |
import random
|
27 |
import logging
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
logger = logging.getLogger(__name__)
|
30 |
+
logger.setLevel(logging.INFO)
|
|
|
31 |
|
32 |
+
# --- ElevenLabs Client Import ---
|
33 |
ELEVENLABS_CLIENT_IMPORTED = False
|
34 |
+
ElevenLabsAPIClient = None
|
35 |
+
Voice = None
|
36 |
+
VoiceSettings = None
|
37 |
try:
|
38 |
from elevenlabs.client import ElevenLabs as ImportedElevenLabsClient
|
39 |
from elevenlabs import Voice as ImportedVoice, VoiceSettings as ImportedVoiceSettings
|
|
|
41 |
Voice = ImportedVoice
|
42 |
VoiceSettings = ImportedVoiceSettings
|
43 |
ELEVENLABS_CLIENT_IMPORTED = True
|
44 |
+
logger.info("ElevenLabs client components imported.")
|
45 |
+
except Exception as e_eleven:
|
46 |
+
logger.warning(f"ElevenLabs client import failed: {e_eleven}. Audio disabled.")
|
|
|
|
|
47 |
|
48 |
+
# --- RunwayML Client Import (Placeholder) ---
|
49 |
RUNWAYML_SDK_IMPORTED = False
|
50 |
+
RunwayMLClient = None
|
51 |
try:
|
52 |
+
logger.info("RunwayML SDK import is a placeholder.")
|
|
|
|
|
|
|
53 |
except ImportError:
|
54 |
+
logger.warning("RunwayML SDK (placeholder) not found. RunwayML disabled.")
|
55 |
+
except Exception as e_runway_sdk:
|
56 |
+
logger.warning(f"Error importing RunwayML SDK (placeholder): {e_runway_sdk}. RunwayML disabled.")
|
57 |
|
58 |
|
59 |
class VisualEngine:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
def __init__(self, output_dir="temp_cinegen_media", default_elevenlabs_voice_id="Rachel"):
|
61 |
self.output_dir = output_dir
|
62 |
os.makedirs(self.output_dir, exist_ok=True)
|
63 |
+
self.font_filename = "DejaVuSans-Bold.ttf"
|
|
|
64 |
font_paths_to_try = [
|
65 |
+
self.font_filename,
|
66 |
+
f"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
|
67 |
+
f"/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",
|
68 |
f"/System/Library/Fonts/Supplemental/Arial.ttf",
|
69 |
f"C:/Windows/Fonts/arial.ttf",
|
70 |
+
f"/usr/local/share/fonts/truetype/mycustomfonts/arial.ttf"
|
71 |
]
|
72 |
+
self.font_path_pil = next((p for p in font_paths_to_try if os.path.exists(p)), None)
|
73 |
+
self.font_size_pil = 20
|
74 |
+
self.video_overlay_font_size = 30
|
75 |
+
self.video_overlay_font_color = 'white'
|
76 |
+
self.video_overlay_font = 'DejaVu-Sans-Bold'
|
77 |
|
78 |
+
try:
|
79 |
+
if self.font_path_pil:
|
80 |
+
self.font = ImageFont.truetype(self.font_path_pil, self.font_size_pil)
|
81 |
+
logger.info(f"Pillow font loaded: {self.font_path_pil}.")
|
82 |
+
else:
|
83 |
+
self.font = ImageFont.load_default()
|
84 |
+
logger.warning("Using default Pillow font.")
|
85 |
+
self.font_size_pil = 10
|
86 |
+
except IOError as e_font:
|
87 |
+
logger.error(f"Pillow font loading IOError: {e_font}. Using default.")
|
88 |
+
self.font = ImageFont.load_default()
|
89 |
+
self.font_size_pil = 10
|
90 |
+
|
91 |
+
self.openai_api_key = None
|
92 |
+
self.USE_AI_IMAGE_GENERATION = False
|
93 |
+
self.dalle_model = "dall-e-3"
|
94 |
+
self.image_size_dalle3 = "1792x1024"
|
95 |
+
self.video_frame_size = (1280, 720)
|
96 |
+
self.elevenlabs_api_key = None
|
97 |
+
self.USE_ELEVENLABS = False
|
98 |
+
self.elevenlabs_client = None
|
99 |
self.elevenlabs_voice_id = default_elevenlabs_voice_id
|
100 |
if VoiceSettings and ELEVENLABS_CLIENT_IMPORTED:
|
101 |
+
self.elevenlabs_voice_settings = VoiceSettings(
|
102 |
+
stability=0.60,
|
103 |
+
similarity_boost=0.80,
|
104 |
+
style=0.15,
|
105 |
+
use_speaker_boost=True
|
106 |
+
)
|
107 |
+
else:
|
108 |
+
self.elevenlabs_voice_settings = None
|
109 |
+
self.pexels_api_key = None
|
110 |
+
self.USE_PEXELS = False
|
111 |
+
self.runway_api_key = None
|
112 |
+
self.USE_RUNWAYML = False
|
113 |
+
self.runway_client = None
|
|
|
|
|
|
|
114 |
logger.info("VisualEngine initialized.")
|
115 |
|
116 |
+
def set_openai_api_key(self, k):
|
117 |
+
self.openai_api_key = k
|
118 |
+
self.USE_AI_IMAGE_GENERATION = bool(k)
|
119 |
+
logger.info(f"DALL-E ({self.dalle_model}) {'Ready.' if k else 'Disabled.'}")
|
120 |
|
121 |
+
def set_elevenlabs_api_key(self, api_key, voice_id_from_secret=None):
|
122 |
+
self.elevenlabs_api_key = api_key
|
123 |
+
if voice_id_from_secret:
|
124 |
+
self.elevenlabs_voice_id = voice_id_from_secret
|
125 |
+
if api_key and ELEVENLABS_CLIENT_IMPORTED and ElevenLabsAPIClient:
|
126 |
try:
|
127 |
+
self.elevenlabs_client = ElevenLabsAPIClient(api_key=api_key)
|
128 |
+
self.USE_ELEVENLABS = bool(self.elevenlabs_client)
|
129 |
+
logger.info(f"ElevenLabs Client {'Ready' if self.USE_ELEVENLABS else 'Failed Init'} (Voice ID: {self.elevenlabs_voice_id}).")
|
130 |
+
except Exception as e:
|
131 |
+
logger.error(f"ElevenLabs client init error: {e}. Disabled.", exc_info=True)
|
132 |
+
self.USE_ELEVENLABS = False
|
133 |
else:
|
134 |
self.USE_ELEVENLABS = False
|
135 |
+
logger.info("ElevenLabs Disabled (no key or SDK).")
|
136 |
|
137 |
+
def set_pexels_api_key(self, k):
|
138 |
+
self.pexels_api_key = k
|
139 |
+
self.USE_PEXELS = bool(k)
|
140 |
+
logger.info(f"Pexels Search {'Ready.' if k else 'Disabled.'}")
|
141 |
|
142 |
+
def set_runway_api_key(self, k):
|
143 |
+
self.runway_api_key = k
|
144 |
+
if k and RUNWAYML_SDK_IMPORTED and RunwayMLClient:
|
145 |
+
try:
|
146 |
+
self.USE_RUNWAYML = True
|
147 |
+
logger.info(f"RunwayML Client (Placeholder SDK) {'Ready.' if self.USE_RUNWAYML else 'Failed Init.'}")
|
148 |
+
except Exception as e:
|
149 |
+
logger.error(f"RunwayML client (Placeholder SDK) init error: {e}. Disabled.", exc_info=True)
|
150 |
+
self.USE_RUNWAYML = False
|
151 |
+
elif k:
|
152 |
+
self.USE_RUNWAYML = True
|
153 |
+
logger.info("RunwayML API Key set (direct API or placeholder).")
|
154 |
+
else:
|
155 |
+
self.USE_RUNWAYML = False
|
156 |
+
logger.info("RunwayML Disabled (no API key).")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
+
def _get_text_dimensions(self, text_content, font_obj):
|
159 |
+
default_line_height = getattr(font_obj, 'size', self.font_size_pil)
|
160 |
+
if not text_content:
|
161 |
+
return 0, default_line_height
|
162 |
try:
|
163 |
+
if hasattr(font_obj, 'getbbox'):
|
164 |
+
bbox = font_obj.getbbox(text_content)
|
165 |
+
width = bbox[2] - bbox[0]
|
166 |
+
height = bbox[3] - bbox[1]
|
167 |
+
return width, height if height > 0 else default_line_height
|
168 |
+
elif hasattr(font_obj, 'getsize'):
|
169 |
+
width, height = font_obj.getsize(text_content)
|
170 |
+
return width, height if height > 0 else default_line_height
|
171 |
+
else:
|
172 |
+
return int(len(text_content) * default_line_height * 0.6), int(default_line_height * 1.2)
|
173 |
+
except Exception as e:
|
174 |
+
logger.warning(f"Error in _get_text_dimensions for '{text_content[:20]}...': {e}")
|
175 |
+
return int(len(text_content) * self.font_size_pil * 0.6), int(self.font_size_pil * 1.2)
|
176 |
|
177 |
def _create_placeholder_image_content(self, text_description, filename, size=None):
|
178 |
+
if size is None:
|
179 |
+
size = self.video_frame_size
|
180 |
+
img = Image.new('RGB', size, color=(20, 20, 40))
|
181 |
+
draw = ImageDraw.Draw(img)
|
182 |
+
padding = 25
|
183 |
+
max_text_width = size[0] - (2 * padding)
|
184 |
+
lines = []
|
185 |
+
if not text_description:
|
186 |
+
text_description = "(Placeholder: No text description provided)"
|
187 |
+
words = text_description.split()
|
188 |
+
current_line = ""
|
189 |
+
for word in words:
|
190 |
+
test_line = current_line + word + " "
|
191 |
+
line_width_test, _ = self._get_text_dimensions(test_line.strip(), self.font)
|
192 |
+
if line_width_test <= max_text_width:
|
193 |
+
current_line = test_line
|
194 |
else:
|
195 |
+
if current_line.strip():
|
196 |
+
lines.append(current_line.strip())
|
197 |
+
word_width, _ = self._get_text_dimensions(word, self.font)
|
198 |
+
if word_width > max_text_width:
|
199 |
+
avg_char_w = self._get_text_dimensions("A", self.font)[0] or 10
|
200 |
+
chars_that_fit = int(max_text_width / avg_char_w) if avg_char_w > 0 else 10
|
201 |
+
if len(word) > chars_that_fit:
|
202 |
+
lines.append(word[:chars_that_fit-3] + "...")
|
203 |
+
else:
|
204 |
+
lines.append(word)
|
205 |
+
current_line = ""
|
206 |
+
else:
|
207 |
+
current_line = word + " "
|
208 |
+
if current_line.strip():
|
209 |
+
lines.append(current_line.strip())
|
210 |
+
if not lines and text_description:
|
211 |
+
avg_char_w = self._get_text_dimensions("A", self.font)[0] or 10
|
212 |
+
chars_that_fit = int(max_text_width / avg_char_w) if avg_char_w > 0 else 10
|
213 |
+
if len(text_description) > chars_that_fit:
|
214 |
+
lines.append(text_description[:chars_that_fit-3] + "...")
|
215 |
+
else:
|
216 |
+
lines.append(text_description)
|
217 |
+
elif not lines:
|
218 |
+
lines.append("(Placeholder Text Error)")
|
219 |
+
_, single_line_height = self._get_text_dimensions("Ay", self.font)
|
220 |
+
single_line_height = single_line_height if single_line_height > 0 else (self.font_size_pil + 2)
|
221 |
+
line_spacing = 2
|
222 |
+
max_lines_to_display = min(len(lines), (size[1] - (2 * padding)) // (single_line_height + line_spacing)) if single_line_height > 0 else 1
|
223 |
+
if max_lines_to_display <= 0:
|
224 |
+
max_lines_to_display = 1
|
225 |
+
total_text_block_height = max_lines_to_display * single_line_height + (max_lines_to_display - 1) * line_spacing
|
226 |
+
y_text_start = padding + (size[1] - (2 * padding) - total_text_block_height) / 2.0
|
227 |
+
current_y = y_text_start
|
228 |
+
for i in range(max_lines_to_display):
|
229 |
+
line_content = lines[i]
|
230 |
+
line_width_actual, _ = self._get_text_dimensions(line_content, self.font)
|
231 |
+
x_text = max(padding, (size[0] - line_width_actual) / 2.0)
|
232 |
+
draw.text((x_text, current_y), line_content, font=self.font, fill=(200, 200, 180))
|
233 |
+
current_y += single_line_height + line_spacing
|
234 |
+
if i == 6 and max_lines_to_display > 7 and len(lines) > max_lines_to_display:
|
235 |
+
ellipsis_width, _ = self._get_text_dimensions("...", self.font)
|
236 |
+
x_ellipsis = max(padding, (size[0] - ellipsis_width) / 2.0)
|
237 |
+
draw.text((x_ellipsis, current_y), "...", font=self.font, fill=(200, 200, 180))
|
238 |
+
break
|
239 |
+
filepath = os.path.join(self.output_dir, filename)
|
240 |
try:
|
241 |
+
img.save(filepath)
|
242 |
+
return filepath
|
243 |
+
except Exception as e:
|
244 |
+
logger.error(f"Error saving placeholder image {filepath}: {e}", exc_info=True)
|
245 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
246 |
|
247 |
+
def _search_pexels_image(self, query, output_filename_base):
|
248 |
+
if not self.USE_PEXELS or not self.pexels_api_key:
|
249 |
+
return None
|
250 |
+
headers = {"Authorization": self.pexels_api_key}
|
251 |
+
params = {"query": query, "per_page": 1, "orientation": "landscape", "size": "large2x"}
|
252 |
+
base_name, _ = os.path.splitext(output_filename_base)
|
253 |
+
pexels_filename = base_name + f"_pexels_{random.randint(1000,9999)}.jpg"
|
254 |
+
filepath = os.path.join(self.output_dir, pexels_filename)
|
|
|
|
|
|
|
255 |
try:
|
256 |
+
logger.info(f"Pexels search: '{query}'")
|
257 |
+
effective_query = " ".join(query.split()[:5])
|
258 |
+
params["query"] = effective_query
|
259 |
+
response = requests.get("https://api.pexels.com/v1/search", headers=headers, params=params, timeout=20)
|
260 |
+
response.raise_for_status()
|
261 |
+
data = response.json()
|
262 |
+
if data.get("photos") and len(data["photos"]) > 0:
|
263 |
+
photo_details = data["photos"][0]
|
264 |
+
photo_url = photo_details["src"]["large2x"]
|
265 |
+
logger.info(f"Downloading Pexels image from: {photo_url}")
|
266 |
+
image_response = requests.get(photo_url, timeout=60)
|
267 |
+
image_response.raise_for_status()
|
268 |
+
img_data = Image.open(io.BytesIO(image_response.content))
|
269 |
+
if img_data.mode != 'RGB':
|
270 |
+
logger.debug(f"Pexels image mode is {img_data.mode}, converting to RGB.")
|
271 |
+
img_data = img_data.convert('RGB')
|
272 |
+
img_data.save(filepath)
|
273 |
+
logger.info(f"Pexels image saved successfully: {filepath}")
|
274 |
+
return filepath
|
275 |
+
else:
|
276 |
+
logger.info(f"No photos found on Pexels for query: '{effective_query}'")
|
277 |
+
return None
|
278 |
+
except requests.exceptions.RequestException as e_req:
|
279 |
+
logger.error(f"Pexels request error for query '{query}': {e_req}", exc_info=True)
|
280 |
+
except json.JSONDecodeError as e_json:
|
281 |
+
logger.error(f"Pexels JSON decode error for query '{query}': {e_json}", exc_info=True)
|
282 |
+
except Exception as e:
|
283 |
+
logger.error(f"General Pexels error for query '{query}': {e}", exc_info=True)
|
284 |
+
return None
|
285 |
+
|
286 |
+
def _generate_video_clip_with_runwayml(self, pt, iip, sifnb, tds=5):
|
287 |
+
if not self.USE_RUNWAYML or not self.runway_api_key:
|
288 |
+
logger.warning("RunwayML disabled.")
|
289 |
+
return None
|
290 |
+
if not iip or not os.path.exists(iip):
|
291 |
+
logger.error(f"Runway Gen-4 needs input image. Path invalid: {iip}")
|
292 |
+
return None
|
293 |
+
runway_dur = 10 if tds > 7 else 5
|
294 |
+
ovfn = sifnb.replace(".png", f"_runway_gen4_d{runway_dur}s.mp4")
|
295 |
+
ovfp = os.path.join(self.output_dir, ovfn)
|
296 |
+
logger.info(f"Runway Gen-4 (Placeholder) img: {os.path.basename(iip)}, motion: '{pt[:100]}...', dur: {runway_dur}s")
|
297 |
+
logger.warning("Using PLACEHOLDER video for Runway Gen-4.")
|
298 |
+
img_clip = None
|
299 |
+
txt_c = None
|
300 |
+
final_ph_clip = None
|
301 |
try:
|
302 |
+
img_clip = ImageClip(iip).set_duration(runway_dur)
|
303 |
+
txt = f"Runway Gen-4 Placeholder\nInput: {os.path.basename(iip)}\nMotion: {pt[:50]}..."
|
304 |
+
txt_c = TextClip(
|
305 |
+
txt,
|
306 |
+
fontsize=24,
|
307 |
+
color='white',
|
308 |
+
font=self.video_overlay_font,
|
309 |
+
bg_color='rgba(0,0,0,0.5)',
|
310 |
+
size=(self.video_frame_size[0] * 0.8, None),
|
311 |
+
method='caption'
|
312 |
+
).set_duration(runway_dur).set_position('center')
|
313 |
+
final_ph_clip = CompositeVideoClip([img_clip, txt_c], size=img_clip.size)
|
314 |
+
final_ph_clip.write_videofile(ovfp, fps=24, codec='libx264', preset='ultrafast', logger=None, threads=2)
|
315 |
+
logger.info(f"Runway Gen-4 placeholder video: {ovfp}")
|
316 |
+
return ovfp
|
317 |
+
except Exception as e:
|
318 |
+
logger.error(f"Runway Gen-4 placeholder error: {e}", exc_info=True)
|
319 |
return None
|
320 |
finally:
|
321 |
+
if img_clip and hasattr(img_clip, 'close'):
|
322 |
+
img_clip.close()
|
323 |
+
if txt_c and hasattr(txt_c, 'close'):
|
324 |
+
txt_c.close()
|
325 |
+
if final_ph_clip and hasattr(final_ph_clip, 'close'):
|
326 |
+
final_ph_clip.close()
|
327 |
+
|
328 |
+
def _create_placeholder_video_content(self, text_description, filename, duration=4, size=None):
|
329 |
+
if size is None:
|
330 |
+
size = self.video_frame_size
|
331 |
+
filepath = os.path.join(self.output_dir, filename)
|
332 |
+
txt_clip = None
|
333 |
+
try:
|
334 |
+
txt_clip = TextClip(
|
335 |
+
text_description,
|
336 |
+
fontsize=50,
|
337 |
+
color='white',
|
338 |
+
font=self.video_overlay_font,
|
339 |
+
bg_color='black',
|
340 |
+
size=size,
|
341 |
+
method='caption'
|
342 |
+
).set_duration(duration)
|
343 |
+
txt_clip.write_videofile(
|
344 |
+
filepath,
|
345 |
+
fps=24,
|
346 |
+
codec='libx264',
|
347 |
+
preset='ultrafast',
|
348 |
+
logger=None,
|
349 |
+
threads=2
|
350 |
+
)
|
351 |
+
logger.info(f"Generic placeholder video created successfully: {filepath}")
|
352 |
+
return filepath
|
353 |
+
except Exception as e:
|
354 |
+
logger.error(f"Failed to create generic placeholder video {filepath}: {e}", exc_info=True)
|
355 |
+
return None
|
356 |
+
finally:
|
357 |
+
if txt_clip and hasattr(txt_clip, 'close'):
|
358 |
+
try:
|
359 |
+
txt_clip.close()
|
360 |
+
except Exception as e_close:
|
361 |
+
logger.warning(f"Error closing TextClip in _create_placeholder_video_content: {e_close}")
|
|
|
362 |
|
363 |
+
def generate_scene_asset(self, image_generation_prompt_text, motion_prompt_text_for_video,
|
364 |
+
scene_data, scene_identifier_filename_base,
|
365 |
+
generate_as_video_clip=False, runway_target_duration=5):
|
366 |
+
base_name = scene_identifier_filename_base
|
367 |
+
asset_info = {
|
368 |
+
'path': None,
|
369 |
+
'type': 'none',
|
370 |
+
'error': True,
|
371 |
+
'prompt_used': image_generation_prompt_text,
|
372 |
+
'error_message': 'Generation not attempted'
|
373 |
+
}
|
374 |
+
input_image_for_runway_path = None
|
375 |
+
image_filename_for_base = base_name + "_base_image.png"
|
376 |
+
temp_image_asset_info = {
|
377 |
+
'error': True,
|
378 |
+
'prompt_used': image_generation_prompt_text,
|
379 |
+
'error_message': 'Base image generation not attempted'
|
380 |
+
}
|
381 |
|
382 |
+
if self.USE_AI_IMAGE_GENERATION and self.openai_api_key:
|
383 |
+
max_r, att_n = 2, 0
|
384 |
+
for att_n in range(max_r):
|
385 |
+
try:
|
386 |
+
img_fp_dalle = os.path.join(self.output_dir, image_filename_for_base)
|
387 |
+
logger.info(f"Attempt {att_n+1} DALL-E (base img): {image_generation_prompt_text[:100]}...")
|
388 |
+
cl = openai.OpenAI(api_key=self.openai_api_key, timeout=90.0)
|
389 |
+
r = cl.images.generate(
|
390 |
+
model=self.dalle_model,
|
391 |
+
prompt=image_generation_prompt_text,
|
392 |
+
n=1,
|
393 |
+
size=self.image_size_dalle3,
|
394 |
+
quality="hd",
|
395 |
+
response_format="url",
|
396 |
+
style="vivid"
|
397 |
+
)
|
398 |
+
iu = r.data[0].url
|
399 |
+
rp = getattr(r.data[0], 'revised_prompt', None)
|
400 |
+
if rp:
|
401 |
+
logger.info(f"DALL-E revised: {rp[:100]}...")
|
402 |
+
ir = requests.get(iu, timeout=120)
|
403 |
+
ir.raise_for_status()
|
404 |
+
id_img = Image.open(io.BytesIO(ir.content))
|
405 |
+
if id_img.mode != 'RGB':
|
406 |
+
id_img = id_img.convert('RGB')
|
407 |
+
id_img.save(img_fp_dalle)
|
408 |
+
logger.info(f"DALL-E base image: {img_fp_dalle}")
|
409 |
+
input_image_for_runway_path = img_fp_dalle
|
410 |
+
temp_image_asset_info = {
|
411 |
+
'path': img_fp_dalle,
|
412 |
+
'type': 'image',
|
413 |
+
'error': False,
|
414 |
+
'prompt_used': image_generation_prompt_text,
|
415 |
+
'revised_prompt': rp
|
416 |
+
}
|
417 |
+
break
|
418 |
+
except openai.RateLimitError as e:
|
419 |
+
logger.warning(f"OpenAI Rate Limit {att_n+1}: {e}. Retry...")
|
420 |
+
time.sleep(5 * (att_n + 1))
|
421 |
+
temp_image_asset_info['error_message'] = str(e)
|
422 |
+
except Exception as e:
|
423 |
+
logger.error(f"DALL-E error: {e}", exc_info=True)
|
424 |
+
temp_image_asset_info['error_message'] = str(e)
|
425 |
+
break
|
426 |
+
if temp_image_asset_info['error']:
|
427 |
+
logger.warning(f"DALL-E failed after {att_n+1} attempts for base image.")
|
428 |
+
|
429 |
+
if temp_image_asset_info['error'] and self.USE_PEXELS:
|
430 |
+
pqt = scene_data.get('pexels_search_query_감독',
|
431 |
+
f"{scene_data.get('emotional_beat','')} {scene_data.get('setting_description','')}")
|
432 |
+
pp = self._search_pexels_image(pqt, image_filename_for_base)
|
433 |
+
if pp:
|
434 |
+
input_image_for_runway_path = pp
|
435 |
+
temp_image_asset_info = {
|
436 |
+
'path': pp,
|
437 |
+
'type': 'image',
|
438 |
+
'error': False,
|
439 |
+
'prompt_used': f"Pexels: {pqt}"
|
440 |
+
}
|
441 |
+
else:
|
442 |
+
current_em = temp_image_asset_info.get('error_message', "")
|
443 |
+
temp_image_asset_info['error_message'] = (current_em + " Pexels failed.").strip()
|
444 |
+
|
445 |
+
if temp_image_asset_info['error']:
|
446 |
+
logger.warning("Base image (DALL-E/Pexels) failed. Placeholder base image.")
|
447 |
+
ppt = temp_image_asset_info.get('prompt_used', image_generation_prompt_text)
|
448 |
+
php = self._create_placeholder_image_content(f"[Base Img Placeholder] {ppt[:100]}...", image_filename_for_base)
|
449 |
+
if php:
|
450 |
+
input_image_for_runway_path = php
|
451 |
+
temp_image_asset_info = {
|
452 |
+
'path': php,
|
453 |
+
'type': 'image',
|
454 |
+
'error': False,
|
455 |
+
'prompt_used': ppt
|
456 |
+
}
|
457 |
+
else:
|
458 |
+
current_em = temp_image_asset_info.get('error_message', "")
|
459 |
+
temp_image_asset_info['error_message'] = (current_em + " Base placeholder failed.").strip()
|
460 |
+
|
461 |
+
if generate_as_video_clip:
|
462 |
+
if self.USE_RUNWAYML and input_image_for_runway_path:
|
463 |
+
video_path = self._generate_video_clip_with_runwayml(
|
464 |
+
motion_prompt_text_for_video,
|
465 |
+
input_image_for_runway_path,
|
466 |
+
base_name,
|
467 |
+
runway_target_duration
|
468 |
+
)
|
469 |
+
if video_path and os.path.exists(video_path):
|
470 |
+
return {
|
471 |
+
'path': video_path,
|
472 |
+
'type': 'video',
|
473 |
+
'error': False,
|
474 |
+
'prompt_used': motion_prompt_text_for_video,
|
475 |
+
'base_image_path': input_image_for_runway_path
|
476 |
+
}
|
477 |
+
else:
|
478 |
+
asset_info = temp_image_asset_info
|
479 |
+
asset_info['error'] = True
|
480 |
+
asset_info['error_message'] = "RunwayML video gen failed; using base image."
|
481 |
+
asset_info['type'] = 'image'
|
482 |
+
return asset_info
|
483 |
+
elif not self.USE_RUNWAYML:
|
484 |
+
asset_info = temp_image_asset_info
|
485 |
+
asset_info['error_message'] = "RunwayML disabled; using base image."
|
486 |
+
asset_info['type'] = 'image'
|
487 |
+
return asset_info
|
488 |
+
else:
|
489 |
+
asset_info = temp_image_asset_info
|
490 |
+
asset_info['error_message'] = (asset_info.get('error_message', "") + " Base image failed, Runway video not attempted.").strip()
|
491 |
+
asset_info['type'] = 'image'
|
492 |
+
return asset_info
|
493 |
+
else:
|
494 |
+
return temp_image_asset_info
|
495 |
|
496 |
+
def generate_narration_audio(self, ttn, ofn="narration_overall.mp3"):
|
497 |
+
if not self.USE_ELEVENLABS or not self.elevenlabs_client or not ttn:
|
498 |
+
logger.info("11L skip.")
|
499 |
+
return None
|
500 |
+
afp = os.path.join(self.output_dir, ofn)
|
501 |
+
try:
|
502 |
+
logger.info(f"11L audio (Voice:{self.elevenlabs_voice_id}): {ttn[:70]}...")
|
503 |
+
asm = None
|
504 |
+
if hasattr(self.elevenlabs_client, 'text_to_speech') and hasattr(self.elevenlabs_client.text_to_speech, 'stream'):
|
505 |
+
asm = self.elevenlabs_client.text_to_speech.stream
|
506 |
+
logger.info("Using 11L .text_to_speech.stream()")
|
507 |
+
elif hasattr(self.elevenlabs_client, 'generate_stream'):
|
508 |
+
asm = self.elevenlabs_client.generate_stream
|
509 |
+
logger.info("Using 11L .generate_stream()")
|
510 |
+
elif hasattr(self.elevenlabs_client, 'generate'):
|
511 |
+
logger.info("Using 11L .generate()")
|
512 |
+
vp = Voice(voice_id=str(self.elevenlabs_voice_id),
|
513 |
+
settings=self.elevenlabs_voice_settings) if Voice and self.elevenlabs_voice_settings else str(self.elevenlabs_voice_id)
|
514 |
+
ab = self.elevenlabs_client.generate(text=ttn, voice=vp, model="eleven_multilingual_v2")
|
515 |
+
with open(afp, "wb") as f:
|
516 |
+
f.write(ab)
|
517 |
+
logger.info(f"11L audio (non-stream): {afp}")
|
518 |
+
return afp
|
519 |
+
else:
|
520 |
+
logger.error("No 11L audio method.")
|
521 |
+
return None
|
522 |
+
|
523 |
+
if asm:
|
524 |
+
vps = {"voice_id": str(self.elevenlabs_voice_id)}
|
525 |
+
if self.elevenlabs_voice_settings:
|
526 |
+
if hasattr(self.elevenlabs_voice_settings, 'model_dump'):
|
527 |
+
vps["voice_settings"] = self.elevenlabs_voice_settings.model_dump()
|
528 |
+
elif hasattr(self.elevenlabs_voice_settings, 'dict'):
|
529 |
+
vps["voice_settings"] = self.elevenlabs_voice_settings.dict()
|
530 |
+
else:
|
531 |
+
vps["voice_settings"] = self.elevenlabs_voice_settings
|
532 |
+
adi = asm(text=ttn, model_id="eleven_multilingual_v2", **vps)
|
533 |
+
with open(afp, "wb") as f:
|
534 |
+
for c in adi:
|
535 |
+
if c:
|
536 |
+
f.write(c)
|
537 |
+
logger.info(f"11L audio (stream): {afp}")
|
538 |
+
return afp
|
539 |
+
except Exception as e:
|
540 |
+
logger.error(f"11L audio error: {e}", exc_info=True)
|
541 |
+
return None
|
542 |
|
543 |
def assemble_animatic_from_assets(self, asset_data_list, overall_narration_path=None, output_filename="final_video.mp4", fps=24):
|
544 |
+
if not asset_data_list:
|
545 |
+
logger.warning("No assets for animatic.")
|
546 |
+
return None
|
547 |
+
processed_clips = []
|
548 |
+
narration_clip = None
|
549 |
+
final_clip = None
|
550 |
+
logger.info(f"Assembling from {len(asset_data_list)} assets. Frame: {self.video_frame_size}.")
|
551 |
+
|
552 |
+
for i, asset_info in enumerate(asset_data_list):
|
553 |
+
asset_path = asset_info.get('path')
|
554 |
+
asset_type = asset_info.get('type')
|
555 |
+
scene_dur = asset_info.get('duration', 4.5)
|
556 |
+
scene_num = asset_info.get('scene_num', i + 1)
|
557 |
+
key_action = asset_info.get('key_action', '')
|
558 |
+
logger.info(f"S{scene_num}: Path='{asset_path}', Type='{asset_type}', Dur='{scene_dur}'s")
|
559 |
+
|
560 |
+
if not (asset_path and os.path.exists(asset_path)):
|
561 |
+
logger.warning(f"S{scene_num}: Not found '{asset_path}'. Skip.")
|
562 |
+
continue
|
563 |
+
if scene_dur <= 0:
|
564 |
+
logger.warning(f"S{scene_num}: Invalid duration ({scene_dur}s). Skip.")
|
565 |
+
continue
|
566 |
+
|
567 |
+
current_scene_mvpy_clip = None
|
568 |
try:
|
569 |
+
if asset_type == 'image':
|
570 |
+
pil_img = Image.open(asset_path)
|
571 |
+
logger.debug(f"S{scene_num}: Loaded img. Mode:{pil_img.mode}, Size:{pil_img.size}")
|
572 |
+
img_rgba = pil_img.convert('RGBA') if pil_img.mode != 'RGBA' else pil_img.copy()
|
573 |
+
thumb = img_rgba.copy()
|
574 |
+
rf = Image.Resampling.LANCZOS if hasattr(Image.Resampling, 'LANCZOS') else Image.BILINEAR
|
575 |
+
thumb.thumbnail(self.video_frame_size, rf)
|
576 |
+
cv_rgba = Image.new('RGBA', self.video_frame_size, (0, 0, 0, 0))
|
577 |
+
xo = (self.video_frame_size[0] - thumb.width) // 2
|
578 |
+
yo = (self.video_frame_size[1] - thumb.height) // 2
|
579 |
+
cv_rgba.paste(thumb, (xo, yo), thumb)
|
580 |
+
final_rgb_pil = Image.new("RGB", self.video_frame_size, (0, 0, 0))
|
581 |
+
final_rgb_pil.paste(cv_rgba, mask=cv_rgba.split()[3])
|
582 |
+
dbg_path = os.path.join(self.output_dir, f"debug_PRE_NUMPY_S{scene_num}.png")
|
583 |
+
final_rgb_pil.save(dbg_path)
|
584 |
+
logger.info(f"DEBUG: Saved PRE_NUMPY_S{scene_num} to {dbg_path}")
|
585 |
+
frame_np = np.array(final_rgb_pil, dtype=np.uint8)
|
586 |
+
if not frame_np.flags['C_CONTIGUOUS']:
|
587 |
+
frame_np = np.ascontiguousarray(frame_np, dtype=np.uint8)
|
588 |
+
logger.debug(f"S{scene_num}: NumPy for MoviePy. Shape:{frame_np.shape}, DType:{frame_np.dtype}, C-Contig:{frame_np.flags['C_CONTIGUOUS']}")
|
589 |
+
if frame_np.size == 0 or frame_np.ndim != 3 or frame_np.shape[2] != 3:
|
590 |
+
logger.error(f"S{scene_num}: Invalid NumPy. Skip.")
|
591 |
+
continue
|
592 |
+
clip_base = ImageClip(frame_np, transparent=False).set_duration(scene_dur)
|
593 |
+
mvpy_dbg_path = os.path.join(self.output_dir, f"debug_MOVIEPY_FRAME_S{scene_num}.png")
|
594 |
+
clip_base.save_frame(mvpy_dbg_path, t=0.1)
|
595 |
+
logger.info(f"DEBUG: Saved MOVIEPY_FRAME_S{scene_num} to {mvpy_dbg_path}")
|
596 |
+
clip_fx = clip_base
|
597 |
+
try:
|
598 |
+
es = random.uniform(1.03, 1.08)
|
599 |
+
clip_fx = clip_base.fx(
|
600 |
+
vfx.resize,
|
601 |
+
lambda t: 1 + (es - 1) * (t / scene_dur) if scene_dur > 0 else 1
|
602 |
+
).set_position('center')
|
603 |
+
except Exception as e:
|
604 |
+
logger.error(f"S{scene_num} Ken Burns error: {e}", exc_info=False)
|
605 |
+
current_scene_mvpy_clip = clip_fx
|
606 |
+
elif asset_type == 'video':
|
607 |
+
src_clip = None
|
608 |
try:
|
609 |
+
src_clip = VideoFileClip(
|
610 |
+
asset_path,
|
611 |
+
target_resolution=(self.video_frame_size[1], self.video_frame_size[0]) if self.video_frame_size else None,
|
612 |
+
audio=False
|
613 |
+
)
|
614 |
+
tmp_clip = src_clip
|
615 |
+
if src_clip.duration != scene_dur:
|
616 |
+
if src_clip.duration > scene_dur:
|
617 |
+
tmp_clip = src_clip.subclip(0, scene_dur)
|
618 |
else:
|
619 |
+
if scene_dur / src_clip.duration > 1.5 and src_clip.duration > 0.1:
|
620 |
+
tmp_clip = src_clip.loop(duration=scene_dur)
|
621 |
+
else:
|
622 |
+
tmp_clip = src_clip.set_duration(src_clip.duration)
|
623 |
+
logger.info(f"S{scene_num} Video clip ({src_clip.duration:.2f}s) shorter than target ({scene_dur:.2f}s).")
|
624 |
+
current_scene_mvpy_clip = tmp_clip.set_duration(scene_dur)
|
625 |
+
if current_scene_mvpy_clip.size != list(self.video_frame_size):
|
626 |
+
current_scene_mvpy_clip = current_scene_mvpy_clip.resize(self.video_frame_size)
|
627 |
+
except Exception as e:
|
628 |
+
logger.error(f"S{scene_num} Video load error '{asset_path}':{e}", exc_info=True)
|
629 |
+
continue
|
630 |
finally:
|
631 |
+
if src_clip and src_clip is not current_scene_mvpy_clip and hasattr(src_clip, 'close'):
|
632 |
+
src_clip.close()
|
633 |
+
else:
|
634 |
+
logger.warning(f"S{scene_num} Unknown asset type '{asset_type}'. Skip.")
|
635 |
+
continue
|
636 |
+
|
637 |
+
if current_scene_mvpy_clip and key_action:
|
638 |
try:
|
639 |
+
to_dur = min(current_scene_mvpy_clip.duration - 0.5, current_scene_mvpy_clip.duration * 0.8) if current_scene_mvpy_clip.duration > 0.5 else current_scene_mvpy_clip.duration
|
640 |
+
to_start = 0.25
|
641 |
+
txt_c = TextClip(
|
642 |
+
f"Scene {scene_num}\n{key_action}",
|
643 |
+
fontsize=self.video_overlay_font_size,
|
644 |
+
color=self.video_overlay_font_color,
|
645 |
+
font=self.video_overlay_font,
|
646 |
+
bg_color='rgba(10,10,20,0.7)',
|
647 |
+
method='caption',
|
648 |
+
align='West',
|
649 |
+
size=(self.video_frame_size[0] * 0.9, None),
|
650 |
+
kerning=-1,
|
651 |
+
stroke_color='black',
|
652 |
+
stroke_width=1.5
|
653 |
+
).set_duration(to_dur).set_start(to_start).set_position(('center', 0.92), relative=True)
|
654 |
+
current_scene_mvpy_clip = CompositeVideoClip(
|
655 |
+
[current_scene_mvpy_clip, txt_c],
|
656 |
+
size=self.video_frame_size,
|
657 |
+
use_bgclip=True
|
658 |
+
)
|
659 |
+
except Exception as e:
|
660 |
+
logger.error(f"S{scene_num} TextClip error:{e}. No text.", exc_info=True)
|
661 |
+
|
662 |
+
if current_scene_mvpy_clip:
|
663 |
+
processed_clips.append(current_scene_mvpy_clip)
|
664 |
+
logger.info(f"S{scene_num} Processed. Dur:{current_scene_mvpy_clip.duration:.2f}s.")
|
665 |
+
except Exception as e:
|
666 |
+
logger.error(f"MAJOR Error S{scene_num} ({asset_path}):{e}", exc_info=True)
|
667 |
finally:
|
668 |
+
if current_scene_mvpy_clip and hasattr(current_scene_mvpy_clip, 'close'):
|
669 |
+
try:
|
670 |
+
current_scene_mvpy_clip.close()
|
671 |
+
except:
|
672 |
+
pass
|
673 |
|
674 |
+
if not processed_clips:
|
675 |
+
logger.warning("No clips processed. Abort.")
|
676 |
+
return None
|
677 |
+
td = 0.75
|
678 |
try:
|
679 |
+
logger.info(f"Concatenating {len(processed_clips)} clips.")
|
680 |
+
if len(processed_clips) > 1:
|
681 |
+
final_clip = concatenate_videoclips(processed_clips, padding=-td if td > 0 else 0, method="compose")
|
682 |
+
elif processed_clips:
|
683 |
+
final_clip = processed_clips[0]
|
684 |
+
if not final_clip:
|
685 |
+
logger.error("Concatenation failed.")
|
686 |
+
return None
|
687 |
+
logger.info(f"Concatenated dur:{final_clip.duration:.2f}s")
|
688 |
+
if td > 0 and final_clip.duration > 0:
|
689 |
+
if final_clip.duration > td * 2:
|
690 |
+
final_clip = final_clip.fx(vfx.fadein, td).fx(vfx.fadeout, td)
|
691 |
+
else:
|
692 |
+
final_clip = final_clip.fx(vfx.fadein, min(td, final_clip.duration / 2.0))
|
693 |
+
if overall_narration_path and os.path.exists(overall_narration_path) and final_clip.duration > 0:
|
694 |
+
try:
|
695 |
+
narration_clip = AudioFileClip(overall_narration_path)
|
696 |
+
final_clip = final_clip.set_audio(narration_clip)
|
697 |
+
logger.info("Narration added.")
|
698 |
+
except Exception as e:
|
699 |
+
logger.error(f"Narration add error:{e}", exc_info=True)
|
700 |
+
elif final_clip.duration <= 0:
|
701 |
+
logger.warning("Video no duration. No audio.")
|
702 |
+
if final_clip and final_clip.duration > 0:
|
703 |
+
op = os.path.join(self.output_dir, output_filename)
|
704 |
+
logger.info(f"Writing video:{op} (Dur:{final_clip.duration:.2f}s)")
|
705 |
+
final_clip.write_videofile(
|
706 |
+
op,
|
707 |
+
fps=fps,
|
708 |
+
codec='libx264',
|
709 |
+
preset='medium',
|
710 |
+
audio_codec='aac',
|
711 |
+
temp_audiofile=os.path.join(self.output_dir, f'temp-audio-{os.urandom(4).hex()}.m4a'),
|
712 |
+
remove_temp=True,
|
713 |
+
threads=os.cpu_count() or 2,
|
714 |
+
logger='bar',
|
715 |
+
bitrate="5000k",
|
716 |
+
ffmpeg_params=["-pix_fmt", "yuv420p"]
|
717 |
+
)
|
718 |
+
logger.info(f"Video created:{op}")
|
719 |
+
return op
|
720 |
+
else:
|
721 |
+
logger.error("Final clip invalid. No write.")
|
722 |
+
return None
|
723 |
+
except Exception as e:
|
724 |
+
logger.error(f"Video write error:{e}", exc_info=True)
|
725 |
+
return None
|
726 |
finally:
|
727 |
+
logger.debug("Closing all MoviePy clips in `assemble_animatic_from_assets` finally block.")
|
728 |
+
clips_to_close = processed_clips + ([narration_clip] if narration_clip else []) + ([final_clip] if final_clip else [])
|
729 |
+
for clip_obj in clips_to_close:
|
730 |
+
if clip_obj and hasattr(clip_obj, 'close'):
|
731 |
+
try:
|
732 |
+
clip_obj.close()
|
733 |
+
except Exception as e_close:
|
734 |
+
logger.warning(f"Ignoring error while closing a clip: {e_close}")
|