Transcription and subtitles¶
Transcription data classes (videopython.base) and the subtitle-burning operation
(videopython.editing). Producing a transcription is
AudioToText; a worked example is
Tutorial 2.
Data classes¶
Transcription
¶
Source code in src/videopython/base/transcription.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | |
__init__
¶
__init__(
segments: list[TranscriptionSegment] | None = None,
words: list[TranscriptionWord] | None = None,
language: str | None = None,
)
Initialize Transcription from either segments or words.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segments
|
list[TranscriptionSegment] | None
|
Pre-constructed segments (backward compatible) |
None
|
words
|
list[TranscriptionWord] | None
|
Words to group into segments by speaker (for diarization) |
None
|
language
|
str | None
|
ISO 639-1 language code detected during transcription (e.g. "en", "pl") |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If both or neither arguments are provided |
Source code in src/videopython/base/transcription.py
speaker_stats
¶
Calculate speaking time percentage for each speaker.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary mapping speaker names to their percentage of total speaking time |
Source code in src/videopython/base/transcription.py
offset
¶
Return a new Transcription with all timings offset by the provided time value.
Source code in src/videopython/base/transcription.py
standardize_segments
¶
standardize_segments(
*,
time: float | None = None,
num_words: int | None = None,
) -> Transcription
Return a new Transcription with standardized segments.
Segments are also split on speaker changes so that each segment contains words from a single speaker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
float | None
|
Maximum duration in seconds for each segment |
None
|
num_words
|
int | None
|
Maximum number of words per segment |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If both time and num_words are provided or if neither is provided |
Source code in src/videopython/base/transcription.py
capitalize_sentences
¶
Return a new Transcription with sentence-start capitalization.
The first letter of the first spoken word and of every word that
follows sentence-ending punctuation (., !, ?, …) is
upper-cased. Remaining characters are left untouched, so acronyms and
proper nouns from the source transcription are preserved. Timing,
speaker, and language are carried through unchanged.
Abbreviation detection is intentionally not attempted: a token like
"U.S." is treated as a sentence end. This heuristic is adequate
for burned-in subtitles and avoids a brittle abbreviation list.
Source code in src/videopython/base/transcription.py
chunk_segments
¶
Return a new Transcription splitting each segment into smaller cues.
Each segment is split into consecutive groups of at most max_words
words, using that group's own first/last word timings. Unlike
:meth:standardize_segments, words are never merged across the
original segments, so silence gaps between segments are preserved and
subtitles do not linger over pauses. Speaker, confidence, and language
metadata are carried through unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_words
|
int
|
Maximum number of words per output segment. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/videopython/base/transcription.py
slice
¶
Return a new Transcription containing only words within the time range.
Slices at word-level granularity: words that overlap with the time range are included, and new segments are reconstructed from the included words.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
float
|
Start time in seconds (inclusive) |
required |
end
|
float
|
End time in seconds (exclusive) |
required |
Returns:
| Type | Description |
|---|---|
Transcription | None
|
New Transcription with words/segments in the time range, or None if no words overlap |
Source code in src/videopython/base/transcription.py
to_srt
¶
Export transcription as an SRT subtitle string.
Source code in src/videopython/base/transcription.py
from_srt
classmethod
¶
Parse an SRT string into a Transcription.
Each SRT block becomes a segment with a single word spanning the full segment duration (word-level timing is not available in SRT).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
srt
|
str
|
SRT-formatted string. |
required |
Returns:
| Type | Description |
|---|---|
Transcription
|
Transcription with one segment per SRT block. |
Source code in src/videopython/base/transcription.py
save_srt
¶
Write transcription to an SRT file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Output file path. |
required |
to_dict
¶
from_dict
classmethod
¶
Create Transcription from dictionary.
Source code in src/videopython/base/transcription.py
TranscriptionSegment
dataclass
¶
Source code in src/videopython/base/transcription.py
to_dict
¶
Convert to dictionary for JSON serialization.
Source code in src/videopython/base/transcription.py
from_dict
classmethod
¶
Create TranscriptionSegment from dictionary.
Source code in src/videopython/base/transcription.py
from_words
classmethod
¶
from_words(
words: list[TranscriptionWord],
*,
speaker: str | None = None,
avg_logprob: float | None = None,
no_speech_prob: float | None = None,
compression_ratio: float | None = None,
) -> TranscriptionSegment
Build a segment spanning words, deriving start/end/text from them.
words must be non-empty: start/end come from the first/last
word and text is the words joined by single spaces. Speaker and the
confidence fields are passed through so callers re-segmenting within a
known source segment can preserve them; callers regrouping words across
segments (where these are ambiguous) simply omit them, leaving None.
The words list is copied, so the result never aliases the caller's.
Source code in src/videopython/base/transcription.py
TranscriptionWord
dataclass
¶
Source code in src/videopython/base/transcription.py
TranscriptionOverlay¶
The add_subtitles operation. It declares requires=("transcription",), so the
transcription is supplied through run_to_file(context=...) rather than a constructor
argument, and the runner re-bases it onto each segment's local timeline. Rendering goes
through libass (FFmpeg's subtitles= filter) from an ASS document compiled at plan time.
from videopython.editing import VideoEdit
edit = VideoEdit.from_dict({
"segments": [{
"source": "input.mp4",
"start": 0.0,
"end": 5.0,
"operations": [{
"op": "add_subtitles",
"style": "boxed", # boxed | outline | clean | karaoke
"region": "bottom", # top | center | bottom
"font_scale": 0.055, # fraction of frame height
# "font": "poppins-bold",
}],
}]
})
edit.run_to_file("output.mp4", context={"transcription": transcription})
The recommended surface¶
| Field | Meaning |
|---|---|
style |
A named look bundling text/highlight colors, border and background. boxed reproduces the historical defaults. |
region |
Which vertical safe-area band the box sits in: top, center, bottom. |
font_scale |
Base font height as a fraction of frame height, so one plan renders correctly at 480p and 4K. Long cues wrap inside the box. |
font |
A bundled font by name: anton, bebas-neue, lato-bold, poppins-bold (full list: videopython.base.fonts.FONT_NAMES). None uses the bundled default. |
These are all relative or enumerated, which is why a stored plan round-trips and an LLM can fill them in — see LLM-first design.
Advanced overrides
The absolute fields (font_size, text_color, highlight_color,
background_color, background_padding, position, anchor, box_width,
font_border_size, highlight_size_multiplier, max_words_per_cue, capitalize)
remain available as optional overrides. Leave them unset to derive everything from
style/region/font_scale, or set one to pin it.
Prefer not setting font_size: an absolute size chosen without knowing the final
post-transform frame is exactly what overflows at render time. With the relative
surface, VideoEdit.validate() rejects an un-fittable plan up front instead of
crashing mid-render.
font_filename is a raw TrueType path; it takes precedence over font and is
llm_hidden, so it never appears in an LLM-facing schema.
TranscriptionOverlay
¶
Bases: Effect
Renders animated word-by-word subtitles with the current word highlighted.
Each word lights up in the highlight color (enlarged by the size
multiplier) as it is spoken, based on transcription timestamps. Requires a
word-level transcription, which the runner supplies via the
requires=("transcription",) declaration -- re-based onto the segment's
local timeline and delivered at plan-compile time through
:class:FilterCtx; the op compiles to a libass subtitles= filter
(:attr:compiles_to_filter), so subtitled edits run on the O(1)-memory
streaming path at native speed.
Source code in src/videopython/editing/transcription_overlay.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
to_ffmpeg_filter
¶
Compile to a libass subtitles= filter entry.
Consumes the segment-local transcription from ctx.context at plan
compile time: writes a temp .ass (registered on ctx.owned_files
for the runner to delete after streaming) and emits one -vf entry.
A missing transcription raises the op's clear context error here --
before any decode.
Source code in src/videopython/editing/transcription_overlay.py
SubtitleStyle
¶
Bases: str, Enum
Named look bundling colors / border / background / highlight.
Lets a caller express intent ("boxed", "outline", ...) instead of a dozen individual numbers.
Source code in src/videopython/editing/transcription_overlay.py
SubtitleRegion
¶
AnchorPoint
¶
Bases: str, Enum
Which point of the subtitle box sits at the configured position.