From 46f15e8102ba9f39591ad6b42271e246b812715b Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Sun, 29 Sep 2024 12:05:36 +0300 Subject: [PATCH] feat: +`reindex` command --- pysrt/commands.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pysrt/commands.py b/pysrt/commands.py index b2bfa24..fe1424f 100755 --- a/pysrt/commands.py +++ b/pysrt/commands.py @@ -87,6 +87,11 @@ class SubRipShifter(object): $ srt split 20m 20m movie.srt => creates movie.1.srt, movie.2.srt and movie.3.srt """) + REINDEX_EPILOG = dedent("""\ + + Examples: + $ srt -i reindex movie.srt + """) FRAME_RATE_HELP = "A frame rate in fps (commonly 23.9 or 25)" ENCODING_HELP = dedent("""\ Change file encoding. Useful for players accepting only latin1 subtitles. @@ -114,6 +119,9 @@ def build_parser(self): type=self.parse_time, help=self.TIMESTAMP_HELP) shift_parser.set_defaults(action=self.shift) + enum_parser = subparsers.add_parser('reindex', help="Sort & re-enumerate subtitles (eg. after merge)", epilog=self.REINDEX_EPILOG, formatter_class=argparse.RawTextHelpFormatter) + enum_parser.set_defaults(action=self.reindex) + rate_parser = subparsers.add_parser('rate', help="Convert subtitles from a frame rate to another", epilog=self.RATE_EPILOG, formatter_class=argparse.RawTextHelpFormatter) rate_parser.add_argument('initial', action='store', type=float, help=self.FRAME_RATE_HELP) rate_parser.add_argument('final', action='store', type=float, help=self.FRAME_RATE_HELP) @@ -161,6 +169,10 @@ def shift(self): self.input_file.shift(milliseconds=self.arguments.time_offset) self.input_file.write_into(self.output_file) + def reindex(self): + self.input_file.clean_indexes() + self.input_file.write_into(self.output_file) + def rate(self): ratio = self.arguments.final / self.arguments.initial self.input_file.shift(ratio=ratio)