cdown / srt

A simple library and set of tools for parsing, modifying, and composing SRT files.
MIT License
473 stars 44 forks source link

How to regenerate the srt file. #97

Closed jp1017 closed 5 months ago

jp1017 commented 5 months ago

I have a requirement:

Here is a srt file.I want to make the sub's endtime equls the next sub's starttime. How to make it.

cdown commented 5 months ago

Sorry, but github issues is not a support forum. You don't need anything srt specific, just loop over the SRT files with an index and do:

sub[i].end = sub[i+1].start

With bounds checks for i.

cdown commented 5 months ago

Completely untested, written directly here:

import srt

with open("input.srt", "r") as file:
    srt_content = file.read()

subtitles = list(srt.parse(srt_content))

for i in range(len(subtitles) - 1):
    subtitles[i].end = subtitles[i + 1].start

modified_srt_content = srt.compose(subtitles)

with open("output.srt", "w") as file:
    file.write(modified_srt_content)