Comfy-Org/ComfyUI · error · RuntimeError

Failed to trim video: {str(e)}

Error message

Failed to trim video: {str(e)}

What it means

Generic wrapper around any failure inside the PyAV-based trim_video routine. The except block closes both containers and re-raises the original exception text as RuntimeError('Failed to trim video: ...'). The root cause is whatever exception the inner message reports — decode errors, muxer errors, or the explicit 'Video too short' ValueError from the same function.

Source

Thrown at comfy_api_nodes/util/conversions.py:414

                output_container.mux(packet)

            logging.info("Encoded %s audio frames", audio_frame_count)

        # Close containers
        output_container.close()
        input_container.close()

        # Return as VideoFromFile using the buffer
        output_buffer.seek(0)
        return InputImpl.VideoFromFile(output_buffer)

    except Exception as e:
        # Clean up on error
        if input_container is not None:
            input_container.close()
        if output_container is not None:
            output_container.close()
        raise RuntimeError(f"Failed to trim video: {str(e)}") from e


def downscale_video_to_max_pixels(video: Input.Video, max_pixels: int) -> Input.Video:
    """Downscale a video to fit within ``max_pixels`` (w * h), preserving aspect ratio.

    Returns the original video object untouched when it already fits. Preserves frame rate, duration, and audio.
    Aspect ratio is preserved up to a fraction of a percent (even-dim rounding).
    """
    src_w, src_h = video.get_dimensions()
    scale_dims = _compute_downscale_dims(src_w, src_h, max_pixels)
    if scale_dims is None:
        return video
    return _apply_video_scale(video, scale_dims)


def _compute_upscale_dims(src_w: int, src_h: int, total_pixels: int) -> tuple[int, int] | None:
    """Return upscaled (w, h) with even dims meeting at least ``total_pixels``, or None if already large enough.

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Read the suffix after 'Failed to trim video:' — it names the real underlying error; fix that first.
  2. If it is the 16-frame minimum, increase duration (see error 762).
  3. Re-export the source as standard MP4 (H.264 + AAC) and retry.
  4. Verify the file opens in ffprobe/VLC before feeding it to the node.
Defensive patterns

Strategy: try-catch

Validate before calling

import av

with av.open(path) as c:
    if not c.streams.video:
        raise ValueError('Input has no video stream')

Try / catch

try:
    trimmed = trim_video(video, start_time, duration)
except RuntimeError as e:
    # message suffix carries the root cause; handle specific causes here
    log.error('trim failed: %s', e)
    raise

Prevention

When it happens

Trigger: trim_video on a corrupted or non-video file (av.open or decode raises), a source with no video stream, an incompatible codec/container combination for re-encoding, or the inner ValueError('Video too short: need at least 16 frames for Moonvalley').

Common situations: User uploads a file with a wrong extension (e.g. .mp4 that is actually HTML from a failed download); hardware without the required encoder; a video with only an audio stream; trimming below the 16-frame minimum described in error 762.

Related errors


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