captionbob docs

Version 1.0.0
Contents ↓

Python: captionbob.effects

captionbob.effects.indigo

Indigo effect: narrow light capitals on an indigo box.

Each caption shows a few words in narrow, heavy, light gray capitals, with no end marks, such as commas and full stops. The words stand on a filled indigo box with round corners. A caption shows when its first word starts: the box and the words grow together from a part of their size to their full size in the pop time, each from its middle. The caption goes at once.

The letters are those of the font, made taller by the letter height. So the widths of the words stay the widths that libass gives for the font, and the caption measure gives them. The letters have an outline in their own color, which makes them heavier.

A caption is two events of the script at one place: the box on the lower layer and the text on the upper layer. libass draws no box with round corners behind a text, so the box is a drawing. The caption measure gives the columns and the rows of the text. The box is as wide as the text plus the side padding on each side, and as high as the rows of the text plus the top padding and the bottom padding. The effect writes the line breaks of the measure, so libass breaks no caption by itself.

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

The defaults are the look of the source video of the effect, measured on its frames and scaled to a video of 1080x1920. The source video shows mostly three words in each caption, which is the common default.

Source file: captionbob/effects/indigo.py

Settings

The command line and the HTTP API call this effect the style indigo. Its settings class is IndigoSettings. 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 "Ubuntu Condensed" Name of the font of the text.
font_size integer 96 Height of the font in pixels.
bold boolean true True for bold text.
primary_color string "#D7CECE" Color of the text, as "#RRGGBB".
stroke_color string "#D7CECE" Color of the outline of the text, as "#RRGGBB".
stroke_width number 3.0 Width of the outline in pixels. 0 gives no outline.
bottom_position number 0.703 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.
capitals boolean true True to write the words in capitals.
letter_height number 1.19 The height of the letters as a part of the height that the font gives them. A row of a caption is the font size times this part high.
box_color string "#260096" Color of the box, as "#RRGGBB".
box_side_padding number 19.0 Room in pixels between the text, with no outline, and the left and the right edge of the box.
box_top_padding number 1.0 Room in pixels between the top of the rows of the text and the top edge of the box.
box_bottom_padding number 8.0 Room in pixels between the bottom of the rows of the text and the bottom edge of the box.
corner_radius number 12.0 The radius of the corners of the box in pixels. 0 gives square corners.
pop_scale number 0.88 The size of a caption at its start, as a part of its full size.
pop_seconds number 0.06 The time in which a caption grows to its full size. 0 gives the full size from the start.
hold_seconds number 0.05 The longest time that a caption stays after its last word ends. The caption goes earlier when the next caption starts.

IndigoSettings

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

The look and the timing of the indigo 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. The bottom position is the place of the bottom of the rows of the text. The stroke is the outline of the letters.

Args:

  • capitals: True to write the words in capitals.
  • letter_height: The height of the letters as a part of the height that the font gives them. A row of a caption is the font size times this part high.
  • box_color: Color of the box, as "#RRGGBB".
  • box_side_padding: Room in pixels between the text, with no outline, and the left and the right edge of the box.
  • box_top_padding: Room in pixels between the top of the rows of the text and the top edge of the box.
  • box_bottom_padding: Room in pixels between the bottom of the rows of the text and the bottom edge of the box.
  • corner_radius: The radius of the corners of the box in pixels. 0 gives square corners.
  • pop_scale: The size of a caption at its start, as a part of its full size.
  • pop_seconds: The time in which a caption grows to its full size. 0 gives the full size from the start.
  • 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.

IndigoSettings.box_style_line

def box_style_line(self, margin_vertical: int) -> str

Write the ASS style line of the box of these settings.

Args:

  • margin_vertical: Room in pixels between the bottom of the caption and the bottom edge of the video.

Returns:

The style line of the box of the captions.

Raises:

  • ValueError: When the font name, the font size, the box color or a margin is outside its range.

IndigoEffect

class IndigoEffect
    def __init__(
            self, settings: IndigoSettings,
            caption_measure: PlaceMeasure) -> None

Builds the ASS script of narrow light capitals on an indigo box.

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 runs ffmpeg once for all the captions of a video.

IndigoEffect.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 is a box event and a text event with the same times. The boxes come first. An event starts with the first word of its caption. It ends 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.