Skip to content

works for me; adding srt-stretch #5

@themaddoctor

Description

@themaddoctor

Thanks. I used it to resync an SRT when I needed a different language. I also needed to stretch the timestamps. With two minor modifications, the tool did that for me, too.

sed -e 's@return time + shift \* 1000@return time * shift@' \
    -e 's@srt-shift filename shift@srt-stretch filename factor@' \
    /usr/bin/srt-shift > /usr/bin/srt-stretch

Here is the full thing:

#!/usr/bin/python

import sys
import re


def main():
    try:
        filename = sys.argv[1]
        shift = float(sys.argv[2])
    except (IndexError, ValueError):
        print("usage: srt-stretch filename factor")
        return

    out = ''

    with open(filename, 'r') as file:
        i = 0
        for line in file:
            line = line.strip()
            if not line:
                out += '\n'
                continue

            i += 1

            if re.compile('^(\d+)$').match(line):
                i = 1

            if i == 1:
                out += '%s\n' % line

            elif i == 2:
                start, end = line.split(' --> ')

                def parse_time(time):
                    hour, minute, second = time.split(':')
                    hour, minute = int(hour), int(minute)
                    second_parts = second.split(',')
                    second = int(second_parts[0])
                    microsecond = int(second_parts[1])

                    return (
                        hour * 60 * 60 * 1000 +
                        minute * 60 * 1000 +
                        second * 1000 +
                        microsecond
                    )

                start, end = map(parse_time, (start, end))

                def shift_time(time):
                    return time * shift

                start, end = map(shift_time, (start, end))

                def get_time(time):
                    return (
                        time // (60 * 60 * 1000),
                        (time % (60 * 60 * 1000)) // (60 * 1000),
                        (time % (60 * 1000)) // 1000,
                        time % 1000,
                    )

                def str_time(time):
                    return '%02d:%02d:%02d,%03d' % get_time(time)

                out += '%s --> %s\n' % (
                    str_time(start),
                    str_time(end),
                )

            elif i >= 3:
                out += '%s\n' % line

    print(out)

if __name__ == '__main__':
    main()

# vim: expandtab tabstop=4 shiftwidth=4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions