byroot / pysrt

Python parser for SubRip (srt) files
GNU General Public License v3.0
446 stars 67 forks source link

Deleting a subtitles line should also trigger a renumber #57

Open Andbarz opened 9 years ago

Andbarz commented 9 years ago

I'm using your library to build a simple scripts which downloads subtitles from opensubtitles.org, removes all the unnecessary lines(synced by... and similar entries), saves the srt file and then encodes it into the video file with ffmpeg. I've found that ffmpeg doesn't like srt files where there's a missing line, and it refuses to work with them.

This is what happens now:

1
00:00:22,712 --> 00:00:24,478
first line

2
00:00:25,000 --> 00:00:31,074
second line

3
00:00:57,413 --> 00:01:00,180
third line

when i run

del sub[1]

I get

1
00:00:22,712 --> 00:00:24,478
first line

3
00:00:57,413 --> 00:01:00,180
third line

Which is not valid according to ffmpeg. Instead I should get

1
00:00:22,712 --> 00:00:24,478
first line

2
00:00:57,413 --> 00:01:00,180
third line

Can you please make that when I close an srt file, pysrt automatically renumbers all the lines, so that there are no interruptions?

byroot commented 9 years ago

I have to think about it. On one hand it would make perfect sense to do it automatically, but in the other end some subtitle have sometimes some kind of tags as indexes, so it would lose data.

I guess it could do it by default, and you could prevent it with close(clean_indexes=False).

That would require a minor version bump though.

byroot commented 9 years ago

By the way until I issue a new version you can simply call clean_indexes() yourself: https://github.com/byroot/pysrt/blob/47aaa592c3bc185cd2bc1d58d1451bf98be3c1ef/pysrt/srtfile.py#L128.

Andbarz commented 9 years ago

Sorry, I didn't find that function. That was what I was really interested in, not automating it on close(). If you haven't time, just let things as they are now.