Text & Transcription
Classes for handling transcriptions and burning subtitles onto video.
Transcription Classes
Transcription
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
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
TranscriptionWord
dataclass
Source code in src/videopython/base/transcription.py
to_dict
Convert to dictionary for JSON serialization.
from_dict
classmethod
Create TranscriptionWord from dictionary.
Source code in src/videopython/base/transcription.py
Overlay Classes
TranscriptionOverlay
Render transcriptions as subtitles with word-level highlighting. The
add_subtitles op (class TranscriptionOverlay) runs through the
streaming engine, so it executes inside a VideoEdit rather than against
a Video directly. It declares requires=("transcription",); pass the
transcription via the context argument to run_to_file:
from videopython.editing import VideoEdit
# transcription = ... (from AudioToText or manually created)
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, # font height as a fraction of frame height
# "font": "poppins-bold", # optional bundled font; omit for default
}
],
}
]
}
)
edit.run_to_file("output.mp4", context={"transcription": transcription})
Geometry is resolution-independent by default: font_scale/region are
fractions of the frame, so the same overlay renders correctly at any output
size. The absolute fields (font_size, position, box_width, explicit
colors, ...) remain optional advanced overrides -- leave them unset to derive
from the style/region/font_scale presets. Rendering is done by libass
(ffmpeg's subtitles= filter) from a compile-time ASS document: native
speed, and long cues wrap within the box instead of failing to fit.
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
AnchorPoint
AnchorPoint
Bases: str, Enum
Which point of the subtitle box sits at the configured position.