FrameIndex#
- class torchcodec.decoders.FrameIndex(_is_key_frame: Tensor, _pts: Tensor, _duration: Tensor, _time_base_num: int, _time_base_den: int)[source]#
What the packets of a video stream say about its frames, as returned by a scan (
VideoStream.scan()).Its members come in three shapes:
Per-frame tensors, of shape
[N]whereNis the number of frames: one entry per frame, in presentation order. These areis_key_frame,pts_secondsandduration_seconds.Keyframe positions,
key_frame_indices, of shape[K]whereKis the number of keyframes. These are indices into the per-frame tensors, not frames themselves.Stream-level metadata scalars, one value for the whole stream:
num_frames_from_content,begin_stream_seconds_from_content,end_stream_seconds_from_contentandaverage_fps_from_content.
index_at()andkey_frame_seconds_for()are convenience methods that search those tensors so that you don’t have to. Together withDemuxer.seek(), they are what an exact seek is built from:frame_index = video_stream.scan() # Reach the frame displayed at 12.5 seconds target = frame_index.pts_seconds[frame_index.index_at(12.5)] demuxer.seek(frame_index.key_frame_seconds_for(target)) packet_decoder.reset() # ... then decode forward, dropping the frames before `target`
Everything here is derived from the stream’s packets rather than from the container header, so it is exact where the header is only a claim. That is what the
_from_contentsuffixes mark, against the_from_headerones onVideoStream.metadata.Examples using
FrameIndex:- property duration_seconds: Tensor#
Float64 tensor of shape
[N], how long each frame is displayed for.
- property end_stream_seconds_from_content: float#
The time at which the last frame stops being displayed.
This is the largest
pts + durationacross the stream, not the last frame’s own end time. Durations vary, so the frame that finishes last isn’t necessarily the one that starts last.
- index_at(seconds: float | Tensor) int | Tensor[source]#
The index of the frame being displayed at
seconds.A frame is displayed from its own pts until that plus its duration, so this is the frame whose interval contains
seconds.
- key_frame_seconds_for(seconds: float) float[source]#
The timestamp of the last keyframe at or before
seconds.
- property pts_seconds: Tensor#
Float64 tensor of shape
[N], the pts of each frame.NaNfor the frames whose packet has no pts. Streams like raw H.264 or HEVC streams have none at all, so their frames can’t be put in presentation order:is_key_frame,key_frame_indices,index_at()andkey_frame_seconds_for()raise on them.