Comfy-Org/ComfyUI · error · ValueError

required_duration must be less than video length

Error message

required_duration must be less than video length

What it means

Wan's get_sample_indices computes required_duration = num_sample / target_fps and rejects when it exceeds total_frames / original_fps — the clip is shorter than the sampling window needs. This is the top-level sanity check before any start-frame selection for training/video preprocessing.

Source

Thrown at comfy_extras/nodes_wan.py:824

    features = features.transpose(1, 2)  # [1, 512, T]
    seq_len = features.shape[2] / float(input_fps)  # T/f_a
    if output_len is None:
        output_len = int(seq_len * output_fps)  # f_m*T/f_a
    output_features = torch.nn.functional.interpolate(
        features, size=output_len, align_corners=True,
        mode='linear')  # [1, 512, output_len]
    return output_features.transpose(1, 2)  # [1, output_len, 512]


def get_sample_indices(original_fps,
                       total_frames,
                       target_fps,
                       num_sample,
                       fixed_start=None):
    required_duration = num_sample / target_fps
    required_origin_frames = int(np.ceil(required_duration * original_fps))
    if required_duration > total_frames / original_fps:
        raise ValueError("required_duration must be less than video length")

    if fixed_start is not None and fixed_start >= 0:
        start_frame = fixed_start
    else:
        max_start = total_frames - required_origin_frames
        if max_start < 0:
            raise ValueError("video length is too short")
        start_frame = np.random.randint(0, max_start + 1)
    start_time = start_frame / original_fps

    end_time = start_time + required_duration
    time_points = np.linspace(start_time, end_time, num_sample, endpoint=False)

    frame_indices = np.round(np.array(time_points) * original_fps).astype(int)
    frame_indices = np.clip(frame_indices, 0, total_frames - 1)
    return frame_indices

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Filter or skip videos shorter than num_sample / target_fps seconds before sampling
  2. Reduce num_sample or raise target_fps so the required duration fits the clip
  3. Verify original_fps matches the actual video (misread fps makes the duration look smaller)

Example fix

# before
idx = get_sample_indices(30, total_frames=16, target_fps=16, num_sample=81)

# after: guard before calling
required = num_sample / target_fps
if total_frames / original_fps < required:
    continue  # skip clip too short
Defensive patterns

Strategy: validation

Validate before calling

required = num_sample / target_fps
if required > total_frames / original_fps:
    raise SystemExit(f"clip {total_frames/original_fps:.2f}s < required {required:.2f}s; skip or reduce num_sample")

Prevention

When it happens

Trigger: Calling get_sample_indices with a short clip (e.g. 16 frames at 30fps ≈ 0.53s) while num_sample/target_fps (e.g. 81/16 ≈ 5.06s) exceeds it; also triggered by an incorrect original_fps that shrinks the computed clip duration.

Common situations: Dataset curation with mixed-length videos where some clips are too short for the target sample count; wrong fps metadata read from the container; increasing num_sample (longer training windows) without filtering the dataset.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/21832cc56b30d1bd. Report an issue: GitHub.