cutcutcodec.core.io.write_ffmpeg

Management of the encoding of a multimedia stream based on PyAV.

Classes

ContainerOutputFFMPEG(in_streams, filename, ...)

Allow to write the output file to disk.

Functions

frame_audio_to_av(frame_audio)

Convert a FrameAudio cutcutcodec into a av audio frame for encoding.

frame_video_to_av(frame_video[, full, yuv])

Convert a FrameVideo cutcutcodec into a av video frame for encoding.

Details

class cutcutcodec.core.io.write_ffmpeg.ContainerOutputFFMPEG(in_streams: Iterable[Stream], filename: Path | str | bytes, streams_settings: Iterable[dict], container_settings: dict | None = None)[source]

Allow to write the output file to disk.

Attributes

filenamepathlib.Path

The absolute path + name of the file to encode (readonly).

streams_settingslist[dict]

Information related to each codec (readonly).

container_settingsdict

Global container file information (readonly).

Examples

>>> import os
>>> from cutcutcodec.core.filter.audio.subclip import FilterAudioSubclip
>>> from cutcutcodec.core.filter.video.subclip import FilterVideoSubclip
>>> from cutcutcodec.core.generation.audio.noise import GeneratorAudioNoise
>>> from cutcutcodec.core.generation.video.noise import GeneratorVideoNoise
>>> from cutcutcodec.core.io.write_ffmpeg import ContainerOutputFFMPEG
>>> streams_settings = [
...     {"encodec": "libopus", "rate": 8000},
...     {"encodec": "libx264", "rate": 12, "shape": (2, 2)},
... ]
>>> container_settings = {"format": "matroska"}
>>> (stream_a,) = FilterAudioSubclip(GeneratorAudioNoise(0).out_streams, 0, 1).out_streams
>>> (stream_v,) = FilterVideoSubclip(GeneratorVideoNoise(0).out_streams, 0, 1).out_streams
>>> streams = (stream_a, stream_v)
>>> ContainerOutputFFMPEG(streams, os.devnull, streams_settings, container_settings).write()
>>>

Initialise and create the class.

Parameters

in_streamstyping.Iterable[Stream]

The ordered video or audio streams to be encoded. For more information, please refer to initializator of cutcutcodec.core.classes.container.ContainerOutput.

filenamepathlike

Path to the file to be encoded.

streams_settingstyping.Iterable[dict]

These are the encoding parameters associated with each stream. They contain all the information about the codecs. For audio streams, here is the format to follow:

  • “encodec”: str, # name of the codec or encoding library (ex libopus)

  • “rate”: int or str, # (autodetect) samplerate in Hz (ex 48000)

  • “options”: dict, # (optional) option for codec (ex {“application”: “voip”})

  • “bitrate”: int, # (optional) the flow in bits/s (ex 1024000)

For video streams, here is the format to follow:

  • “encodec”: str, # name of the codec or encoding library (ex libx264)

  • “rate”: numbers.Real or str, # (autodetect) the framerate in Hz (ex “30000/1001”)

  • “shape”: tuple[int, int], # (autodetect) shape (height, width) of the frames

  • “options”: dict, # (optional) option for codec (ex {“crf”: “23”})

  • “bitrate”: int, # (optional) the flow in bits/s (ex 6400000)

  • “pix_fmt”: str, # (optional) pixel format (ex “yuv444p10le”)

  • “range”: str, # (default = “tv”) “tv” == “limited” or “pc” == “full”

container_settingsdict, optional

Global container file information. must contain the following fields:

  • “format”: str or None, # specific format to use, defaults to autodect

  • “container_options”: dict, # (optional) options to pass to the container

  • “options”: dict, # (optional) options to pass to the container and all streams

cutcutcodec.core.io.write_ffmpeg.frame_audio_to_av(frame_audio: FrameAudio) AudioFrame[source]

Convert a FrameAudio cutcutcodec into a av audio frame for encoding.

Parameters

frame_audiocutcutcodec.core.classes.frame_audio.FrameAudio

The torch frame to cast.

Returns

av_frameav.audio.frame.audioFrame

The equivalent av audio frame containing a similar audio signal.

Examples

>>> import torch
>>> from cutcutcodec.core.classes.frame_audio import FrameAudio
>>> from cutcutcodec.core.io.write_ffmpeg import frame_audio_to_av
>>>
>>> frame_audio_to_av(FrameAudio(10, 48000, "mono", torch.empty(1, 1024)))  
<av.AudioFrame pts=480000, 1024 samples at 48000Hz, mono, flt at ...
>>> frame_audio_to_av(FrameAudio(10, 48000, "5.1", torch.empty(6, 1024)))  
<av.AudioFrame pts=480000, 1024 samples at 48000Hz, 5.1, flt at ...
>>>
cutcutcodec.core.io.write_ffmpeg.frame_video_to_av(frame_video: FrameVideo, full: bool = False, yuv: bool = True) VideoFrame[source]

Convert a FrameVideo cutcutcodec into a av video frame for encoding.

Parameters

frame_videocutcutcodec.core.classes.frame_video.FrameVideo

The torch frame video to cast.

fullboolean, default=False

If set to True, encode in full range rather limited range as default.

yuvboolean, default=True

If set to false, return a frame in rgb pixel format rather yuv.

Returns

av_frameav.video.frame.VideoFrame

The equivalent av video frame containing the similar image in format bgr24.

Examples

>>> import torch
>>> from cutcutcodec.core.classes.frame_video import FrameVideo
>>> from cutcutcodec.core.io.write_ffmpeg import frame_video_to_av
>>>
>>> frame_video_to_av(
...     FrameVideo(10, torch.zeros(480, 720, 3))
... )  
<av.VideoFrame, pts=3003000 yuv444p16le 720x480 at ...>
>>> frame_video_to_av(
...     FrameVideo(10, torch.zeros(480, 720, 4))
... )  
<av.VideoFrame, pts=3003000 yuva444p16le 720x480 at ...>
>>> frame_video_to_av(
...     FrameVideo(10, torch.zeros(480, 720, 3)),
...     yuv=False,
... )  
<av.VideoFrame, pts=3003000 rgb24 720x480 at ...>
>>>