cutcutcodec.core.io.read_ffmpeg.ContainerInputFFMPEG

class cutcutcodec.core.io.read_ffmpeg.ContainerInputFFMPEG(filename: Path | str | bytes, **av_kwargs)[source]

Allow to decode a multimedia file with ffmpeg.

Attributes

av_kwargsdict[str]

The parameters passed to av.open.

filenamepathlib.Path

The path to the physical file that contains the extracted video stream (readonly).

Notes

In order to avoid the folowing error :

av.error.InvalidDataError: [Errno 1094995529] Invalid data found when processing input; last error log: [libdav1d] Error parsing OBU data

Which happens when reading a multi-stream file sparingly, The instances of av.container.InputContainer are new for each stream.

Examples

>>> import torch
>>> from cutcutcodec.core.io.read_ffmpeg import ContainerInputFFMPEG
>>> from cutcutcodec.utils import get_project_root
>>> video = get_project_root() / "media" / "video" / "intro.webm"
>>> with ContainerInputFFMPEG(video) as container:
...     for stream in container.out_streams:
...         if stream.type == "video":
...             stream.snapshot(0, (stream.height, stream.width)).shape
...         elif stream.type == "audio":
...             torch.round(stream.snapshot(0, rate=2, samples=3), decimals=5)
...
(720, 1280, 3)
(360, 640, 3)
FrameAudio(0, 2, 'stereo', [[     nan,  0.1804 , -0.34765],
                            [     nan, -0.07236,  0.07893]])
FrameAudio(0, 2, 'mono', [[     nan,  0.06998, -0.24758]])
>>>

Initialise and create the class.

Parameters

filenamepathlike

Path to the file to be decoded.

**av_kwargsdict

Directly transmitted to av.open.

  • "format" (str): Specific format to use. Defaults to autodect.

  • "options" (dict): Options to pass to the container and all streams.

  • "container_options" (dict): Options to pass to the container.

  • "stream_options" (list): Options to pass to each stream.

  • "metadata_encoding" (str): Encoding to use when reading or writing file metadata.

    Defaults to “utf-8”.

  • "metadata_errors" (str): Specifies how to handle encoding errors;

    behaves like str.encode parameter. Defaults to “strict”.

  • "buffer_size" (int): Size of buffer for Python input/output operations in bytes.

    Honored only when file is a file-like object. Defaults to 32768 (32k).

  • "timeout" (float or tuple): How many seconds to wait for data before giving up,

    as a float, or a (open timeout, read timeout) tuple.

Raises

cutcutcodec.core.exceptions.DecodeError

If it fails to extract any multimedia stream from the provided file.

property av_kwargs: dict[str]

Return the parameters passed to av.open.

property filename: Path

Return the path to the physical file that contains the extracted video stream.