Usage
WebVTT files with Byte Order Mark (BOM)
When the WebVTT file has BOM, saving it will keep the BOM:
import webvtt
vtt = webvtt.read('captions_with_bom.vtt')
# saved file keeps the BOM
vtt.save()
Add a BOM to a file without it:
import webvtt
vtt = webvtt.read('captions_without_bom.vtt',
add_bom=True
)
# saved file has BOM
vtt.save()
Remove the BOM from a file:
import webvtt
vtt = webvtt.read('captions_with_bom.vtt')
# saved file does not have BOM
vtt.save(add_bom=False)
Save file with a different encoding:
import webvtt
vtt = webvtt.read('captions.vtt')
vtt.save(encoding='utf-32-le')
# save in different encoding with BOM
vtt.save(encoding='utf-32-le',
add_bom=True
)
WebVTT Styles
import webvtt
vtt = webvtt.read('captions.vtt')
for style in vtt.styles:
print(style.text)
# retrieve list of lines
print(style.lines)
Adding styles:
import webvtt
vtt = webvtt.read('captions.vtt')
vtt.styles.append(
webvtt.Style('::cue(b) {\n color: peachpuff;\n}')
)
# list of lines is supported
vtt.styles.append(
webvtt.Style(['::cue(b) {',
' color: peachpuff;',
'}'
])
)
Updating styles:
import webvtt
vtt = webvtt.read('captions.vtt')
vtt.styles[0].lines[1] = ' color: papayawhip;'
WebVTT Comments
Comments can be added or retrieved from different items:
import webvtt
vtt = webvtt.read('captions.vtt')
# comments from the top of the file
print(vtt.header_comments)
# comments from the bottom of the file
print(vtt.footer_comments)
# comments in a style
print(vtt.styles[0].comments)
# comments in a caption
print(vtt.captions[0].comments)
# comments are just a list of strings
vtt.captions[5].comments.append('caption for review')