INNER CODE UNIT · Python

fit_scene_durations

ChenShuo2004/cs-board · webapp/server.py:582

def fit_scene_durations(scenes: list[dict[str, Any]], audio_duration: float) -> bool:
    """Make scene timing add up exactly to the voice track without starving the final image."""
    if not scenes:
        return False
    target_ms = max(len(scenes), round(max(0.001, audio_duration) * 1000))
    minimum_ms = min(1000, target_ms // len(scenes))
    remaining_ms = target_ms - minimum_ms * len(scenes)
    weights = [max(1, len(str(scene.get("text", "")))) for scene in scenes]
    total_weight = sum(weights)
    exact_extras = [remaining_ms * weight / total_weight for weight in weights]
    extras = [int(value) for value in exact_extras]
    leftover = remaining_ms - sum(extras)
    order = sorted(range(len(scenes)), key=lambda i: exact_extras[i] - extras[i], reverse=True)
    for index in order[:leftover]:
        extras[index] += 1
    durations = [minimum_ms + extra for extra in extras]
    changed = any(int(scene.get("duration_ms", 0)) != durations[i] for i, scene in enumerate(scenes))
    for scene, duration_ms in zip(scenes, durations):

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…