Comfy-Org/ComfyUI · error · ValueError

TrimAudioDuration: Start time must be less than end time and

Error message

TrimAudioDuration: Start time must be less than end time and be within the audio length.

What it means

TrimAudioDuration computes start_frame and end_frame in samples (with negative start interpreted as offset-from-end) and clamps both to [0, audio_length]. If after clamping start_frame >= end_frame — zero/negative requested duration, start beyond audio length, or a negative start pushing the window off the end — it raises.

Source

Thrown at comfy_extras/nodes_audio.py:477

            return IO.NodeOutput(None)
        waveform = audio["waveform"]
        sample_rate = audio["sample_rate"]
        audio_length = waveform.shape[-1]

        if audio_length == 0:
            return IO.NodeOutput(audio)

        if start_index < 0:
            start_frame = audio_length + int(round(start_index * sample_rate))
        else:
            start_frame = int(round(start_index * sample_rate))
        start_frame = max(0, min(start_frame, audio_length))

        end_frame = start_frame + int(round(duration * sample_rate))
        end_frame = max(0, min(end_frame, audio_length))

        if start_frame >= end_frame:
            raise ValueError("TrimAudioDuration: Start time must be less than end time and be within the audio length.")

        return IO.NodeOutput({"waveform": waveform[..., start_frame:end_frame], "sample_rate": sample_rate})

    trim = execute  # TODO: remove


class SplitAudioChannels(IO.ComfyNode):
    @classmethod
    def define_schema(cls):
        return IO.Schema(
            node_id="SplitAudioChannels",
            search_aliases=["stereo to mono"],
            display_name="Split Audio Channels",
            description="Separates the audio into left and right channels.",
            category="audio",
            inputs=[
                IO.Audio.Input("audio"),
            ],

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Validate duration > 0 and 0 <= start < audio duration before the node call
  2. For negative starts, ensure abs(start) leaves at least duration seconds of audio: audio_length/sr - abs(start)*sr >= duration*sr
  3. Compute segments against the actual waveform length (waveform.shape[-1] // sample_rate) instead of nominal metadata
  4. Skip or pass-through when the requested window is empty (the node already returns untrimmed audio for some None/empty cases)

Example fix

// before
TrimAudioDuration(audio, start_time=120.0, duration=10.0)  # 60s clip

// after
length_s = waveform.shape[-1] / sample_rate
start_time = min(start_time, max(0.0, length_s - duration))
TrimAudioDuration(audio, start_time=start_time, duration=duration)
Defensive patterns

Strategy: validation

Validate before calling

def trim_window_valid(waveform, sample_rate, start, duration) -> bool:
    n = waveform.shape[-1]
    if duration <= 0:
        return False
    s = n + round(start * sample_rate) if start < 0 else round(start * sample_rate)
    s = max(0, min(s, n))
    e = max(0, min(s + round(duration * sample_rate), n))
    return s < e

Try / catch

try:
    out = TrimAudioDuration.execute(audio, start_time, duration)
except ValueError as e:
    if 'Start time must be less than end time' in str(e):
        out = audio  # nothing to trim; pass through
    else:
        raise

Prevention

When it happens

Trigger: duration=0 or negative; start_time pointing at or past the end of the clip; negative start_time whose absolute offset exceeds remaining audio after clamping (e.g. start=-1000s on a 5s clip); extremely short audio with rounding collapsing the window to zero samples.

Common situations: Parameterized/batch trim workflows where duration is computed from metadata and can reach 0; loops trimming successive segments where the final segment's computed start lands past the end.

Related errors


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