Python: captionbob.effects
captionbob.effects.color_shift_emoji
Color shift emoji effect: the lines of a caption take a color in turn, and an emoji slides above the caption.
A caption shows a few words, in lines of a few words each. All its words show together from the time at which the first word is spoken. The time of a caption is cut into even parts, one part for each line. In the part of a line, that line has the primary color and the other lines have the secondary color, so the primary color moves down the caption from one line to the next. A caption of one line has the primary color for its whole time.
One emoji image stands above the caption, with a gap between the two. Its width is a part of the width of the widest line of the caption. It slides across in a straight line for the time of the caption: its left edge starts at a part of the video width, and its right edge ends at another part. An animated emoji plays from its first frame at the start of each caption.
The text is an ASS script, which the ffmpeg filter ass draws. ASS cannot draw an image, so the effect also gives one emoji layer for each caption, and the ffmpeg filter overlay draws it under the text. The emoji image is not a setting of the look: build_layers takes it with the words, and build_ass_text needs no image. The width of each line, and the breaks of a line that is too wide for the room between the side margins, come from the caption measure. The common part of the effects is in effect_common.py.
Source file: captionbob/effects/color_shift_emoji.py
Settings
The command line and the HTTP API call this effect the style color_shift_emoji. Its settings class is ColorShiftEmojiSettings. Each setting has the default below.
| Setting | Type | Default | Description |
|---|---|---|---|
most_words |
integer | 6 |
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 | "#FF1911" |
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. |
words_per_line |
integer | 3 |
The words of one line of a caption. |
secondary_color |
string | "#FFE434" |
The color of each line of a caption outside its part of the time, as "#RRGGBB". In its part a line has the primary color. |
emoji_width_part |
number | 0.2 |
The width of the emoji as a part of the width of the widest line of the caption, above 0. |
emoji_gap |
number | 10.0 |
Room in pixels between the emoji and the top of the caption. |
emoji_start_left |
number | 0.3 |
The left edge of the emoji at the start of a caption, as a part of the video width, from 0 to 1. |
emoji_end_right |
number | 0.7 |
The right edge of the emoji at the end of a caption, as a part of the video width, from 0 to 1. |
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. |
SlidingEmojiLayer
class SlidingEmojiLayer(NamedTuple)
The emoji above one caption, as the ffmpeg filters draw it.
The emoji is a square of one size. It slides in a straight line for the time of its caption, at one height. An edge of the emoji is a line between two columns or two rows of pixels.
Args:
image_path: The emoji image. An animated image plays from its first frame at the start of the caption.start_seconds: Time at which the caption starts.end_seconds: Time at which the caption ends.full_size: The side of the emoji in pixels.start_left_edge: The left edge of the emoji at the start of the caption: the emoji fills this column and the columns right of it.end_right_edge: The right edge of the emoji at the end of the caption: the emoji fills the columns left of this column.bottom_edge: The bottom edge of the emoji: the emoji fills the rows above this row. It is the emoji gap above the top of the caption.
SlidingEmojiLayer.size_expression
def size_expression(self) -> str
Write the side of the emoji as ffmpeg reads it.
Returns:
An ffmpeg expression of a whole number of pixels, 1 or above.
SlidingEmojiLayer.left_expression
def left_expression(self) -> str
Write the left column of the emoji as the overlay filter reads it.
Returns:
An ffmpeg expression of the time t and the width w of the emoji frame.
SlidingEmojiLayer.top_expression
def top_expression(self) -> str
Write the top row of the emoji as the overlay filter reads it.
Returns:
An ffmpeg expression of the height h of the emoji frame.
ColorShiftEmojiLayers
class ColorShiftEmojiLayers(NamedTuple)
What the effect gives for the words of one video.
Args:
ass_text: The text of the ASS script.emojis: One emoji layer for each caption, in time order.
ColorShiftEmojiSettings
@dataclass(frozen=True)
class ColorShiftEmojiSettings(CaptionSettings)
The look and the timing of the color shift emoji effect.
The settings of CaptionSettings come first: the word counts, the font, the colors, the stroke, the bottom position and the side margin. This effect shows up to 6 words, and its primary color is red. These settings follow them.
Args:
words_per_line: The words of one line of a caption.secondary_color: The color of each line of a caption outside its part of the time, as "#RRGGBB". In its part a line has the primary color.emoji_width_part: The width of the emoji as a part of the width of the widest line of the caption, above 0.emoji_gap: Room in pixels between the emoji and the top of the caption.emoji_start_left: The left edge of the emoji at the start of a caption, as a part of the video width, from 0 to 1.emoji_end_right: The right edge of the emoji at the end of a caption, as a part of the video width, from 0 to 1.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 or the words per line 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.
ColorShiftEmojiEffect
class ColorShiftEmojiEffect
def __init__(
self, settings: ColorShiftEmojiSettings,
caption_measure: CaptionMeasure) -> None
Builds the ASS script and the emoji layers of the color shift emoji effect.
Args:
settings: The look and the timing of the effect.caption_measure: The measure that says how wide libass draws each line of a caption. It must use the ffmpeg that draws the video.
ColorShiftEmojiEffect.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.
The script is the text of the effect with no emoji. It is the script that build_layers gives.
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.
ColorShiftEmojiEffect.build_layers
def build_layers(
self, words: Sequence[Word], video: VideoInfo,
emoji_path: Path) -> ColorShiftEmojiLayers
Build the ASS script and the emoji layers for one video.
Each caption is one event of the script, with its lines in the bottom middle of the video. An event starts with its first word, and all its words show from that time. 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.emoji_path: The emoji image, such as an animated GIF or a PNG with a see-through background. The effect does not read the image. ffmpeg reads it when it draws the layers.
Returns:
The script and one emoji layer for each caption that has time on the screen.
Raises:
TypeError: When the emoji path is not a Path.FileNotFoundError: When the emoji image is not a file.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.