byroot / pysrt

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

remove_overlaps and to_string #70

Closed doakey3 closed 7 years ago

doakey3 commented 7 years ago

I've created 2 new functions. 1 for removing the overlap between subtitles and another for getting the srt format as a string (as opposed to saving to a file).

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.2%) to 71.128% when pulling abdfe9f644b9edb0ceae2571bc785e29ce00b0ba on doakey3:master into 529d83e5222522c2ecefea6442bcd11b33158673 on byroot:master.

doakey3 commented 7 years ago

In my remove_overlaps function, I made it automatically call clean_indexes() if a subtitle section gets entirely removed.

I noticed in one of your comments you said that this could cause a problem for certain types of srt files. If this is the case, then I should alter my function to not automatically clean the indexes. EDIT: Done.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+1.1%) to 72.414% when pulling 21b9a6c32b3ef3a8a3c4c8060ec41ba28d81da00 on doakey3:master into 529d83e5222522c2ecefea6442bcd11b33158673 on byroot:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+1.1%) to 72.414% when pulling 81b49bd1926f1560091598f8d68e71ac5a62df4e on doakey3:master into 529d83e5222522c2ecefea6442bcd11b33158673 on byroot:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+1.1%) to 72.414% when pulling f282b20ba4612aa8d741ef9d12aa0cb556188d35 on doakey3:master into 529d83e5222522c2ecefea6442bcd11b33158673 on byroot:master.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+1.1%) to 72.414% when pulling eb37d91293612915b6aa76ec6fb95fdf96a3e7b2 on doakey3:master into 529d83e5222522c2ecefea6442bcd11b33158673 on byroot:master.

byroot commented 7 years ago

Sorry but I don't think remove_overlap belongs to pysrt. It's a very opinionated method that could legitimately behave in multiple different ways. I prefer to let pysrt users implement that themselves.

As for to_string, I'm pretty sure ''.join(subtitles) should works.

nipunasudha commented 3 years ago

Although this PR didn't make it into the main repo, this helped me a lot with my project. Thank you very much @doakey3!

@byroot you are right to reject this PR, even for me this was not the overlap removal method I needed. This changes start times of subtitles, but what actually made sense is to shorten lifetime of subtitles by changing the end time.

ghost commented 2 years ago

As for to_string, I'm pretty sure ''.join(subtitles) should works.

That doesn't seem to be the case.

>>> subtitles = pysrt.from_string('1\n00:00:00,000 --> 00:00:05,000\nTest')
>>> ''.join(subtitles)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, SubRipItem found

This works though:

>>> subtitles = pysrt.from_string('1\n00:00:00,000 --> 00:00:05,000\nTest\n\n2\n00:00:10,000 --> 00:00:15,000\nTest 2')
>>> '\n'.join(map(str, subtitles))
'1\n00:00:00,000 --> 00:00:05,000\nTest\n\n2\n00:00:10,000 --> 00:00:15,000\nTest 2\n'