File size: 36,826 Bytes
287c9ca
e0b9b11
92cb699
 
 
5089920
 
 
 
 
 
92cb699
 
5089920
9840152
5089920
990e23e
92cb699
 
 
 
 
5089920
92cb699
200c5c4
 
5089920
f13d4b2
5089920
f13d4b2
5089920
 
 
f13d4b2
5089920
f13d4b2
5089920
e0b9b11
 
 
5089920
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c2220b
f13d4b2
287c9ca
92cb699
e0b9b11
 
5089920
 
 
 
 
 
 
 
 
 
 
 
 
e0b9b11
5089920
e0b9b11
 
5089920
 
 
e0b9b11
5089920
e0b9b11
f02ab98
5089920
 
e0b9b11
 
5089920
92cb699
5089920
 
 
e0b9b11
f02ab98
5089920
 
 
 
 
 
200c5c4
09d5c67
5089920
e0b9b11
5089920
e0b9b11
92cb699
f13d4b2
5089920
 
 
 
 
 
 
 
 
e0b9b11
 
5089920
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0b9b11
5089920
 
50c620f
5089920
e0b9b11
 
5089920
e0b9b11
 
5089920
e0b9b11
 
 
 
5089920
 
e0b9b11
5089920
92cb699
 
 
 
 
5089920
92cb699
29c2122
5089920
e0b9b11
 
5089920
e0b9b11
 
200c5c4
5089920
e0b9b11
 
 
5089920
 
e0b9b11
92cb699
 
 
 
5089920
92cb699
 
f13d4b2
5089920
e0b9b11
5089920
9840152
200c5c4
5089920
 
92cb699
9840152
92cb699
 
 
f13d4b2
5089920
92cb699
 
 
 
e0b9b11
92cb699
9840152
b97795f
5089920
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09d5c67
92cb699
 
9d84ba9
5089920
200c5c4
5089920
92cb699
 
 
 
5089920
92cb699
5089920
 
 
200c5c4
92cb699
5089920
200c5c4
 
92cb699
5089920
 
 
92cb699
5089920
 
 
 
 
 
 
 
 
 
 
 
 
29c2122
e0b9b11
5089920
f13d4b2
e0b9b11
f13d4b2
5089920
9840152
 
754c854
5089920
 
200c5c4
5089920
200c5c4
5089920
 
 
 
 
 
 
 
200c5c4
5089920
 
200c5c4
5089920
 
 
 
200c5c4
5089920
92cb699
5089920
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92cb699
5089920
92cb699
 
9840152
 
5089920
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92cb699
5089920
 
 
 
 
 
 
 
8583908
5089920
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92cb699
5089920
 
92cb699
5089920
 
 
 
 
 
 
 
 
b97795f
5089920
 
 
 
 
 
 
 
 
 
 
 
 
 
200c5c4
 
 
5089920
 
 
 
e0b9b11
5089920
 
 
 
92cb699
5089920
 
 
 
 
 
 
 
 
 
 
754c854
5089920
 
5e4272a
5089920
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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
329
330
331
332
333
334
335
336
337
338
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# core/visual_engine.py
from PIL import Image, ImageDraw, ImageFont, ImageOps
# --- MONKEY PATCH FOR Image.ANTIALIAS ---
try:
    if hasattr(Image, 'Resampling') and hasattr(Image.Resampling, 'LANCZOS'): # Pillow 9+
        if not hasattr(Image, 'ANTIALIAS'): Image.ANTIALIAS = Image.Resampling.LANCZOS
    elif hasattr(Image, 'LANCZOS'): # Pillow 8
         if not hasattr(Image, 'ANTIALIAS'): Image.ANTIALIAS = Image.LANCZOS
    elif not hasattr(Image, 'ANTIALIAS'):
             print("WARNING: Pillow version lacks common Resampling attributes or ANTIALIAS. Video effects might fail.")
except Exception as e_mp: print(f"WARNING: ANTIALIAS monkey-patch error: {e_mp}")
# --- END MONKEY PATCH ---

from moviepy.editor import (ImageClip, VideoFileClip, concatenate_videoclips, TextClip,
                            CompositeVideoClip, AudioFileClip)
import moviepy.video.fx.all as vfx
import numpy as np
import os
import openai
import requests
import io
import time
import random
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# --- ElevenLabs Client Import ---
ELEVENLABS_CLIENT_IMPORTED = False
ElevenLabsAPIClient = None
Voice = None
VoiceSettings = None
try:
    from elevenlabs.client import ElevenLabs as ImportedElevenLabsClient
    from elevenlabs import Voice as ImportedVoice, VoiceSettings as ImportedVoiceSettings
    ElevenLabsAPIClient = ImportedElevenLabsClient
    Voice = ImportedVoice
    VoiceSettings = ImportedVoiceSettings
    ELEVENLABS_CLIENT_IMPORTED = True
    logger.info("ElevenLabs client components imported.")
except Exception as e_eleven:
    logger.warning(f"ElevenLabs client import failed: {e_eleven}. Audio generation disabled.")

# --- RunwayML Client Import (Placeholder) ---
RUNWAYML_SDK_IMPORTED = False
RunwayMLClient = None # Placeholder for the actual RunwayML client class
try:
    # This is a hypothetical import. Replace with actual RunwayML SDK import if available.
    # Example: from runwayml import RunwayClient as ImportedRunwayMLClient
    # RunwayMLClient = ImportedRunwayMLClient
    # RUNWAYML_SDK_IMPORTED = True
    # logger.info("RunwayML SDK (placeholder) imported.")
    logger.info("RunwayML SDK import is a placeholder. Actual SDK needed for Runway features.")
except ImportError:
    logger.warning("RunwayML SDK (placeholder) not found. RunwayML video generation will be disabled.")
except Exception as e_runway_sdk:
    logger.warning(f"Error importing RunwayML SDK (placeholder): {e_runway_sdk}. RunwayML features disabled.")


class VisualEngine:
    def __init__(self, output_dir="temp_cinegen_media", default_elevenlabs_voice_id="Rachel"):
        self.output_dir = output_dir
        os.makedirs(self.output_dir, exist_ok=True)

        self.font_filename = "arial.ttf"
        font_paths_to_try = [
            self.font_filename,
            f"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
            f"/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",
            f"/System/Library/Fonts/Supplemental/Arial.ttf",
            f"C:/Windows/Fonts/arial.ttf",
            f"/usr/local/share/fonts/truetype/mycustomfonts/{self.font_filename}"
        ]
        self.font_path_pil = next((p for p in font_paths_to_try if os.path.exists(p)), None)
        self.font_size_pil = 20
        self.video_overlay_font_size = 30
        self.video_overlay_font_color = 'white'
        self.video_overlay_font = 'Liberation-Sans-Bold' # For MoviePy TextClip

        try:
            self.font = ImageFont.truetype(self.font_path_pil, self.font_size_pil) if self.font_path_pil else ImageFont.load_default()
            if self.font_path_pil: logger.info(f"Pillow font loaded: {self.font_path_pil}.")
            else: logger.warning("Using default Pillow font."); self.font_size_pil = 10
        except IOError:
            logger.warning("Pillow font error. Using default."); self.font = ImageFont.load_default(); self.font_size_pil = 10

        self.openai_api_key = None; self.USE_AI_IMAGE_GENERATION = False
        self.dalle_model = "dall-e-3"; self.image_size_dalle3 = "1792x1024"
        self.video_frame_size = (1280, 720)

        self.elevenlabs_api_key = None; self.USE_ELEVENLABS = False
        self.elevenlabs_client = None
        self.elevenlabs_voice_id = default_elevenlabs_voice_id
        if VoiceSettings and ELEVENLABS_CLIENT_IMPORTED:
            self.elevenlabs_voice_settings = VoiceSettings(stability=0.60, similarity_boost=0.80, style=0.15, use_speaker_boost=True)
        else: self.elevenlabs_voice_settings = None

        self.pexels_api_key = None; self.USE_PEXELS = False

        # <<< RUNWAYML START >>>
        self.runway_api_key = None; self.USE_RUNWAYML = False
        self.runway_client = None # Placeholder for the actual RunwayML client instance
        # <<< RUNWAYML END >>>

        logger.info("VisualEngine initialized.")

    def set_openai_api_key(self,k):
        self.openai_api_key=k; self.USE_AI_IMAGE_GENERATION=bool(k)
        logger.info(f"DALL-E ({self.dalle_model}) {'Ready.' if k else 'Disabled.'}")

    def set_elevenlabs_api_key(self,api_key, voice_id_from_secret=None):
        self.elevenlabs_api_key=api_key
        if voice_id_from_secret: self.elevenlabs_voice_id = voice_id_from_secret
        if api_key and ELEVENLABS_CLIENT_IMPORTED and ElevenLabsAPIClient:
            try:
                self.elevenlabs_client = ElevenLabsAPIClient(api_key=api_key)
                self.USE_ELEVENLABS=bool(self.elevenlabs_client)
                logger.info(f"ElevenLabs Client {'Ready' if self.USE_ELEVENLABS else 'Failed Init'} (Voice ID: {self.elevenlabs_voice_id}).")
            except Exception as e: logger.error(f"ElevenLabs client init error: {e}. Disabled.", exc_info=True); self.USE_ELEVENLABS=False
        else: self.USE_ELEVENLABS=False; logger.info("ElevenLabs Disabled (no key or SDK).")

    def set_pexels_api_key(self,k):
        self.pexels_api_key=k; self.USE_PEXELS=bool(k)
        logger.info(f"Pexels Search {'Ready.' if k else 'Disabled.'}")

    # <<< RUNWAYML START >>>
    def set_runway_api_key(self, k):
        self.runway_api_key = k
        if k and RUNWAYML_SDK_IMPORTED and RunwayMLClient: # Assuming RunwayMLClient is the SDK's client class
            try:
                # self.runway_client = RunwayMLClient(api_key=k) # Actual initialization
                self.USE_RUNWAYML = True # Assume success for placeholder
                logger.info(f"RunwayML Client (Placeholder) {'Ready.' if self.USE_RUNWAYML else 'Failed Init.'}")
            except Exception as e:
                logger.error(f"RunwayML client (Placeholder) init error: {e}. Disabled.", exc_info=True)
                self.USE_RUNWAYML = False
        elif k and not (RUNWAYML_SDK_IMPORTED and RunwayMLClient):
            self.USE_RUNWAYML = True # Allow use with direct HTTP requests if SDK isn't used/available
            logger.info("RunwayML API Key set. SDK (Placeholder) not imported/used. Direct API calls would be needed.")
        else:
            self.USE_RUNWAYML = False
            logger.info("RunwayML Disabled (no API key or SDK issue).")
    # <<< RUNWAYML END >>>

    def _get_text_dimensions(self,text_content,font_obj):
        # ... (no changes from your previous version)
        if not text_content: return 0,self.font_size_pil
        try:
            if hasattr(font_obj,'getbbox'): # Pillow 8.0.0+
                bbox=font_obj.getbbox(text_content);w=bbox[2]-bbox[0];h=bbox[3]-bbox[1]
                return w, h if h > 0 else self.font_size_pil
            elif hasattr(font_obj,'getsize'): # Older Pillow
                w,h=font_obj.getsize(text_content)
                return w, h if h > 0 else self.font_size_pil
            else: # Should not happen with standard ImageFont objects
                return int(len(text_content)*self.font_size_pil*0.6),int(self.font_size_pil*1.2 if self.font_size_pil*1.2>0 else self.font_size_pil)
        except Exception as e:
            logger.warning(f"Error in _get_text_dimensions for '{text_content[:20]}...': {e}")
            return int(len(text_content)*self.font_size_pil*0.6),int(self.font_size_pil*1.2) # Fallback


    def _create_placeholder_image_content(self,text_description,filename,size=None):
        # ... (no changes from your previous version, ensure filename includes extension e.g. .png)
        if size is None: size = self.video_frame_size
        img=Image.new('RGB',size,color=(20,20,40));d=ImageDraw.Draw(img);padding=25;max_w=size[0]-(2*padding);lines=[];
        if not text_description: text_description="(Placeholder: No prompt text)"
        words=text_description.split();current_line=""
        for word in words:
            test_line=current_line+word+" ";
            if self._get_text_dimensions(test_line,self.font)[0] <= max_w: current_line=test_line
            else:
                if current_line: lines.append(current_line.strip());
                current_line=word+" "
        if current_line.strip(): lines.append(current_line.strip()) # Add last line
        if not lines and text_description: lines.append(text_description[:int(max_w//(self.font_size_pil*0.6 +1))]+"..." if text_description else "(Text too long)") # Handle single very long word
        elif not lines: lines.append("(Placeholder Text Error)")

        _,single_line_h=self._get_text_dimensions("Ay",self.font); single_line_h = single_line_h if single_line_h > 0 else self.font_size_pil + 2

        max_lines_to_display=min(len(lines),(size[1]-(2*padding))//(single_line_h+2)) if single_line_h > 0 else 1
        if max_lines_to_display <=0: max_lines_to_display = 1 # Ensure at least one line can be attempted

        y_text_start = padding + (size[1]-(2*padding) - max_lines_to_display*(single_line_h+2))/2.0
        y_text = y_text_start

        for i in range(max_lines_to_display):
            line_content=lines[i];line_w,_=self._get_text_dimensions(line_content,self.font);x_text=(size[0]-line_w)/2.0
            d.text((x_text,y_text),line_content,font=self.font,fill=(200,200,180));y_text+=single_line_h+2
            if i==6 and max_lines_to_display > 7: d.text((x_text,y_text),"...",font=self.font,fill=(200,200,180));break
        filepath=os.path.join(self.output_dir,filename);
        try:img.save(filepath);return filepath
        except Exception as e:logger.error(f"Saving placeholder image {filepath}: {e}", exc_info=True);return None


    def _search_pexels_image(self, query, output_filename_base):
        # ... (no changes from your previous version, ensure output_filename_base has .png for consistency, it will be replaced)
        if not self.USE_PEXELS or not self.pexels_api_key: return None
        headers = {"Authorization": self.pexels_api_key}; params = {"query": query, "per_page": 1, "orientation": "landscape", "size": "large"}
        # Use a more unique filename for Pexels images to avoid clashes if query is similar
        pexels_filename = output_filename_base.replace(".png", f"_pexels_{random.randint(1000,9999)}.jpg").replace(".mp4", f"_pexels_{random.randint(1000,9999)}.jpg")
        filepath = os.path.join(self.output_dir, pexels_filename)
        try:
            logger.info(f"Searching Pexels for: '{query}'"); effective_query = " ".join(query.split()[:5]); params["query"] = effective_query
            response = requests.get("https://api.pexels.com/v1/search", headers=headers, params=params, timeout=20)
            response.raise_for_status(); data = response.json()
            if data.get("photos") and len(data["photos"]) > 0:
                photo_url = data["photos"][0]["src"]["large2x"] # Using large2x for better quality
                image_response = requests.get(photo_url, timeout=60); image_response.raise_for_status()
                img_data = Image.open(io.BytesIO(image_response.content))
                if img_data.mode != 'RGB': img_data = img_data.convert('RGB')
                img_data.save(filepath); logger.info(f"Pexels image saved: {filepath}"); return filepath
            else: logger.info(f"No photos found on Pexels for query: '{effective_query}'")
        except Exception as e: logger.error(f"Pexels search/download for query '{query}': {e}", exc_info=True)
        return None


    # <<< RUNWAYML START >>>
    def _generate_video_clip_with_runwayml(self, prompt_text, scene_identifier_filename_base, target_duration_seconds=4, input_image_path=None):
        """
        Placeholder for generating a video clip using RunwayML.
        This needs to be implemented with the actual RunwayML SDK or API.
        """
        if not self.USE_RUNWAYML or not self.runway_api_key:
            logger.warning("RunwayML not enabled or API key missing. Cannot generate video clip.")
            return None

        output_video_filename = scene_identifier_filename_base.replace(".png", ".mp4") # Ensure .mp4 extension
        output_video_filepath = os.path.join(self.output_dir, output_video_filename)

        logger.info(f"Attempting RunwayML video generation for: {prompt_text[:100]}... (Target duration: {target_duration_seconds}s)")
        logger.info(f"RunwayML Output (Placeholder): {output_video_filepath}")

        # --- START ACTUAL RUNWAYML API INTERACTION (HYPOTHETICAL) ---
        # This section is highly dependent on RunwayML's specific API/SDK.
        # Example using a hypothetical SDK:
        # try:
        #     if not self.runway_client:
        #         # self.runway_client = RunwayMLClient(api_key=self.runway_api_key) # Or however it's initialized
        #         logger.warning("RunwayML client not initialized (Placeholder).")
        #         # For placeholder, simulate creating a dummy video file
        #         return self._create_placeholder_video_content(prompt_text, output_video_filename, duration=target_duration_seconds)


        #     generation_params = {
        #         "text_prompt": prompt_text,
        #         "duration_seconds": target_duration_seconds,
        #         "width": self.video_frame_size[0], # Or Runway's supported sizes
        #         "height": self.video_frame_size[1],
        #         # Add other params like seed, motion scale, etc.
        #     }
        #     if input_image_path and os.path.exists(input_image_path):
        #         generation_params["input_image_path"] = input_image_path # For image-to-video
        #         logger.info(f"Using input image for RunwayML: {input_image_path}")

        #     task_id = self.runway_client.submit_video_generation_task(**generation_params) # Hypothetical
        #     logger.info(f"RunwayML task submitted: {task_id}. Polling for completion...")

        #     while True:
        #         status = self.runway_client.get_task_status(task_id) # Hypothetical
        #         if status == "completed":
        #             video_url = self.runway_client.get_video_url(task_id) # Hypothetical
        #             video_response = requests.get(video_url, stream=True, timeout=300)
        #             video_response.raise_for_status()
        #             with open(output_video_filepath, 'wb') as f:
        #                 for chunk in video_response.iter_content(chunk_size=8192):
        #                     f.write(chunk)
        #             logger.info(f"RunwayML video downloaded and saved: {output_video_filepath}")
        #             return output_video_filepath
        #         elif status in ["failed", "error"]:
        #             logger.error(f"RunwayML task {task_id} failed.")
        #             return None
        #         time.sleep(10) # Poll interval

        # except Exception as e:
        #     logger.error(f"Error during RunwayML video generation: {e}", exc_info=True)
        #     return None
        # --- END ACTUAL RUNWAYML API INTERACTION (HYPOTHETICAL) ---

        # For now, as a placeholder, create a dummy MP4 file with MoviePy
        # This allows the rest of the pipeline to be tested.
        # **REPLACE THIS WITH ACTUAL RUNWAYML CALLS**
        logger.warning("Using PLACEHOLDER video generation for RunwayML.")
        return self._create_placeholder_video_content(f"[RunwayML Placeholder] {prompt_text}", output_video_filename, duration=target_duration_seconds)

    def _create_placeholder_video_content(self, text_description, filename, duration=4, size=None):
        """Creates a short video clip with text as a placeholder."""
        if size is None: size = self.video_frame_size
        filepath = os.path.join(self.output_dir, filename)
        
        # Create a simple text clip
        txt_clip = TextClip(text_description, fontsize=50, color='white', font=self.video_overlay_font,
                            bg_color='black', size=size, method='caption').set_duration(duration)
        
        try:
            txt_clip.write_videofile(filepath, fps=24, codec='libx264', preset='ultrafast', logger=None)
            logger.info(f"Placeholder video saved: {filepath}")
            return filepath
        except Exception as e:
            logger.error(f"Failed to create placeholder video {filepath}: {e}", exc_info=True)
            return None
        finally:
            if hasattr(txt_clip, 'close'): txt_clip.close()
    # <<< RUNWAYML END >>>


    def generate_scene_asset(self, image_prompt_text, scene_data, scene_identifier_filename_base,
                             generate_as_video_clip=False, runway_target_duration=4, input_image_for_runway=None):
        """
        Generates either an image or a video clip for a scene.
        Returns a dictionary: {'path': asset_path, 'type': 'image'/'video', 'error': bool}
        """
        # Ensure scene_identifier_filename_base does not have an extension yet, or handle it
        base_name, _ = os.path.splitext(scene_identifier_filename_base)

        if generate_as_video_clip and self.USE_RUNWAYML:
            logger.info(f"Attempting RunwayML video clip generation for {base_name}")
            video_path = self._generate_video_clip_with_runwayml(
                image_prompt_text, # Use DALL-E prompt also for Runway text-to-video
                base_name, # Pass base name, function will add .mp4
                target_duration_seconds=runway_target_duration,
                input_image_path=input_image_for_runway
            )
            if video_path and os.path.exists(video_path):
                return {'path': video_path, 'type': 'video', 'error': False, 'prompt_used': image_prompt_text}
            else:
                logger.warning(f"RunwayML video clip generation failed for {base_name}. Falling back to image.")
                # Fall through to image generation

        # Image Generation (DALL-E, Pexels, Placeholder)
        # Ensure image filename has .png
        image_filename_with_ext = base_name + ".png"
        filepath = os.path.join(self.output_dir, image_filename_with_ext)

        if self.USE_AI_IMAGE_GENERATION and self.openai_api_key:
            max_retries = 2
            for attempt in range(max_retries):
                try:
                    # ... (DALL-E generation logic - no changes from your previous version) ...
                    logger.info(f"Attempt {attempt+1}: DALL-E ({self.dalle_model}) for: {image_prompt_text[:100]}...")
                    client = openai.OpenAI(api_key=self.openai_api_key, timeout=90.0)
                    response = client.images.generate(model=self.dalle_model, prompt=image_prompt_text, n=1, size=self.image_size_dalle3, quality="hd", response_format="url", style="vivid")
                    image_url = response.data[0].url; revised_prompt = getattr(response.data[0], 'revised_prompt', None)
                    if revised_prompt: logger.info(f"DALL-E 3 revised_prompt: {revised_prompt[:100]}...")
                    image_response = requests.get(image_url, timeout=120); image_response.raise_for_status()
                    img_data = Image.open(io.BytesIO(image_response.content));
                    if img_data.mode != 'RGB': img_data = img_data.convert('RGB')
                    img_data.save(filepath); logger.info(f"AI Image (DALL-E) saved: {filepath}");
                    return {'path': filepath, 'type': 'image', 'error': False, 'prompt_used': image_prompt_text, 'revised_prompt': revised_prompt}
                except openai.RateLimitError as e:
                    logger.warning(f"OpenAI Rate Limit: {e}. Retrying after {5*(attempt+1)}s..."); time.sleep(5 * (attempt + 1))
                    if attempt == max_retries - 1: logger.error("Max retries for RateLimitError."); break
                except openai.APIError as e: logger.error(f"OpenAI API Error: {e}"); break
                except requests.exceptions.RequestException as e: logger.error(f"Requests Error (DALL-E download): {e}"); break
                except Exception as e: logger.error(f"Generic error (DALL-E gen): {e}", exc_info=True); break
            logger.warning("DALL-E generation failed. Trying Pexels fallback...")
        
        # Pexels or Placeholder if DALL-E failed or disabled
        if self.USE_PEXELS:
            pexels_query_text = scene_data.get('pexels_search_query_๊ฐ๋…', f"{scene_data.get('emotional_beat','')} {scene_data.get('setting_description','')}")
            pexels_path = self._search_pexels_image(pexels_query_text, image_filename_with_ext) # Pass filename with extension
            if pexels_path:
                return {'path': pexels_path, 'type': 'image', 'error': False, 'prompt_used': f"Pexels: {pexels_query_text}"}
            logger.warning("Pexels also failed/disabled. Using placeholder image.")
        
        placeholder_path = self._create_placeholder_image_content(
            f"[AI/Pexels Failed] {image_prompt_text[:100]}...", image_filename_with_ext
        )
        if placeholder_path:
            return {'path': placeholder_path, 'type': 'image', 'error': False, 'prompt_used': image_prompt_text}
        else:
            return {'path': None, 'type': 'none', 'error': True, 'prompt_used': image_prompt_text}


    def generate_narration_audio(self, text_to_narrate, output_filename="narration_overall.mp3"):
        # ... (no changes from your previous version) ...
        if not self.USE_ELEVENLABS or not self.elevenlabs_client or not text_to_narrate:
            logger.info("ElevenLabs conditions not met (API key, client init, or text). Skipping audio.")
            return None

        audio_filepath = os.path.join(self.output_dir, output_filename)
        try:
            logger.info(f"Generating ElevenLabs audio (Voice ID: {self.elevenlabs_voice_id}) for: {text_to_narrate[:70]}...")

            audio_stream_method = None
            if hasattr(self.elevenlabs_client, 'text_to_speech') and hasattr(self.elevenlabs_client.text_to_speech, 'stream'):
                audio_stream_method = self.elevenlabs_client.text_to_speech.stream
                logger.info("Using elevenlabs_client.text_to_speech.stream()")
            elif hasattr(self.elevenlabs_client, 'generate_stream') : # Older SDK might have this
                audio_stream_method = self.elevenlabs_client.generate_stream
                logger.info("Using elevenlabs_client.generate_stream()")
            elif hasattr(self.elevenlabs_client, 'generate'): # Fallback to non-streaming
                logger.info("Using elevenlabs_client.generate() (non-streaming).")
                # This one doesn't return a stream, it returns bytes directly
                voice_param = Voice(voice_id=str(self.elevenlabs_voice_id), settings=self.elevenlabs_voice_settings) if Voice and self.elevenlabs_voice_settings else str(self.elevenlabs_voice_id)
                audio_bytes = self.elevenlabs_client.generate(
                    text=text_to_narrate,
                    voice=voice_param,
                    model="eleven_multilingual_v2" # or other suitable model
                )
                with open(audio_filepath, "wb") as f:
                    f.write(audio_bytes)
                logger.info(f"ElevenLabs audio (non-streamed) saved: {audio_filepath}")
                return audio_filepath
            else:
                logger.error("No recognized audio generation method found on ElevenLabs client.")
                return None

            # If we have a streaming method
            if audio_stream_method:
                voice_param_for_stream = {"voice_id": str(self.elevenlabs_voice_id)}
                # For Pydantic v1 style for elevenlabs sdk <1.0
                # if self.elevenlabs_voice_settings and hasattr(self.elevenlabs_voice_settings, 'dict'):
                #    voice_param_for_stream["voice_settings"] = self.elevenlabs_voice_settings.dict()
                # For Pydantic v2 style for elevenlabs skd >=1.0
                if self.elevenlabs_voice_settings and hasattr(self.elevenlabs_voice_settings, 'model_dump'):
                     voice_param_for_stream["voice_settings"] = self.elevenlabs_voice_settings.model_dump()
                elif self.elevenlabs_voice_settings : # If not a pydantic model, pass as is if supported
                     voice_param_for_stream["voice_settings"] = self.elevenlabs_voice_settings

                audio_data_iterator = audio_stream_method(
                    text=text_to_narrate,
                    model_id="eleven_multilingual_v2",
                    **voice_param_for_stream
                )
                with open(audio_filepath, "wb") as f:
                    for chunk in audio_data_iterator:
                        if chunk: f.write(chunk)
                logger.info(f"ElevenLabs audio (streamed) saved: {audio_filepath}")
                return audio_filepath

        except AttributeError as ae:
             logger.error(f"AttributeError with ElevenLabs client: {ae}. SDK method/params might be different.", exc_info=True)
        except Exception as e:
            logger.error(f"Error generating ElevenLabs audio: {e}", exc_info=True)
        return None

    def assemble_animatic_from_assets(self, asset_data_list, overall_narration_path=None, output_filename="final_video.mp4", fps=24):
        """
        Assembles the final video from a list of assets (images or video clips).
        Each item in asset_data_list should be a dict like:
        {'path': 'path/to/asset', 'type': 'image'|'video', 'duration': desired_scene_duration_in_animatic,
         'scene_num': num, 'key_action': 'text'}
        """
        if not asset_data_list:
            logger.warning("No asset data provided for animatic assembly.")
            return None

        processed_moviepy_clips = []
        narration_audio_clip = None
        final_composite_clip = None
        total_video_duration_from_assets = sum(item.get('duration', 4.5) for item in asset_data_list)
        logger.info(f"Assembling animatic from {len(asset_data_list)} assets. Target frame: {self.video_frame_size}. Approx total duration: {total_video_duration_from_assets:.2f}s.")

        for i, asset_info in enumerate(asset_data_list):
            asset_path = asset_info.get('path')
            asset_type = asset_info.get('type')
            # This 'duration' is the desired display duration of THIS scene in the final animatic
            target_scene_duration = asset_info.get('duration', 4.5) # Default if not specified
            scene_num = asset_info.get('scene_num', i + 1)
            key_action = asset_info.get('key_action', '')

            if not (asset_path and os.path.exists(asset_path)):
                logger.warning(f"Asset not found for Scene {scene_num}: {asset_path}. Skipping.")
                continue
            if target_scene_duration <= 0:
                logger.warning(f"Scene {scene_num} has invalid duration ({target_scene_duration}s). Skipping.")
                continue

            current_clip = None
            try:
                if asset_type == 'image':
                    pil_img = Image.open(asset_path)
                    if pil_img.mode != 'RGB': pil_img = pil_img.convert('RGB')
                    img_copy = pil_img.copy()
                    resample_filter = Image.Resampling.LANCZOS if hasattr(Image.Resampling, 'LANCZOS') else (Image.ANTIALIAS if hasattr(Image, 'ANTIALIAS') else Image.BILINEAR)
                    img_copy.thumbnail(self.video_frame_size, resample_filter)
                    canvas = Image.new('RGB', self.video_frame_size, (random.randint(0,10), random.randint(0,10), random.randint(0,10)))
                    xo, yo = (self.video_frame_size[0] - img_copy.width) // 2, (self.video_frame_size[1] - img_copy.height) // 2
                    canvas.paste(img_copy, (xo, yo))
                    frame_np = np.array(canvas)
                    current_clip_base = ImageClip(frame_np).set_duration(target_scene_duration)

                    # Ken Burns for ImageClips
                    try:
                        end_scale = random.uniform(1.03, 1.08)
                        current_clip = current_clip_base.fx(vfx.resize, lambda t: 1 + (end_scale - 1) * (t / target_scene_duration)).set_position('center')
                    except Exception as e_fx:
                        logger.error(f"Ken Burns error for image {asset_path}: {e_fx}. Using static image.")
                        current_clip = current_clip_base

                elif asset_type == 'video':
                    source_video_clip = VideoFileClip(asset_path, target_resolution=(self.video_frame_size[1], self.video_frame_size[0]))
                    # Fit video into target_scene_duration:
                    # If source is shorter, it will play once. If longer, it will be cut.
                    # For more complex looping/speed adjustments, more logic is needed.
                    if source_video_clip.duration > target_scene_duration:
                        current_clip = source_video_clip.subclip(0, target_scene_duration)
                    elif source_video_clip.duration < target_scene_duration:
                         # Simple loop if significantly shorter, or just play once if close
                        if target_scene_duration / source_video_clip.duration > 1.5 and source_video_clip.duration > 0.1 : # Loop if target is >150% of source
                             current_clip = source_video_clip.loop(duration=target_scene_duration)
                        else: # Play once, duration will be its own, MoviePy handles concatenation padding
                             current_clip = source_video_clip.set_duration(source_video_clip.duration) # Explicitly set
                             logger.info(f"Runway clip for S{scene_num} ({source_video_clip.duration:.2f}s) shorter than target ({target_scene_duration:.2f}s), will play once.")
                    else: # Durations match
                        current_clip = source_video_clip
                    
                    # Ensure the clip has the target duration for consistent concatenation
                    if current_clip.duration != target_scene_duration:
                         current_clip = current_clip.set_duration(target_scene_duration)


                    # Resize if necessary (MoviePy does this on CompositeVideoClip too, but explicit can be good)
                    if current_clip.size != list(self.video_frame_size):
                        current_clip = current_clip.resize(self.video_frame_size)
                    
                    # Close the original source_video_clip if it's different from current_clip (e.g., after subclip)
                    if current_clip != source_video_clip and hasattr(source_video_clip, 'close'):
                        source_video_clip.close()


                else:
                    logger.warning(f"Unknown asset type '{asset_type}' for Scene {scene_num}. Skipping.")
                    continue

                # Add text overlay
                if current_clip and key_action:
                    text_overlay_duration = min(target_scene_duration - 0.5, target_scene_duration * 0.8) if target_scene_duration > 0.5 else target_scene_duration
                    text_overlay_start = (target_scene_duration - text_overlay_duration) / 2.0
                    if text_overlay_duration > 0:
                        txt_clip = TextClip(f"Scene {scene_num}\n{key_action}",
                                            fontsize=self.video_overlay_font_size, color=self.video_overlay_font_color,
                                            font=self.video_overlay_font, bg_color='rgba(10,10,20,0.7)',
                                            method='caption', align='West', size=(self.video_frame_size[0] * 0.9, None),
                                            kerning=-1, stroke_color='black', stroke_width=1.5
                                           ).set_duration(text_overlay_duration).set_start(text_overlay_start).set_position(('center', 0.92), relative=True)
                        current_clip = CompositeVideoClip([current_clip, txt_clip], size=self.video_frame_size, use_bgclip=True, bg_color=(0,0,0))
                
                if current_clip:
                    processed_moviepy_clips.append(current_clip)

            except Exception as e:
                logger.error(f"Error processing asset for Scene {scene_num} ({asset_path}): {e}", exc_info=True)
                if current_clip and hasattr(current_clip, 'close'): current_clip.close() # Ensure closure on error

        if not processed_moviepy_clips:
            logger.warning("No MoviePy clips successfully processed. Aborting animatic assembly.")
            return None

        transition_duration = 0.75
        try:
            if len(processed_moviepy_clips) > 1:
                final_composite_clip = concatenate_videoclips(processed_moviepy_clips, padding=-transition_duration, method="compose")
            elif processed_moviepy_clips:
                final_composite_clip = processed_moviepy_clips[0]
            else: # Should have been caught above, but defensive
                logger.error("No clips available for final concatenation.")
                return None


            if final_composite_clip.duration > transition_duration * 2:
                final_composite_clip = final_composite_clip.fx(vfx.fadein, transition_duration).fx(vfx.fadeout, transition_duration)
            elif final_composite_clip.duration > 0:
                 final_composite_clip = final_composite_clip.fx(vfx.fadein, min(transition_duration, final_composite_clip.duration/2.0))

            if overall_narration_path and os.path.exists(overall_narration_path):
                try:
                    narration_audio_clip = AudioFileClip(overall_narration_path)
                    if final_composite_clip.duration > 0 and narration_audio_clip.duration < final_composite_clip.duration:
                        logger.info(f"Narration ({narration_audio_clip.duration:.2f}s) shorter than visuals ({final_composite_clip.duration:.2f}s). Trimming video.")
                        final_composite_clip = final_composite_clip.subclip(0, narration_audio_clip.duration)
                    elif final_composite_clip.duration <= 0: logger.warning("Video has no duration. Audio not added.")
                    
                    if narration_audio_clip and final_composite_clip.duration > 0: # Check again
                        final_composite_clip = final_composite_clip.set_audio(narration_audio_clip)
                        logger.info("Overall narration added.")
                except Exception as e: logger.error(f"Adding narration error: {e}", exc_info=True)
            
            if final_composite_clip and final_composite_clip.duration > 0:
                output_path = os.path.join(self.output_dir, output_filename)
                logger.info(f"Writing final animatic: {output_path} (Duration: {final_composite_clip.duration:.2f}s)")
                final_composite_clip.write_videofile(
                    output_path, fps=fps, codec='libx264', preset='medium', audio_codec='aac',
                    temp_audiofile=os.path.join(self.output_dir, f'temp-audio-{os.urandom(4).hex()}.m4a'),
                    remove_temp=True, threads=os.cpu_count() or 2, logger='bar', bitrate="5000k"
                )
                logger.info(f"Animatic created: {output_path}"); return output_path
            else: logger.error("Final animatic clip invalid or has no duration. Not writing file."); return None
        except Exception as e: logger.error(f"Animatic writing error: {e}", exc_info=True); return None
        finally:
            for clip in processed_moviepy_clips:
                if hasattr(clip, 'close'): clip.close()
            if narration_audio_clip and hasattr(narration_audio_clip, 'close'): narration_audio_clip.close()
            if final_composite_clip and hasattr(final_composite_clip, 'close'): final_composite_clip.close()