Keep video segments within a declared duration policy

Scene changes rarely fall at perfectly convenient clip lengths. An export policy needs to say how short a clip may be, how long it may be, and what happens when the remaining tail is too small. Write those decisions before selecting scene boundaries so that a final merge does not silently break the maximum duration.
Check a candidate cut
This synthetic helper filters candidate scene times inside a permitted interval and selects the one nearest a target. It handles one decision, not a complete segmentation algorithm.
def choose_cut(start, scenes, minimum=10, target=15, maximum=20):
candidates = [t for t in scenes if minimum <= t - start <= maximum]
return min(candidates, key=lambda t: abs((t - start) - target)) if candidates else start + maximum
assert choose_cut(0, [3, 14, 19, 26]) == 14
assert choose_cut(0, [3, 26]) == 20
The caller must clamp the result to the actual media end and decide how to treat missing or duplicated scene times. Use a stable time unit, such as integer milliseconds, if rounding makes boundary comparisons unpredictable.
Candidate cuts pass the duration policy; the remaining tail needs a separate explicit decision.
Resolve the short-tail conflict
Suppose a 43-second source is cut into 20, 20 and 3 seconds. Merging the tail produces a 23-second clip, which violates a strict 20-second maximum. If a 10-second minimum is also required, redistribute the final span into 20 and 13 seconds, or choose earlier cuts that leave enough room. Another valid policy can explicitly permit an undersized tail. State the exception in the export UI and metadata.
Test complete coverage independently of the duration policy. Sorted segments should start at zero, meet without gaps or overlaps, and end at the source duration. Then check each segment against the declared minimum and maximum, with the documented exception applied only where intended.
Keep scene detection optional
An input with no useful scene boundary still needs a deterministic split policy. Detection thresholds can change candidate cuts, so save the selected boundaries with the export settings when reproducibility matters. A scene boundary is a suggestion about visual change, not proof that the narrative remains understandable.
The FFmpeg filter documentation provides media-processing primitives; the duration policy above is an authored example. ShortForge describes scene-aware segmentation, but this fixture does not audit its implementation or establish full-coverage correctness for every file.