captionbob docs

Version 1.0.0
Contents ↓

Python: captionbob.render

captionbob.render.ffmpeg_expression

The ffmpeg expressions of the image layers, read with no ffmpeg.

An emoji layer gives the filters scale and overlay of the burn three ffmpeg expressions: the side, the left column and the top row of its image. The live preview draws the layers in the browser, which has no ffmpeg, so the library works out the value of those same expressions on each frame.

The reader follows the expression code of ffmpeg, libavutil/eval.c:

  • The spaces are taken out first.
  • A sum is terms joined by + and -. A term is factors joined by * and /.
  • A factor is a value with one optional sign in front and powers with ^. The sign is put on after the powers, so -2^2 is -4, and a power can have its own sign, so 2^-1 is 0.5.
  • A value is a number, a name, a function call or a sum in brackets. A number can have its own sign, as in 1.1+-0.1.
  • A division by 0 gives an infinity, and 0/0 gives NaN.
  • The function if works out only the value that it gives.

The reader takes only the part of the ffmpeg expressions that the image layers use: the functions of FUNCTION_ARGUMENT_COUNTS, the constant PI and the names that the caller gives. It refuses any other function or name, a number with a postfix such as 2k, and a sequence with ;. So a new layer with a new function fails here with a clear message, not in the browser.

Source file: captionbob/render/ffmpeg_expression.py

FfmpegExpression

class FfmpegExpression
    def __init__(self, text: str) -> None

One ffmpeg expression, read once and worked out for many frames.

Args:

  • text: The expression, as a layer gives it to ffmpeg.

Raises:

  • ValueError: When the text is not an expression that the reader takes.

FfmpegExpression.value

def value(self, names: Mapping[str, float]) -> float

Work out the value of the expression.

Args:

  • names: The value of each name that the expression may use, such as the time t of a frame.

Returns:

The value, which can be an infinity or NaN, as in ffmpeg.

Raises:

  • ValueError: When the expression uses a name that names does not have and that is not a constant.