Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add reindex command #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pysrt/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down