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

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

How to get a valid milliseconds using Python

问题

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

  1. import pysubs2
  2. subs = pysubs2.load("my_subtitles.srt", encoding="utf-8")
  3. for line in subs:
  4. line.start = line.start.replace(microsecond=line.start.microsecond * 100)
  5. line.end = line.end.replace(microsecond=line.end.microsecond * 100)
  6. 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. 1
  2. 00:00:06,10 --> 00:00:08,05.
  3. Let go, let go.
  4. 2
  5. 00:00:24,21 --> 00:00:29,24.
  6. This is Dateline: Springfield.
  7. 3
  8. 00:00:30,08 --> 00:00:32,18.
  9. Homer and Marge were
  10. 4
  11. 00:00:40,03 --> 00:00:45,08.
  12. high school sweethearts.
  13. 5
  14. 00:00:45,13 --> 00:00:49,05.
  15. She saved me from a dateless prom.

after , using Subtitle Edit It gives you a valid milliseconds

  1. 1
  2. 00:00:06,417 --> 00:00:08,208
  3. Let go, let go.
  4. 2
  5. 00:00:24,875 --> 00:00:29,999
  6. This is Dateline: Springfield.
  7. 3
  8. 00:00:30,333 --> 00:00:32,750
  9. Homer and Marge were
  10. 4
  11. 00:00:40,125 --> 00:00:45,333
  12. high school sweethearts.
  13. 5
  14. 00:00:45,542 --> 00:00:49,208
  15. She saved me from a dateless prom.

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

  1. import pysubs2
  2. subs = pysubs2.load("my_subtitles.srt", encoding="utf-8")
  3. subs.shift(s=0.0)
  4. for line in subs:
  5. line.text = "{\\be1}" + line.text
  6. 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

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

  1. import math
  2. import re
  3. subtitles = """
  4. 1
  5. 00:00:06,10 --> 00:00:08,05.
  6. Let go, let go.
  7. 2
  8. 00:00:24,21 --> 00:00:29,24.
  9. This is Dateline: Springfield.
  10. 3
  11. 00:00:30,08 --> 00:00:32,18.
  12. Homer and Marge were
  13. 4
  14. 00:00:40,03 --> 00:00:45,08.
  15. high school sweethearts.
  16. 5
  17. 00:00:45,13 --> 00:00:49,05.
  18. She saved me from a dateless prom.
  19. """
  20. def modify_sub_match(match):
  21. fps = 24 # 设置 fps 的值,根据不同的值进行调整!
  22. # 根据 fps 计算毫秒
  23. miliseconds = math.floor(1000 * int(match.group(2)) / fps) - 1
  24. # 返回匹配组1,即 xx:xx:xx,连接计算出的毫秒并格式化为带有前导零的形式,以防值小于100
  25. return f"{match.group(1)}{miliseconds:03d}"
  26. # (\d{2}:\d{2}:\d{2},)(\d{2}) 是一个正则表达式,用于匹配 xx:xx:xx,xx
  27. modified_subtitles = re.sub(r"(\d{2}:\d{2}:\d{2},)(\d{2})", modify_sub_match, subtitles)
  28. # 打印 `modified_subtitles` 的值
  29. print(modified_subtitles)
  30. # 将 `modified_subtitles` 保存到 output_subtitles.srt
  31. with open("output_subtitles.srt", "w") as file:
  32. file.write(modified_subtitles)

希望这对你有帮助!

英文:

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

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

确定