Module Documentation¶
Apologies for the sometimes poor formatting of text, it is machine generated by mkdocstrings.
Cheers
Doug Scoular
Transcribe video files to SRT subtitle text files using a pre-trained model.
Author: Doug Scoular
Date: 2025-09-16
Email: dscoular@gmail.com
License: MIT
The main class that does all the work is Transcribe it employs an instance of the FileFilter class to find the video files it is going to process and turn into SRT subtitle text files.
Requirements: (see pyproject.toml for versions):
- whisper (openai/whisper)
- pysrt
- numpy
- AudioSegment (pydub)
- ffmpeg (for audio decoding, must be installed separately into the Operating System)
FileFilter
¶
A class which recursively searches the given input_path filtering files it finds based on a matching filename "suffix" e.g. .mp4 combined with "include" and "exclude" rglob patterns.
Examples:
>>> # Instantiating our FileFilter instance and obtaining matching files.
>>> filter = FileFilter(Path('.'),
... '.mp4',
... include_patterns=['**/*.mp4'],
... exclude_patterns=['**/exclude_this.mp4'])
>>> matching_files = filter.get_matching_files()
>>> for file in matching_files:
>>> print(file)
foo.mp4
bar.mp4
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
Path
|
The root directory to scan for files. |
required |
suffix
|
Optional[str]
|
The file suffix to filter by (defaults to '.mp4'). |
'.mp4'
|
include_patterns
|
Optional[list[str]]
|
List of glob patterns to include. |
None
|
exclude_patterns
|
Optional[list[str]]
|
List of glob patterns to exclude. |
None
|
Source code in src/transcriber/transcribe.py
get_matching_files()
¶
Recursively Scans the self.input_path directory using rglob patterns and returns a list of all files that match our FileFilter instance's criteria (suffix, include_patterns and exclude_patterns).
Returns:
| Type | Description |
|---|---|
list[Path]
|
A sorted list of Path objects matching the filter criteria. |
Source code in src/transcriber/transcribe.py
Transcriber
¶
A class to handle transcription of video files to SRT subtitle text files using an OpenAI/Whisper pre-trained model. Takes our parsed command-line arguments to instantiate an instance which we can then use to transcribe videos to text.
Examples:
>>> # Manually create our arguments namespace.
>>> my_args = argparse.Namespace(input_path='/tmp/Bonsai_Tutorials')
>>> my_args.model = 'base.en' # Choose the smallest transcription model.
>>> my_args.force = True # Force overwriting existing ".srt" files.
>>> my_args.suffix = '.mp4' # Only consider ".mp4" files.
>>> # Include "rglob" patterns we are interested in.
>>> my_args.include = ['**/001000_20250218_1337 - moving objects and setting a few preferences.mp4']
>>> # Exclude "rglob" patterns we don't want to process.
>>> my_args.excluded = ['**/skip_this.mp4']
>>> my_args.dry_run = False # Actually process the files.
>>> my_args.interactive = False # Don't interactively prompt the user.
>>> # Instantiate our Transcriber instance with our arguments.
>>> transcriber = Transcriber(my_args)
>>> # Start the transcription process.
>>> transcriber.videos_to_text()
We matched 1 files.
PROCESSING: /tmp/Bonsai_Tutorials/001000_20250218_1337 - moving objects and setting a few preferences/001000_20250218_1337 - moving objects and setting a few preferences.mp4 -> /tmp/Bonsai_Tutorials/001000_20250218_1337 - moving objects and setting a few preferences/001000_20250218_1337 - moving objects and setting a few preferences.srt...
SUCCESS: Transcription saved to [/tmp/Bonsai_Tutorials/001000_20250218_1337 - moving objects and setting a few preferences/001000_20250218_1337 - moving objects and setting a few preferences.srt]
Transcription completed for all files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
argparse.Namespace
|
Parsed command-line arguments. |
required |
Source code in src/transcriber/transcribe.py
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 | |
transcribe(input_file)
¶
Transcribe the audio from the given video input file and returns a dictionary of transcribed text and other relevant metadata. We return None if the transcription fails.
Examples:
>>> # Transcribe our video to our dictionary of subtitle metadata.
>>> srt_metadata = transcriber.transcribe("/path/to/video.mp4")
>>> pp srt_metadata
{
'language': 'en',
'segments': [{'avg_logprob': -0.18892038023317015,
'compression_ratio': 1.5515463917525774,
'end': 6.28,
'id': 0,
'no_speech_prob': 0.09762045741081238,
'seek': 0,
'start': 0.0,
'temperature': 0.0,
'text': ' Welcome to Vanilla Blender. I figured before we get into Bonsai we can go'
...
}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_file
|
Path
|
The root directory to scan for files. |
required |
Returns: A dictionary with a dictionary of transcription results, or None on failure.
Source code in src/transcriber/transcribe.py
videos_to_text()
¶
Convert video files in the input path to audio and transcribe them to SRT text files based on the arguments given when we instantiated our Transcriber class.
Source code in src/transcriber/transcribe.py
main(args=None)
¶
Main function to run the transcriber.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Optional[list[str]]
|
List of command-line arguments to parse. |
None
|
Source code in src/transcriber/transcribe.py
parse_and_prompt_arguments(args=None)
¶
Parse command-line arguments and prompt for a subset of missing ones if in interactive mode. We don't bother prompting for "include" or "exclude" arguments since we would encourage you to learn to use the command-line arguments for more advance usage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
list[str] | None
|
List of command-line arguments to parse. |
None
|
Returns:
| Type | Description |
|---|---|
argparse.Namespace
|
The parsed command-line arguments. |
Raises:
| Type | Description |
|---|---|
SystemExit
|
If version is requested or invalid input is provided. |
Source code in src/transcriber/transcribe.py
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 | |
validate_dot_suffix(value)
¶
A custom argparse type that ensures the value is a string starting with a dot '.'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The input string to validate. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The validated suffix string. |
Raises:
| Type | Description |
|---|---|
argparse.ArgumentTypeError
|
If the value is invalid. |