m-lyon / filter-subs

Filter Subtitles to remove SDH entries
MIT License
38 stars 10 forks source link

Add New Line #12

Closed sweetngx closed 2 years ago

sweetngx commented 2 years ago

Well, while doing all this with this great script, I want to make my own advertisement in the first line of the subtitle, can you help me?

m-lyon commented 2 years ago

Yeah so that is pretty doable with the module.

from subtitle_filter.libs.subtitle import Subtitles, Subtitle

# Read in your subtitle file
subs = Subtitles('/my/subtitle/file.srt')

# Optionally apply the subtitle filter
subs.filter()

# Create your custom subtitle line with the Subtitle class
new_sub = Subtitle()
# Set the contents of the subtitle
new_sub.contents = 'this is my \n awesome subtitle' # use "\n" for multiline subtitle entries.
# Set the timestamp to whatever time you want it to appear
new_sub.start = '00:01:40,723'
new_sub.end = '00:01:42,707'

# The Subtitles class keeps all the Subtitle instances in a list referenced by Subtitles.subtitles
# Prepend this list with your newly created subtitle
subs.subtitles.insert(0, new_sub)
# Reindex the subtitles
for idx, sub in enumerate(self.subtitles):
    sub.index = idx + 1

# Save your new subtitle file
subs.save('/my/awesome/new/subtitle.srt')