WebVTT Python Library

0.5.1 · active · verified Sat Apr 11

webvtt-py is a Python library (current version 0.5.1) for reading, writing, converting, and segmenting WebVTT caption files. It is actively maintained with regular releases, often several times a year, addressing new features, bug fixes, and Python compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to read an existing WebVTT file, iterate over its captions, and then create a new WebVTT object programmatically, add captions to it, and save it to a new file. It also includes creating a dummy file for immediate execution.

import webvtt
import os

# Create a dummy VTT file for demonstration
dummy_vtt_content = """
WEBVTT

1
00:00:00.000 --> 00:00:03.000
Hello, world!

2
00:00:04.000 --> 00:00:07.000
This is a test caption.
"""

with open("example.vtt", "w", encoding="utf-8") as f:
    f.write(dummy_vtt_content)

# Read a WebVTT file
vtt = webvtt.read('example.vtt')

print("--- Captions from example.vtt ---")
for caption in vtt:
    print(f"[{caption.start} --> {caption.end}] {caption.text}")

# Create a new WebVTT object and add captions programmatically
new_vtt = webvtt.WebVTT()
new_vtt.add(webvtt.Caption(start='00:00:01.000', end='00:00:05.000', text='First dynamic caption.'))
new_vtt.add(webvtt.Caption(start='00:00:06.000', end='00:00:10.000', text='Second dynamic caption.'))

# Save the new WebVTT object to a file
new_vtt.save('output.vtt')
print("\nGenerated output.vtt with 2 captions.")

# Clean up dummy files
os.remove("example.vtt")
os.remove("output.vtt")
print("Cleaned up example.vtt and output.vtt")

view raw JSON →