captionbob docs

Version 1.0.0
Contents ↓

Python: captionbob.effects

captionbob.effects.pebble

Pebble effect: gray words on a dark box that light up when spoken.

A caption shows up to four words on one line, on a dark box with round corners. The caption shows all its words from its start, in the unspoken color. When a word is spoken, it changes from the unspoken color to the primary color in the light up time, and it keeps the primary color until the caption goes. The caption comes and goes with no fade.

A caption is two events of the script at one place: the box on the lower layer and the text on the layer above it. libass draws no box with round corners behind a text, so the box is a drawing. The caption measure gives the columns of the text, and the box is as wide as the text plus the side padding on each side. It is as high as the rows of the text, each row as high as the font size, plus the vertical padding above and below. 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, with the box behind a caption.

The defaults are the look of the source video of the effect, measured on its frames and scaled to a video of 1080x1920.

Source file: captionbob/effects/pebble.py

Settings

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

Setting Type Default Description
most_words integer 4 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 52 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 0.0 Width of the outline in pixels. 0 gives no outline.
bottom_position number 0.651 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.
unspoken_color string "#6E6F73" Color of a word before it is spoken, as "#RRGGBB".
box_color string "#26272A" Color of the box behind the caption, as "#RRGGBB".
box_side_padding number 40.0 Room in pixels between the text and the left and the right edge of the box.
box_vertical_padding number 23.0 Room in pixels between the rows of the text and the top and the bottom edge of the box. A row is as high as the font size.
corner_radius number 17.0 Radius of the round corners of the box in pixels. 0 gives square corners.
light_up_seconds number 0.13 Time in which a spoken word changes from the unspoken color to the primary color. 0 changes it at once.
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.

PebbleSettings

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

The look and the timing of the pebble effect.

The settings of CaptionSettings come first: the word counts, the font, the colors, the stroke, the bottom position and the side margin. The primary color is the color of a word that is spoken. These settings follow them. The box reaches into the side margins by its side padding.

Args:

  • unspoken_color: Color of a word before it is spoken, as "#RRGGBB".
  • box_color: Color of the box behind the caption, as "#RRGGBB".
  • box_side_padding: Room in pixels between the text and the left and the right edge of the box.
  • box_vertical_padding: Room in pixels between the rows of the text and the top and the bottom edge of the box. A row is as high as the font size.
  • corner_radius: Radius of the round corners of the box in pixels. 0 gives square corners.
  • light_up_seconds: Time in which a spoken word changes from the unspoken color to the primary color. 0 changes it at once.
  • 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.

PebbleSettings.box_style_line

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

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

The box is a drawing in the fill color of its style, with no outline. The event of the box gives its place with its tags.

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.

PebbleEffect

class PebbleEffect
    def __init__(
            self, settings: PebbleSettings,
            caption_measure: PlaceMeasure) -> None

Builds the ASS script of gray words on a dark box that light up.

Args:

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

PebbleEffect.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 is two events with the same times: the box on the lower layer and the text on the layer above it. 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.