{"record":{"id":"7e17d81c54855909","repo":"Comfy-Org/ComfyUI","slug":"failed-to-trim-video-str-e","errorCode":null,"errorMessage":"Failed to trim video: {str(e)}","messagePattern":"Failed to trim video: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"comfy_api_nodes/util/conversions.py","lineNumber":414,"sourceCode":"                output_container.mux(packet)\n\n            logging.info(\"Encoded %s audio frames\", audio_frame_count)\n\n        # Close containers\n        output_container.close()\n        input_container.close()\n\n        # Return as VideoFromFile using the buffer\n        output_buffer.seek(0)\n        return InputImpl.VideoFromFile(output_buffer)\n\n    except Exception as e:\n        # Clean up on error\n        if input_container is not None:\n            input_container.close()\n        if output_container is not None:\n            output_container.close()\n        raise RuntimeError(f\"Failed to trim video: {str(e)}\") from e\n\n\ndef downscale_video_to_max_pixels(video: Input.Video, max_pixels: int) -> Input.Video:\n    \"\"\"Downscale a video to fit within ``max_pixels`` (w * h), preserving aspect ratio.\n\n    Returns the original video object untouched when it already fits. Preserves frame rate, duration, and audio.\n    Aspect ratio is preserved up to a fraction of a percent (even-dim rounding).\n    \"\"\"\n    src_w, src_h = video.get_dimensions()\n    scale_dims = _compute_downscale_dims(src_w, src_h, max_pixels)\n    if scale_dims is None:\n        return video\n    return _apply_video_scale(video, scale_dims)\n\n\ndef _compute_upscale_dims(src_w: int, src_h: int, total_pixels: int) -> tuple[int, int] | None:\n    \"\"\"Return upscaled (w, h) with even dims meeting at least ``total_pixels``, or None if already large enough.\n","sourceCodeStart":396,"sourceCodeEnd":432,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy_api_nodes/util/conversions.py#L396-L432","documentation":"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.","triggerScenarios":"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').","commonSituations":"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.","solutions":["Read the suffix after 'Failed to trim video:' — it names the real underlying error; fix that first.","If it is the 16-frame minimum, increase duration (see error 762).","Re-export the source as standard MP4 (H.264 + AAC) and retry.","Verify the file opens in ffprobe/VLC before feeding it to the node."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"import av\n\nwith av.open(path) as c:\n    if not c.streams.video:\n        raise ValueError('Input has no video stream')","typeGuard":null,"tryCatchPattern":"try:\n    trimmed = trim_video(video, start_time, duration)\nexcept RuntimeError as e:\n    # message suffix carries the root cause; handle specific causes here\n    log.error('trim failed: %s', e)\n    raise","preventionTips":["Validate the file opens and has a video stream before the trim call.","Keep trim windows inside the clip duration and above the 16-frame minimum.","Pre-normalize exotic sources to H.264 MP4 with ffmpeg."],"tags":["video","pyav","wrapper-exception","trim"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}