spdl.io.Demuxer

class Demuxer(src: SourceType, *, demux_config: DemuxConfig | None = None, name: str | None = None, **kwargs: Any)[source]

Demuxer can demux audio, video and image from the source.

Parameters:
  • src – Source identifier. If str type, it is interpreted as a source location, such as local file path or URL. If bytes type, then they are interpreted as in-memory data. If array type (objects implement buffer protocol, such as NumPy NDArray and PyTorch Tensor), then they must be 1 dimensional uint8 array, which contains the raw bytes of the source.

  • demux_config (DemuxConfig) – Custom I/O config.

  • name – Optional custom name for this demuxer instance. When provided, this name will be used in error messages instead of generic identifiers like memory addresses. This is particularly useful when demuxing from in-memory data (bytes/arrays) to make debugging easier. For file-based sources, the filename already provides context, so this parameter is typically not needed.

Methods

demux_audio([window, bsf])

Demux audio from the source.

demux_image(*[, bsf])

Demux image from the source.

demux_video([window, bsf])

Demux video from the source.

has_audio()

Returns true if the source has audio stream.

streaming_demux()

Stream demux packets from the source.

Attributes

audio_codec

The codec metadata of the default audio stream.

audio_stream_index

The index of default audio stream.

image_codec

The codec metadata of the default image stream.

video_codec

The codec metadata of the default video stream.

video_stream_index

The index of default video stream.

property audio_codec: AudioCodec[source]

The codec metadata of the default audio stream.

property audio_stream_index: int[source]

The index of default audio stream.

demux_audio(window: tuple[float | Decimal | str | Fraction, float | Decimal | str | Fraction] | None = None, *, bsf: str | None = None) AudioPackets[source]

Demux audio from the source.

Parameters:
  • window – A time window specifying start and end times. If omitted, the entire audio is demuxed.

  • bsf – Bit-stream filter expression

Returns:

Demuxed audio packets.

demux_image(*, bsf: str | None = None) ImagePackets[source]

Demux image from the source.

Returns:

Demuxed image packets.

demux_video(window: tuple[float | Decimal | str | Fraction, float | Decimal | str | Fraction] | None = None, *, bsf: str | None = None) VideoPackets[source]

Demux video from the source.

Parameters:

window – A time window specifying start and end times. If omitted, the entire video is demuxed.

Returns:

Demuxed video packets.

has_audio() bool[source]

Returns true if the source has audio stream.

property image_codec: ImageCodec[source]

The codec metadata of the default image stream.

streaming_demux(indices: None = None, *, duration: float = -1, num_packets: int = -1) Iterator[VideoPackets | AudioPackets | ImagePackets][source]
streaming_demux(indices: int, *, duration: float = -1, num_packets: int = -1) Iterator[VideoPackets | AudioPackets | ImagePackets]
streaming_demux(indices: Sequence[int] | set[int], *, duration: float = -1, num_packets: int = -1) Iterator[dict[int, VideoPackets | AudioPackets | ImagePackets]]

Stream demux packets from the source.

Example - Streaming decoding audio

src = "foo.mp4"
with spdl.io.Demuxer(src) as demuxer:
    index = demuxer.audio_stream_index
    audio_decoder = spdl.io.Decoder(demuxer.audio_codec)
    packet_stream = demuxer.streaming_demux([index], duration=3)
    for packets in packet_stream:
        if index in packets:
            frames = decoder.decode(packets[index])
            buffer = spd.io.convert_frames(frames)
property video_codec: VideoCodec[source]

The codec metadata of the default video stream.

property video_stream_index: int[source]

The index of default video stream.