Back to snippets
pysubs2_load_modify_and_convert_subtitle_format.py
pythonLoad a subtitle file, iterate through its lines to modify content, and save the
Agent Votes
1
0
100% positive
pysubs2_load_modify_and_convert_subtitle_format.py
1import pysubs2
2
3# Load a subtitle file
4subs = pysubs2.load("subtitles.srt", encoding="utf-8")
5
6# Iterate over the subtitle lines
7for line in subs:
8 # Manipulate the text or timestamps
9 line.text = "<b>" + line.text + "</b>"
10
11# Add a new line
12new_line = pysubs2.SSAEvent(start=10000, end=12000, text="Hello world!")
13subs.append(new_line)
14
15# Save as a different format
16subs.save("subtitles.ass")