Reading WebVTT caption files

import webvtt

# we can iterate over the captions
for caption in'captions.vtt'):
    print(caption.start)  # start timestamp in text format
    print(caption.end)  # end timestamp in text format
    print(caption.text)  # caption text

# you can also iterate over the lines of a particular caption
for line in vtt[0].lines:

# caption text is returned clean without class tags
# we can access the raw text of a caption with raw_text
>>> vtt[0].text
'This is a caption text'
>>> vtt[0].raw_text
'This is a <c.colorE5E5E5>caption</c> text'

# caption identifiers
>>> vtt[0].identifier
'crédit de transcription'

Reading WebVTT caption files from file-like object

import webvtt
import requests
from io import StringIO

payload = requests.get('').text()
buffer = StringIO(payload)

for caption in webvtt.read_buffer(buffer):

Creating captions

from webvtt import WebVTT, Caption

vtt = WebVTT()

# creating a caption with a list of lines
caption = Caption(
    ['Caption line 1', 'Caption line 2']

# adding a caption

# creating another caption with a text
caption = Caption(
    'Caption line 1\nCaption line 2'


Manipulating captions

import webvtt

vtt ='captions.vtt')

# update start timestamp
vtt[0].start = '00:00:01.250'

# update end timestamp
vtt[0].end = '00:00:03.890'

# update caption text
vtt[0].text = 'My caption text'

# delete a caption
del vtt.captions[2]

Saving captions

import webvtt

vtt ='captions.vtt')

# save to original file

# save to a different file'my_captions.vtt')

# write to opened file
with open('my_captions.vtt', 'w') as fd:

Converting captions

You can read captions from the following formats:

  • SubRip (.srt)
  • YouTube SBV (.sbv)
import webvtt

# to read from a different format use the method from_ followed by
# the extension.
vtt = webvtt.from_sbv('captions.sbv')

# if we just want to convert the file we can do this in one line

Also we can convert WebVTT to other formats:

  • SubRip (.srt)
import webvtt

# save in SRT format
vtt ='captions.vtt')

# write to opened file in SRT format
with open('', 'w') as fd:
    webvtt.write(fd, format='srt)