VideoCodecParameters#
- class torchcodec.decoders.VideoCodecParameters(codec: str, *, time_base: Fraction, extradata: bytes | None = None, codec_tag: int | None = None, profile: int | None = None, level: int | None = None, bit_rate: int | None = None, bits_per_coded_sample: int | None = None, bits_per_raw_sample: int | None = None, coded_side_data: dict[str, bytes] | None = None, width: int | None = None, height: int | None = None, pixel_format: str | None = None, sample_aspect_ratio: Fraction | None = None, framerate: Fraction | None = None, r_frame_rate: Fraction | None = None, field_order: str | None = None, color_range: str | None = None, color_primaries: str | None = None, color_transfer_characteristic: str | None = None, color_space: str | None = None, chroma_location: str | None = None, video_delay: int | None = None)[source]#
What a
VideoPacketDecoderneeds to know to decode the packets of a video stream.If you have a
Demuxer,VideoStream.codec_parametersgives you the parameters of a stream. They can be pickled, e.g. to build the decoder in another process. To decode packets that don’t come from aDemuxer, build them yourself:codec_parameters = VideoCodecParameters( "h264", time_base=Fraction(1, 90_000), extradata=extradata ) decoder = VideoPacketDecoder(codec_parameters)
The fields mirror those of FFmpeg’s AVCodecParameters, with the same names and meanings. FFmpeg’s names stand for its enums, e.g.
pixel_format="yuv420p".Note
Which fields you need depends on the codec and device: printing the parameters a
Demuxerreports for a similar file shows what FFmpeg uses.To decode on CUDA, set
width,heightandpixel_format: depending on the codec, the frames may otherwise be silently decoded on the CPU. Also setr_frame_rate, or the frames’ durations are 0.Note
The parameters are meant to be passed to a decoder or stored, not read as metadata: use
VideoStream.metadatafor that. The values aDemuxerreports are FFmpeg’s, and can change across FFmpeg and TorchCodec versions. The fields’ names and meanings are stable.Examples using
VideoCodecParameters:- bits_per_coded_sample: int | None = None#
The number of bits per sample in the compressed data, for the codecs that need it (mostly uncompressed and PCM codecs).
- bits_per_raw_sample: int | None = None#
The number of bits per sample in the decoded data, when it’s not implied by the pixel or sample format, e.g. 10 for 10-bit video.
- codec_tag: int | None = None#
The container’s identifier of the codec, e.g. a FourCC in AVI or MP4. A few codecs read it to tell variants apart.
- coded_side_data: dict[str, bytes] | None = None#
Extra data about the stream, keyed by FFmpeg’s names for its types, e.g.
"Display Matrix"for video or"Replay Gain"for audio.If a video stream has a display matrix, it is under
"Display Matrix"on every FFmpeg version.
- color_range: str | None = None#
FFmpeg’s name of the color range, e.g.
"tv"(limited) or"pc"(full).
- color_space: str | None = None#
FFmpeg’s name of the color space, i.e. the YUV matrix, e.g.
"bt709".
- color_transfer_characteristic: str | None = None#
FFmpeg’s name of the transfer characteristic, e.g.
"bt709"or"smpte2084".
- extradata: bytes | None = None#
The codec’s setup data that isn’t in the packets, e.g. the
avcCbox of H.264 in MP4. Not all codecs and streams need it.
- field_order: str | None = None#
"progressive"or, for interlaced video, which field comes first:"tt","bb","tb"or"bt".
- framerate: Fraction | None = None#
The frame rate, in frames per second, as the codec reports it. Setting it requires FFmpeg 6.1 or above.
- pixel_format: str | None = None#
FFmpeg’s name of the decoded frames’ pixel format, e.g.
"yuv420p".ffmpeg -pix_fmtslists them.
- r_frame_rate: Fraction | None = None#
The frame rate, in frames per second, as the container reports it. Not part of FFmpeg’s
AVCodecParameters, but of itsAVStream.
- time_base: Fraction#
a packet’s
ptsofnmeansn * time_baseseconds.- Type:
The unit of the packets’ timestamps, in seconds