captionbob docs

Version 1.0.0
Contents ↓

Python: captionbob.effects

captionbob.effects.word_magnify

Word magnify effect: each word is large when it is spoken.

A caption shows a few words, and it shows them whole from its start. A word is large at the time at which it is spoken. It shrinks in a straight line to its normal size in the first part of the time in which it is spoken. The large word grows around its own middle and is drawn over the words next to it, so no other word moves.

Each word has an event of its own on a layer above the caption. A large word makes its line wider, and libass would then break the line at another place. So the effect asks the caption measure where libass breaks each caption, and it writes these line breaks into the script.

The effect draws nothing itself. It builds the text of an ASS script, and the ffmpeg filter ass draws the script on the video. The common part of the effects is in effect_common.py.

Source file: captionbob/effects/word_magnify.py

Settings

The command line and the HTTP API call this effect the style word_magnify. Its settings class is WordMagnifySettings. Each setting has the default below.

Setting Type Default Description
most_words integer 3 The most words that one caption shows.
fewest_words integer 1 The fewest words that one caption shows, when its sentence is long enough for that.
font_name string "DejaVu Sans" Name of the font of the text.
font_size integer 80 Height of the font in pixels.
bold boolean true True for bold text.
primary_color string "#FFFFFF" Color of the text, as "#RRGGBB".
stroke_color string "#000000" Color of the outline of the text, as "#RRGGBB".
stroke_width number 6.0 Width of the outline in pixels. 0 gives no outline.
bottom_position number 0.75 Where the bottom of the caption sits, as a part of the video height from the top. 0.75 is three quarters of the way down.
side_margin integer 54 Room in pixels that stays free on the left and on the right of the caption. A caption that is too wide for the room between the margins breaks into lines.
magnification number 1.5 How many times larger a word is when it is spoken, 1 or above. 1 gives a caption in which no word grows.
magnify_part number 0.5 The part of the time of a word, at its start, in which the word shrinks to its normal size, from 0 to 1. 0 gives a caption in which no word grows.
fade_seconds number 0.1 Time in which a caption fades in, and in which it fades out. 0 gives no fade.
hold_seconds number 0.6 The longest time that a caption stays after its last word ends. The caption goes earlier when the next caption starts.

WordMagnifySettings

@dataclass(frozen=True)
class WordMagnifySettings(CaptionSettings)

The look and the timing of the word magnify effect.

The settings of CaptionSettings come first: the word counts, the font, the colors, the stroke, the bottom position and the side margin. These settings follow them. In this effect the outline of a large word is larger by the same factor as the word.

Args:

  • magnification: How many times larger a word is when it is spoken, 1 or above. 1 gives a caption in which no word grows.
  • magnify_part: The part of the time of a word, at its start, in which the word shrinks to its normal size, from 0 to 1. 0 gives a caption in which no word grows.
  • fade_seconds: Time in which a caption fades in, and in which it fades out. 0 gives no fade.
  • hold_seconds: The longest time that a caption stays after its last word ends. The caption goes earlier when the next caption starts.

Raises:

  • TypeError: When a word count is not a whole number.
  • ValueError: When a value is outside its range, when a color is not in the form "#RRGGBB", or when the font name is empty or holds a comma.

WordMagnifyEffect

class WordMagnifyEffect
    def __init__(
            self, settings: WordMagnifySettings,
            caption_measure: CaptionMeasure) -> None

Builds the ASS script in which each word is large when it is spoken.

Args:

  • settings: The look and the timing of the effect.
  • caption_measure: The measure that says where libass draws each word of a caption. It must use the ffmpeg that draws the video.

WordMagnifyEffect.build_ass_text

def build_ass_text(self, words: Sequence[Word], video: VideoInfo) -> str

Build the ASS script of the effect for the words of one video.

Each caption group has one base event, which shows each word until the word is spoken. Each word that grows has one word event, which shows the word from the time at which it is spoken. All events of a caption start with its first word. They end when the next caption starts, and at the latest the hold time after its last word and at the end of the video.

Args:

  • words: The words of the video in time order, each with its times.
  • video: The frame size and the duration of the video.

Returns:

The text of the ASS script. It has no event when there are no words.

Raises:

  • ValueError: When the side margins leave no room for the caption in the width of the video, or when libass draws no pixel for a word.
  • RuntimeError: When the ffmpeg of the caption measure fails.