-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathscript2video_pipeline.py
More file actions
657 lines (547 loc) · 31.3 KB
/
script2video_pipeline.py
File metadata and controls
657 lines (547 loc) · 31.3 KB
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
import os
import shutil
import json
import logging
import asyncio
import time
from typing import Optional, Dict, List, Tuple, Literal
from moviepy import VideoFileClip, concatenate_videoclips
from PIL import Image
from pipelines.base import BasePipeline
from agents import *
import yaml
from interfaces import *
from langchain.chat_models import init_chat_model
from utils.timer import Timer
import importlib
class Script2VideoPipeline:
# events
character_portrait_events = {}
shot_desc_events = {}
frame_events = {}
def __init__(
self,
chat_model: str,
image_generator,
video_generator,
working_dir: str,
custom_assets: Optional[Dict[str, List[Dict[str, str]]]] = None,
):
self.chat_model = chat_model
self.image_generator = image_generator
self.video_generator = video_generator
self.custom_assets = custom_assets or {"sample_images": [], "sample_videos": []}
self.character_extractor = CharacterExtractor(chat_model=self.chat_model)
self.character_portraits_generator = CharacterPortraitsGenerator(image_generator=self.image_generator)
self.storyboard_artist = StoryboardArtist(chat_model=self.chat_model)
self.camera_image_generator = CameraImageGenerator(chat_model=self.chat_model, image_generator=self.image_generator, video_generator=self.video_generator)
self.reference_image_selector = ReferenceImageSelector(chat_model=self.chat_model)
self.working_dir = working_dir
os.makedirs(self.working_dir, exist_ok=True)
def _get_custom_asset_pairs(self) -> List[Tuple[str, str]]:
"""
Convert custom assets from config into (path, description) pairs.
Returns a list of tuples suitable for available_image_path_and_text_pairs.
"""
asset_pairs = []
# Add sample images
for asset in self.custom_assets.get("sample_images", []):
if "path" in asset and "description" in asset:
# Verify the file exists
if os.path.exists(asset["path"]):
asset_pairs.append((asset["path"], asset["description"]))
else:
print(f"⚠️ Warning: Custom asset image not found: {asset['path']}")
# Note: sample_videos could be added here in the future if needed
# For now, we focus on sample_images as they're used as reference images
return asset_pairs
@classmethod
def init_from_config(
cls,
config_path: str,
):
with open(config_path, "r") as f:
config = yaml.safe_load(f)
chat_model_args = config["chat_model"]["init_args"]
chat_model = init_chat_model(**chat_model_args)
image_generator_cls_module, image_generator_cls_name = config["image_generator"]["class_path"].rsplit(".", 1)
image_generator_cls = getattr(importlib.import_module(image_generator_cls_module), image_generator_cls_name)
image_generator_args = config["image_generator"]["init_args"]
image_generator = image_generator_cls(**image_generator_args)
video_generator_cls_module, video_generator_cls_name = config["video_generator"]["class_path"].rsplit(".", 1)
video_generator_cls = getattr(importlib.import_module(video_generator_cls_module), video_generator_cls_name)
video_generator_args = config["video_generator"]["init_args"]
video_generator = video_generator_cls(**video_generator_args)
# Load custom assets if provided
custom_assets = config.get("assets", {"sample_images": [], "sample_videos": []})
return cls(
chat_model=chat_model,
image_generator=image_generator,
video_generator=video_generator,
working_dir=config["working_dir"],
custom_assets=custom_assets,
)
async def __call__(
self,
script: str,
user_requirement: str,
style: str,
characters: List[CharacterInScene] = None,
character_portraits_registry: Optional[Dict[str, Dict[str, Dict[str, str]]]] = None,
):
if characters is None:
characters = await self.extract_characters(script=script)
# characters_path = os.path.join(self.working_dir, "characters.json")
# if os.path.exists(characters_path):
# with open(characters_path, "r", encoding="utf-8") as f:
# characters = [CharacterInScene.model_validate(c) for c in json.load(f)]
# print(f"🚀 Loaded {len(characters)} characters from existing file.")
# else:
# print(f"🔍 Extracting characters from script...")
# characters = await self.extract_characters(script=script)
# with open(characters_path, "w", encoding="utf-8") as f:
# json.dump([c.model_dump() for c in characters], f, ensure_ascii=False, indent=4)
# print(f"☑️ Extracted {len(characters)} characters from script and saved to {characters_path}.")
if character_portraits_registry is None:
character_portraits_registry_path = os.path.join(self.working_dir, "character_portraits_registry.json")
if os.path.exists(character_portraits_registry_path):
with open(character_portraits_registry_path, "r", encoding="utf-8") as f:
character_portraits_registry = json.load(f)
print(f"🚀 Loaded {len(character_portraits_registry)} character portraits from existing file.")
else:
print(f"🔍 Generating character portraits...")
character_portraits_registry = await self.generate_character_portraits(
characters=characters,
character_portraits_registry=None,
style=style,
)
with open(character_portraits_registry_path, "w", encoding="utf-8") as f:
json.dump(character_portraits_registry, f, ensure_ascii=False, indent=4)
print(f"☑️ Generated {len(character_portraits_registry)} character portraits and saved to {character_portraits_registry_path}.")
# design shots
storyboard = await self.design_storyboard(
script=script,
characters=characters,
user_requirement=user_requirement,
)
# decompose visual descriptions of shots
shot_descriptions = await self.decompose_visual_descriptions(
shot_brief_descriptions=storyboard,
characters=characters,
)
# construct camera tree
camera_tree = await self.construct_camera_tree(
shot_descriptions=shot_descriptions,
)
priority_shot_idxs = [camera.parent_cam_idx for camera in camera_tree if camera.parent_cam_idx is not None]
tasks = [
self.generate_frames_for_single_camera(
camera=camera,
shot_descriptions=shot_descriptions,
characters=characters,
character_portraits_registry=character_portraits_registry,
priority_shot_idxs=priority_shot_idxs,
)
for camera in camera_tree
]
video_tasks = [
self.generate_video_for_single_shot(
shot_description=shot_description,
)
for shot_description in shot_descriptions
]
tasks.extend(video_tasks)
await asyncio.gather(*tasks)
final_video_path = os.path.join(self.working_dir, "final_video.mp4")
if os.path.exists(final_video_path):
print(f"🚀 Skipped concatenating videos, already exists.")
else:
print(f"🎬 Starting concatenating videos...")
video_clips = [
VideoFileClip(os.path.join(self.working_dir, "shots", f"{shot_description.idx}", "video.mp4"))
for shot_description in shot_descriptions
]
final_video = concatenate_videoclips(video_clips)
final_video.write_videofile(final_video_path, codec="libx264", preset="medium")
print(f"☑️ Concatenated videos, saved to {final_video_path}.")
return final_video_path
async def generate_frames_for_single_camera(
self,
camera: Camera,
shot_descriptions: List[ShotDescription],
characters: List[CharacterInScene],
character_portraits_registry: Dict[str, Dict[str, Dict[str, str]]],
priority_shot_idxs: List[int],
):
# 1. generate the first_frame of the first shot of the camera
first_shot_idx = camera.active_shot_idxs[0]
first_shot_ff_path = os.path.join(self.working_dir, "shots", f"{first_shot_idx}", "first_frame.png")
if os.path.exists(first_shot_ff_path):
print(f"🚀 Skipped generating first_frame for shot {first_shot_idx}, already exists.")
self.frame_events[first_shot_idx]["first_frame"].set()
else:
print(f"🖼️ Starting first_frame generation for shot {first_shot_idx}...")
available_image_path_and_text_pairs = []
# Add custom assets from config
available_image_path_and_text_pairs.extend(self._get_custom_asset_pairs())
for character_idx in shot_descriptions[first_shot_idx].ff_vis_char_idxs:
identifier_in_scene = characters[character_idx].identifier_in_scene
registry_item = character_portraits_registry[identifier_in_scene]
for view, item in registry_item.items():
available_image_path_and_text_pairs.append((item["path"], item["description"]))
# generate the first_frame based on the shot_description.ff_desc
if camera.parent_shot_idx is not None:
# generate the first_frame based on the transition video
parent_shot_idx = camera.parent_shot_idx
await self.frame_events[parent_shot_idx]["first_frame"].wait()
parent_shot_ff_path = os.path.join(self.working_dir, "shots", f"{parent_shot_idx}", "first_frame.png")
transition_video_path = os.path.join(self.working_dir, "shots", f"{first_shot_idx}", f"transition_video_from_shot_{parent_shot_idx}.mp4")
if os.path.exists(transition_video_path):
print(f"🚀 Skipped generating transition video for shot {first_shot_idx} from shot {parent_shot_idx}, already exists.")
else:
print(f"🖼️ Starting transition video generation for shot {first_shot_idx} from shot {parent_shot_idx}...")
transition_video_output = await self.camera_image_generator.generate_transition_video(
first_shot_visual_desc=shot_descriptions[parent_shot_idx].visual_desc,
second_shot_visual_desc=shot_descriptions[first_shot_idx].visual_desc,
first_shot_ff_path=parent_shot_ff_path,
)
transition_video_output.save(transition_video_path)
print(f"☑️ Generated transition video for shot {first_shot_idx} from shot {parent_shot_idx}, saved to {transition_video_path}.")
new_camera_image_path = os.path.join(self.working_dir, "shots", f"{first_shot_idx}", f"new_camera_{camera.idx}.png")
if os.path.exists(new_camera_image_path):
print(f"🚀 Skipped generating new camera image for shot {first_shot_idx}, already exists.")
else:
print(f"🖼️ Starting new camera image generation for shot {first_shot_idx}...")
new_camera_image = self.camera_image_generator.get_new_camera_image(transition_video_path)
new_camera_image.save(new_camera_image_path)
print(f"☑️ Generated new camera image for shot {first_shot_idx} (not completed), saved to {new_camera_image_path}.")
available_image_path_and_text_pairs.append(
(
new_camera_image_path,
f"The composition and background are correct but some elements may be wrong. The wrong elements should be replaced.\nWrong elements: {camera.missing_info}.\nYou must select this image as the main reference and replace the characters in the image with the provided character portraits. Don't change the background."
)
)
# 如果子镜头缺少信息,则需要选择参考图像生成
if camera.parent_shot_idx is None or camera.missing_info is not None:
ff_selector_output_path = os.path.join(self.working_dir, "shots", f"{first_shot_idx}", "first_frame_selector_output.json")
if os.path.exists(ff_selector_output_path):
with open(ff_selector_output_path, 'r', encoding='utf-8') as f:
ff_selector_output = json.load(f)
print(f"🚀 Loaded existing reference image selection and prompt for first_frame of shot {first_shot_idx} from {ff_selector_output_path}.")
else:
print(f"🔍 Selecting reference images and generating prompt for first_frame of shot {first_shot_idx}...")
ff_selector_output = await self.reference_image_selector.select_reference_images_and_generate_prompt(
available_image_path_and_text_pairs=available_image_path_and_text_pairs,
frame_description=shot_descriptions[first_shot_idx].ff_desc
)
with open(ff_selector_output_path, 'w', encoding='utf-8') as f:
json.dump(ff_selector_output, f, ensure_ascii=False, indent=4)
print(f"☑️ Selected reference images and generated prompt for first_frame of shot {first_shot_idx}, saved to {ff_selector_output_path}.")
reference_image_path_and_text_pairs, prompt = ff_selector_output["reference_image_path_and_text_pairs"], ff_selector_output["text_prompt"]
prefix_prompt = ""
for i, (image_path, text) in enumerate(reference_image_path_and_text_pairs):
prefix_prompt += f"Image {i}: {text}\n"
prompt = f"{prefix_prompt}\n{prompt}"
reference_image_paths = [item[0] for item in reference_image_path_and_text_pairs]
ff_image: ImageOutput = await self.image_generator.generate_single_image(
prompt=prompt,
reference_image_paths=reference_image_paths,
size="1600x900",
)
ff_image.save(first_shot_ff_path)
self.frame_events[first_shot_idx]["first_frame"].set()
print(f"☑️ Generated first_frame for shot {first_shot_idx}, saved to {first_shot_ff_path}.")
else:
shutil.copy(new_camera_image_path, first_shot_ff_path)
self.frame_events[first_shot_idx]["first_frame"].set()
print(f"☑️ Generated first_frame for shot {first_shot_idx}, saved to {first_shot_ff_path}.")
# 2. generate the following frames of the camera
priority_tasks = []
normal_tasks = []
if shot_descriptions[first_shot_idx].variation_type in ["medium", "large"]:
task = self.generate_frame_for_single_shot(
shot_idx=first_shot_idx,
frame_type="last_frame",
first_shot_ff_path_and_text_pair=(first_shot_ff_path, shot_descriptions[first_shot_idx].ff_desc),
frame_desc=shot_descriptions[first_shot_idx].lf_desc,
visible_characters=[characters[idx] for idx in shot_descriptions[first_shot_idx].lf_vis_char_idxs],
character_portraits_registry=character_portraits_registry,
)
normal_tasks.append(task)
for shot_idx in camera.active_shot_idxs[1:]:
first_frame_task = self.generate_frame_for_single_shot(
shot_idx=shot_idx,
frame_type="first_frame",
first_shot_ff_path_and_text_pair=(first_shot_ff_path, shot_descriptions[first_shot_idx].ff_desc),
frame_desc=shot_descriptions[shot_idx].ff_desc,
visible_characters=[characters[idx] for idx in shot_descriptions[shot_idx].ff_vis_char_idxs],
character_portraits_registry=character_portraits_registry,
)
if shot_idx in priority_shot_idxs:
priority_tasks.append(first_frame_task)
else:
normal_tasks.append(first_frame_task)
if shot_descriptions[shot_idx].variation_type in ["medium", "large"]:
last_frame_task = self.generate_frame_for_single_shot(
shot_idx=shot_idx,
frame_type="last_frame",
first_shot_ff_path_and_text_pair=(first_shot_ff_path, shot_descriptions[first_shot_idx].ff_desc),
frame_desc=shot_descriptions[shot_idx].lf_desc,
visible_characters=[characters[idx] for idx in shot_descriptions[shot_idx].lf_vis_char_idxs],
character_portraits_registry=character_portraits_registry,
)
normal_tasks.append(last_frame_task)
await asyncio.gather(*priority_tasks)
await asyncio.gather(*normal_tasks)
async def generate_video_for_single_shot(
self,
shot_description: ShotDescription,
):
video_path = os.path.join(self.working_dir, "shots", f"{shot_description.idx}", "video.mp4")
if os.path.exists(video_path):
print(f"🚀 Skipped generating video for shot {shot_description.idx}, already exists.")
else:
await self.frame_events[shot_description.idx]["first_frame"].wait()
if shot_description.variation_type in ["medium", "large"]:
await self.frame_events[shot_description.idx]["last_frame"].wait()
frame_paths = []
frame_paths.append(os.path.join(self.working_dir, "shots", f"{shot_description.idx}", "first_frame.png"))
if shot_description.variation_type in ["medium", "large"]:
frame_paths.append(os.path.join(self.working_dir, "shots", f"{shot_description.idx}", "last_frame.png"))
print(f"🎬 Starting video generation for shot {shot_description.idx}...")
video_output = await self.video_generator.generate_single_video(
prompt=shot_description.motion_desc + "\n" + shot_description.audio_desc,
reference_image_paths=frame_paths,
)
video_output.save(video_path)
print(f"☑️ Generated video for shot {shot_description.idx}, saved to {video_path}.")
async def generate_frame_for_single_shot(
self,
shot_idx: int,
frame_type: Literal["first_frame", "last_frame"],
first_shot_ff_path_and_text_pair: Tuple[str, str],
frame_desc: str,
visible_characters: List[CharacterInScene],
character_portraits_registry: Dict[str, Dict[str, Dict[str, str]]],
) -> ImageOutput:
frame_image_path = os.path.join(self.working_dir, "shots", f"{shot_idx}", f"{frame_type}.png")
if os.path.exists(frame_image_path):
print(f"🚀 Skipped generating {frame_type} for shot {shot_idx}, already exists.")
else:
print(f"🖼️ Starting {frame_type} generation for shot {shot_idx}...")
available_image_path_and_text_pairs = []
# Add custom assets from config
available_image_path_and_text_pairs.extend(self._get_custom_asset_pairs())
for visible_character in visible_characters:
identifier_in_scene = visible_character.identifier_in_scene
registry_item = character_portraits_registry[identifier_in_scene]
for view, item in registry_item.items():
available_image_path_and_text_pairs.append((item["path"], item["description"]))
available_image_path_and_text_pairs.append(first_shot_ff_path_and_text_pair)
selector_output_path = os.path.join(self.working_dir, "shots", f"{shot_idx}", f"{frame_type}_selector_output.json")
if os.path.exists(selector_output_path):
with open(selector_output_path, 'r', encoding='utf-8') as f:
selector_output = json.load(f)
print(f"🚀 Loaded existing reference image selection and prompt for {frame_type} frame of shot {shot_idx} from {selector_output_path}.")
else:
print(f"🔍 Selecting reference images and generating prompt for {frame_type} frame of shot {shot_idx}...")
selector_output = await self.reference_image_selector.select_reference_images_and_generate_prompt(
available_image_path_and_text_pairs=available_image_path_and_text_pairs,
frame_description=frame_desc
)
with open(selector_output_path, 'w', encoding='utf-8') as f:
json.dump(selector_output, f, ensure_ascii=False, indent=4)
print(f"☑️ Selected reference images and generated prompt for {frame_type} frame of shot {shot_idx}, saved to {selector_output_path}.")
reference_image_path_and_text_pairs, prompt = selector_output["reference_image_path_and_text_pairs"], selector_output["text_prompt"]
prefix_prompt = ""
for i, (image_path, text) in enumerate(reference_image_path_and_text_pairs):
prefix_prompt += f"Image {i}: {text}\n"
prompt = f"{prefix_prompt}\n{prompt}"
reference_image_paths = [item[0] for item in reference_image_path_and_text_pairs]
frame_image: ImageOutput = await self.image_generator.generate_single_image(
prompt=prompt,
reference_image_paths=reference_image_paths,
size="1600x900",
)
frame_image.save(frame_image_path)
print(f"☑️ Generated {frame_type} frame for shot {shot_idx}, saved to {frame_image_path}.")
self.frame_events[shot_idx][frame_type].set()
return frame_image_path
async def construct_camera_tree(
self,
shot_descriptions: List[ShotDescription],
):
camera_tree_path = os.path.join(self.working_dir, "camera_tree.json")
if os.path.exists(camera_tree_path):
with open(camera_tree_path, "r", encoding="utf-8") as f:
camera_tree = json.load(f)
camera_tree = [Camera.model_validate(camera) for camera in camera_tree]
print(f"🚀 Loaded {len(camera_tree)} cameras from existing file.")
return camera_tree
cameras: List[Camera] = []
for shot_description in shot_descriptions:
if shot_description.cam_idx not in [camera.idx for camera in cameras]:
cameras.append(Camera(idx=shot_description.cam_idx, active_shot_idxs=[shot_description.idx]))
else:
cameras[shot_description.cam_idx].active_shot_idxs.append(shot_description.idx)
camera_tree = await self.camera_image_generator.construct_camera_tree(cameras=cameras, shot_descs=shot_descriptions)
with open(camera_tree_path, "w", encoding="utf-8") as f:
json.dump([camera.model_dump() for camera in camera_tree], f, ensure_ascii=False, indent=4)
print(f"✅ Constructed camera tree and saved to {camera_tree_path}.")
return camera_tree
async def extract_characters(
self,
script: str,
):
save_path = os.path.join(self.working_dir, "characters.json")
if os.path.exists(save_path):
with open(save_path, "r", encoding="utf-8") as f:
characters = json.load(f)
characters = [CharacterInScene.model_validate(character) for character in characters]
print(f"🚀 Loaded {len(characters)} characters from existing file.")
else:
characters = await self.character_extractor.extract_characters(script)
with open(save_path, "w", encoding="utf-8") as f:
json.dump([character.model_dump() for character in characters], f, ensure_ascii=False, indent=4)
print(f"✅ Extracted {len(characters)} characters from script and saved to {save_path}.")
for character in characters:
self.character_portrait_events[character.idx] = asyncio.Event()
return characters
async def generate_character_portraits(
self,
characters: List[CharacterInScene],
character_portraits_registry: Optional[Dict[str, Dict[str, Dict[str, str]]]],
style: str,
):
character_portraits_registry_path = os.path.join(self.working_dir, "character_portraits_registry.json")
if character_portraits_registry is None:
if os.path.exists(character_portraits_registry_path):
with open(character_portraits_registry_path, 'r', encoding='utf-8') as f:
character_portraits_registry = json.load(f)
else:
character_portraits_registry = {}
tasks = [
self.generate_portraits_for_single_character(character, style)
for character in characters
if character.identifier_in_scene not in character_portraits_registry
]
if tasks:
for future in asyncio.as_completed(tasks):
character_portraits_registry.update(await future)
with open(character_portraits_registry_path, 'w', encoding='utf-8') as f:
json.dump(character_portraits_registry, f, ensure_ascii=False, indent=4)
print(f"✅ Completed character portrait generation for {len(characters)} characters.")
else:
print("🚀 All characters already have portraits, skipping portrait generation.")
return character_portraits_registry
async def generate_portraits_for_single_character(
self,
character: CharacterInScene,
style: str,
):
character_dir = os.path.join(self.working_dir, "character_portraits", f"{character.idx}_{character.identifier_in_scene}")
os.makedirs(character_dir, exist_ok=True)
front_portrait_path = os.path.join(character_dir, "front.png")
if os.path.exists(front_portrait_path):
pass
else:
front_portrait_output = await self.character_portraits_generator.generate_front_portrait(character, style)
front_portrait_output.save(front_portrait_path)
side_portrait_path = os.path.join(character_dir, "side.png")
if os.path.exists(side_portrait_path):
pass
else:
side_portrait_output = await self.character_portraits_generator.generate_side_portrait(character, front_portrait_path)
side_portrait_output.save(side_portrait_path)
back_portrait_path = os.path.join(character_dir, "back.png")
if os.path.exists(back_portrait_path):
pass
else:
back_portrait_output = await self.character_portraits_generator.generate_back_portrait(character, front_portrait_path)
back_portrait_output.save(back_portrait_path)
self.character_portrait_events[character.idx].set()
print(f"☑️ Completed character portrait generation for {character.identifier_in_scene}.")
return {
character.identifier_in_scene: {
"front": {
"path": front_portrait_path,
"description": f"A front view portrait of {character.identifier_in_scene}.",
},
"side": {
"path": side_portrait_path,
"description": f"A side view portrait of {character.identifier_in_scene}.",
},
"back": {
"path": back_portrait_path,
"description": f"A back view portrait of {character.identifier_in_scene}.",
},
}
}
async def design_storyboard(
self,
script: str,
characters: List[CharacterInScene],
user_requirement: str,
):
storyboard_path = os.path.join(self.working_dir, "storyboard.json")
if os.path.exists(storyboard_path):
with open(storyboard_path, 'r', encoding='utf-8') as f:
storyboard = json.load(f)
storyboard = [ShotBriefDescription.model_validate(shot) for shot in storyboard]
print(f"🚀 Loaded {len(storyboard)} shot brief descriptions from existing file.")
else:
print(f"🔍 Designing storyboard...")
storyboard = await self.storyboard_artist.design_storyboard(
script=script,
characters=characters,
user_requirement=user_requirement,
retry_timeout=150,
)
with open(storyboard_path, 'w', encoding='utf-8') as f:
json.dump([shot.model_dump() for shot in storyboard], f, ensure_ascii=False, indent=4)
print(f"✅ Designed storyboard and saved to {storyboard_path}.")
for shot_brief_description in storyboard:
self.shot_desc_events[shot_brief_description.idx] = asyncio.Event()
return storyboard
async def decompose_visual_descriptions(
self,
shot_brief_descriptions: List[ShotBriefDescription],
characters: List[CharacterInScene],
):
tasks = [
self.decompose_visual_description_for_single_shot_brief_description(shot_brief_description, characters)
for shot_brief_description in shot_brief_descriptions
]
shot_descriptions = await asyncio.gather(*tasks)
return shot_descriptions
async def decompose_visual_description_for_single_shot_brief_description(
self,
shot_brief_description: ShotBriefDescription,
characters: List[CharacterInScene],
):
shot_description_path = os.path.join(self.working_dir, "shots", f"{shot_brief_description.idx}", "shot_description.json")
os.makedirs(os.path.dirname(shot_description_path), exist_ok=True)
if os.path.exists(shot_description_path):
with open(shot_description_path, 'r', encoding='utf-8') as f:
shot_description = ShotDescription.model_validate(json.load(f))
print(f"🚀 Loaded shot {shot_brief_description.idx} description from existing file.")
else:
shot_description = await self.storyboard_artist.decompose_visual_description(
shot_brief_desc=shot_brief_description,
characters=characters,
retry_timeout=120,
)
with open(shot_description_path, 'w', encoding='utf-8') as f:
json.dump(shot_description.model_dump(), f, ensure_ascii=False, indent=4)
print(f"✅ Decomposed visual description for shot {shot_brief_description.idx} and saved to {shot_description_path}.")
self.shot_desc_events[shot_brief_description.idx].set()
if shot_description.variation_type in ["medium", "large"]:
self.frame_events[shot_brief_description.idx] = {
"first_frame": asyncio.Event(),
"last_frame": asyncio.Event(),
}
else:
self.frame_events[shot_brief_description.idx] = {
"first_frame": asyncio.Event(),
}
return shot_description