Encoders

Encoder objects are passed to the encoder parameter of bewegung.Video.render(). While bewegung handles the composition of video frames, the completed frames are streamed to an actual video encoding tool or library. It is possible to define custom encoders.

Available Encoders

class bewegung.FFmpegH264Encoder(buffersize=134217728, preset='slow', crf=17, tune='animation')

Mutable. Context manager. Wraps FFmpeg with H.264.

Parameters
class bewegung.FFmpegGifEncoder(buffersize=134217728)

Mutable. Context manager. Wraps FFmpeg with gif.

Parameters

buffersize (int) – Maximum size of buffer in bytes between bewegung and ffmpeg. A larger buffer may have a mildly positive impact on performance.

Defining Custom Encoders

Custom encoders can be defined by deriving from the bewegung.EncoderBase class. This mechanism can be used to build both custom ffmpeg-based pipelines as well as wrap other tools such as mencoder.

The EncoderBase Class

class bewegung.EncoderBase

Encoder classes wrap video encoding tools and libraries such as ffmpeg. Encoder objects are callable and return themselves when called. This mechanism is used to (re-) configure the encoder object. Encoder objects also use Python’s context manager protocol and expose BinaryIO objects, i.e. streams, as a context for actual encoding. bewegung.Video.render() will write rendered images as RGB bitmaps to this stream so the encoder can pick them up. Encoder objects can either be “running” or “idling”. They can also either be “configured” or “unconfigured”. In the latter case, they will not allow to encode a video.

Mutable.

If the orginal cunstructor method is overridden, it must be called from the child class.

__call__(video, video_fn)

Configures the encoder. Returns encoder object itself.

Do not override!

Parameters
  • video (VideoABC) – Video object

  • video_fn (str) – Location and name (path) of where to store the video file. If omitted, no video will be rendered. However, indivual frames may in fact still be rendered if frame_fn has been specified.

Return type

EncoderABC

__enter__()

Context manager entry point. Returns the encoder’s input stream.

Do not override!

Return type

BinaryIO

__exit__(exc_type, exc_value, traceback)

Context manager exit point.

Do not override!

Parameters
  • exc_type (Optional[Type]) – Type of exception

  • exc_value (Optional[Exception]) – Actual exception object

  • traceback (Optional[traceback]) – Related traceback object

_enter()

Starts the encoder. Returns the encoder’s input stream.

Must be reimplemented!

Return type

BinaryIO

_exit()

Stops the encoder.

Must be reimplemented!

property configured: bool

Has the encoder been configured?

Do not override!

Return type

bool

property running: bool

Is the encoder currently running?

Do not override!

Return type

bool

property stream: BinaryIO

Exposes the input stream of the encoder. If the encoder is not running, trying to access this attribute raises an exception.

Do not override!

Return type

BinaryIO

property video_fn: str

Exposes the name/path of the target video file. If the encoder is not configured, trying to access this attribute raises an exception.

Do not override!

Return type

str