Comfy-Org/ComfyUI · error · ValueError

Could not determine duration for file '{self.__file}'

Error message

Could not determine duration for file '{self.__file}'

What it means

VideoFromFile.get_duration tries container duration, then stream duration, then falls back to decoding/counting packets and dividing by average_rate. If every strategy fails (no usable metadata, and zero decodable/demuxed frames so frame_count == 0), it raises — the file claims to be a video but yields no timing information.

Source

Thrown at comfy_api/latest/_input_impl/video_types.py:214

            )
            if video_stream and video_stream.frames and video_stream.average_rate:
                return float(video_stream.frames / video_stream.average_rate)

            # Last resort: decode frames to count them
            if video_stream and video_stream.average_rate:
                frame_count = 0
                container.seek(0)
                frame_iterator = (
                    container.decode(video_stream)
                    if video_stream.codec.capabilities & 0x100
                    else container.demux(video_stream)
                )
                for packet in frame_iterator:
                    frame_count += 1
                if frame_count > 0:
                    return float(frame_count / video_stream.average_rate)

        raise ValueError(f"Could not determine duration for file '{self.__file}'")

    def get_frame_count(self) -> int:
        """
        Returns the number of frames in the video without materializing them as
        torch tensors.
        """
        if isinstance(self.__file, io.BytesIO):
            self.__file.seek(0)

        with av.open(self.__file, mode="r") as container:
            video_stream = self._get_first_video_stream(container)
            # 1. Prefer the frames field if available and usable
            if (
                video_stream.frames
                and video_stream.frames > 0
                and not self.__start_time
                and not self.__duration
            ):

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Verify the file plays in a player and ffprobe shows a duration; re-download/re-export if not
  2. For fragmented mp4, remux with ffmpeg -i in.mp4 -c copy out.mp4 to write proper header metadata
  3. If duration is genuinely unknown upstream, catch the error and require the user to pass duration explicitly

Example fix

// before
d = video_input.get_duration()  # raises on header-only mp4

# after
# remux first: ffmpeg -i in.mp4 -c copy fixed.mp4
d = VideoFromFile('fixed.mp4').get_duration()
Defensive patterns

Strategy: validation

Validate before calling

import av
with av.open(path) as c:
    ok = (c.duration or 0) > 0 or any((s.duration or 0) > 0 for s in c.streams)
if not ok:
    raise ValueError('no duration metadata; remux or fix the file before use')

Try / catch

try:
    dur = vi.get_duration()
except ValueError:
    dur = None  # require explicit duration from the caller

Prevention

When it happens

Trigger: Calling get_duration on a file with missing/zero duration metadata AND zero counted frames — severely truncated downloads, header-only mp4, or a stream where demux yields nothing.

Common situations: Interrupted download leaving a stub file; screen-recordings or fragmented mp4s with no header duration; exotic containers where PyAV cannot read duration.

Related errors


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