pythonmillisecondssubtitle

How to get a valid milliseconds using Python


I have a subtitle of a two-digit milliseconds, how do I get a valid three-digit milliseconds?

example before

1
00:00:06,10 --> 00:00:08,05.
Let go, let go.

2
00:00:24,21 --> 00:00:29,24.
This is Dateline: Springfield.

3
00:00:30,08 --> 00:00:32,18.
Homer and Marge were

4
00:00:40,03 --> 00:00:45,08.
high school sweethearts.

5
00:00:45,13 --> 00:00:49,05.
She saved me from a dateless prom.

after , using Subtitle Edit It gives you a valid milliseconds

1
00:00:06,417 --> 00:00:08,208
Let go, let go.

2
00:00:24,875 --> 00:00:29,999
This is Dateline: Springfield.

3
00:00:30,333 --> 00:00:32,750
Homer and Marge were

4
00:00:40,125 --> 00:00:45,333
high school sweethearts.

5
00:00:45,542 --> 00:00:49,208
She saved me from a dateless prom.

I used a script pysubs2 https://pypi.org/project/pysubs2/

import pysubs2
subs = pysubs2.load("my_subtitles.srt", encoding="utf-8")
subs.shift(s=0.0)
for line in subs:
    line.text = "{\\be1}" + line.text
subs.save("Output_subtitles.srt")

Only supplies a zero at the end of the timestamp

00:00:06,10 --> 00:00:08,05 to 00:00:06,100 --> 00:00:08,050

Note I know code provides seconds and milliseconds But all I want is to calculate the duration milliseconds
like use Subtitle Edit


Solution

  • What you want can be done with regular expression and re.sub from the standard python library.

    import math
    import re
    
    
    subtitles = """
    1
    00:00:06,10 --> 00:00:08,05.
    Let go, let go.
    
    2
    00:00:24,21 --> 00:00:29,24.
    This is Dateline: Springfield.
    
    3
    00:00:30,08 --> 00:00:32,18.
    Homer and Marge were
    
    4
    00:00:40,03 --> 00:00:45,08.
    high school sweethearts.
    
    5
    00:00:45,13 --> 00:00:49,05.
    She saved me from a dateless prom.
    """
    
    
    def modify_sub_match(match):
        fps = 24  # set the value for fps, adjsut for values different from 24!
    
        # calculate miliseconds based on fps
        miliseconds = math.floor(1000 * int(match.group(2)) / fps) - 1
    
        # return the match group 1, i.e: xx:xx:xx, concatenate the calculated miliseconds
        # and format with a leading zeros, in case the value is less than 100
        return f"{match.group(1)}{miliseconds:03d}"
    
    
    # (\d{2}:\d{2}:\d{2},)(\d{2}) is a regular expresion that matches xx:xx:xx,xx
    modified_subtitles = re.sub(r"(\d{2}:\d{2}:\d{2},)(\d{2})", modify_sub_match, subtitles)
    
    # print the value of `modified_subtitles`
    print(modified_subtitles)
    
    # save `modified_subtitles` to output_subtitles.srt
    with open("output_subtitles.srt", "w") as file:
        file.write(modified_subtitles)