如何使用Python获取有效的毫秒数

huangapple go评论84阅读模式
英文:

How to get a valid milliseconds using Python

问题

我明白了,你想将两位数的毫秒转换为三位数的毫秒。你可以使用以下代码:

import pysubs2

subs = pysubs2.load("my_subtitles.srt", encoding="utf-8")
for line in subs:
    line.start = line.start.replace(microsecond=line.start.microsecond * 100)
    line.end = line.end.replace(microsecond=line.end.microsecond * 100)

subs.save("Output_subtitles.srt")

这将把所有的时间戳的毫秒部分转换为三位数。

英文:

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

答案1

得分: 0

以下是已翻译的代码部分:

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  # 设置 fps 的值,根据不同的值进行调整!

    # 根据 fps 计算毫秒
    miliseconds = math.floor(1000 * int(match.group(2)) / fps) - 1

    # 返回匹配组1,即 xx:xx:xx,连接计算出的毫秒并格式化为带有前导零的形式,以防值小于100
    return f"{match.group(1)}{miliseconds:03d}"

# (\d{2}:\d{2}:\d{2},)(\d{2}) 是一个正则表达式,用于匹配 xx:xx:xx,xx
modified_subtitles = re.sub(r"(\d{2}:\d{2}:\d{2},)(\d{2})", modify_sub_match, subtitles)

# 打印 `modified_subtitles` 的值
print(modified_subtitles)

# 将 `modified_subtitles` 保存到 output_subtitles.srt
with open("output_subtitles.srt", "w") as file:
    file.write(modified_subtitles)

希望这对你有帮助!

英文:

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)

huangapple
  • 本文由 发表于 2023年6月9日 03:11:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定