spdl.io.Muxer

class Muxer(dst: str | Path, /, *, format: str | None = None)[source]

Multiplexer that convines multiple packet streams. e.g. create a video

Parameters:
  • dst – The destination such as file path, pipe, URL (such as RTMP, UDP).

  • format

    Optional Override the output format, or specify the output media device. This argument serves two different use cases.

    1. Override the output format. This is useful when writing raw data or in a format different from the extension.

    2. Specify the output device. This allows to output media streams to hardware devices, such as speaker and video screen.

    Note

    This option roughly corresponds to -f option of ffmpeg command. Please refer to the ffmpeg documentations for possible values.

    https://ffmpeg.org/ffmpeg-formats.html#Muxers

    For device access, the available values vary based on hardware (AV device) and software configuration (ffmpeg build). Please refer to the ffmpeg documentations for possible values.

    https://ffmpeg.org/ffmpeg-devices.html#Output-Devices

Methods

add_encode_stream()

Add an output stream with encoding.

add_remux_stream()

Add an output stream without encoding.

close()

Close the resource.

flush()

Notify the muxer that all the streams are written.

open([muxer_config])

Open the muxer (output file) for writing.

write(stream_index, packets)

Write packets to muxer.

add_encode_stream(config: AudioEncodeConfig, *, encoder: str | None = None, encoder_config: dict[str, str] | None = None) AudioEncoder[source]
add_encode_stream(config: VideoEncodeConfig, *, encoder: str | None = None, encoder_config: dict[str, str] | None = None) VideoEncoder

Add an output stream with encoding.

Use this method when you want to create a media from tensor/array.

Parameters:
  • config – Encoding (codec) configuration. See the corresponding factory functions for the detail. (audio_encode_config() and video_encode_config())

  • encoder – Specify or override the encoder to use. Use ffmpeg -encoders to list the available encoders.

  • encoder_config – Encoder-specific options. Use ffmpeg -h encoder=<ENCODER> to list the available options.

Returns:

Encoder object which can be used to encode frames object into packets

object.

add_remux_stream(codec: AudioCodec) None[source]
add_remux_stream(codec: VideoCodec) None

Add an output stream without encoding.

Use this method when you want to pass demuxed packets to output stream without decoding.

Parameters:

codec – Codec parameters from the source.

Example

demuxer = spdl.io.Demuxer(“source_video.mp4”)

muxer = spdl.io.Muxer(“stripped_audio.aac”) muxer.add_remux_stream(demuxer.audio_codec)

with muxer.open():
for packets in demuxer.streaming_demux(num_packets=5):

muxer.write(0, packets)

close() None[source]

Close the resource.

This is automatically called when using Muxer as context manager.

flush() None[source]

Notify the muxer that all the streams are written.

This is automatically called when using Muxer as context manager.

open(muxer_config: dict[str, str] | None = None) Muxer[source]

Open the muxer (output file) for writing.

Parameters:

muxers. (Options spefici to devices and)

Example - Protocol option

muxer = spdl.io.Muxer(“rtmp://localhost:1234/live/app”, format=”flv”) muxer.add_encode_stream(…) # Passing protocol option listen=1 makes Muxer act as RTMP server. with muxer.open(muxer_config={“listen”: “1”}):

muxer.write(0, video_packet)

Example - Device option

muxer = spdl.io.Muxer(“-”, format=”sdl”) muxer.add_encode_stream(…) # Open SDL video player with fullscreen with muxer.open(muxer_config={“window_fullscreen”: “1”}):

muxer.write(0, video_packet)

write(stream_index: int, packets: AudioPackets | VideoPackets) None[source]

Write packets to muxer.

Parameters:
  • stream_index – The stream to write to.

  • packets – Audio/video data.