[英]how to convert datetime-like string into milliseconds
I have a user-defined function ( return_times
) that takes json file and returns two datetime-like strings.我有一个用户定义的函数( return_times
),它接受 json 文件并返回两个类似日期时间的字符串。
time_1, time_2= return_times("file.json")
print(time_1, time_2) # outputs: 00:00:11.352 00:01:51.936
By datetime-like string I mean 00:00:11.352
which suits '%H:%M:%S.%f'
formatting.通过类似日期时间的字符串,我的意思是00:00:11.352
适合'%H:%M:%S.%f'
格式。 However, when I try to convert them into milliseconds, I get negative values.但是,当我尝试将它们转换为毫秒时,我得到了负值。
from datetime import datetime
dt_obj_1 = datetime.strptime(time_1, '%H:%M:%S.%f')
start_ms = dt_obj_1.timestamp() * 1000
dt_obj_2 = datetime.strptime(time_2, '%H:%M:%S.%f')
end_ms = dt_obj_2.timestamp() * 1000
print(start_ms, end_ms ) # outputs: -2209019260648.0 -2209019160064.0
If I success I would like to trim a video with the following command:如果我成功了,我想使用以下命令修剪视频:
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
ffmpeg_extract_subclip("long_video.mp4", start_ms, end_ms, targetname="video_trimmed.mp4"), so just delete ` * 1000` part.
Note that ffmpeg_extract_subclip
requires its t1
and t2
parameters to be in seconds, not in milliseconds as I initially thought.请注意, ffmpeg_extract_subclip
要求其t1
和t2
参数以秒为单位,而不是我最初认为的以毫秒为单位。
Because of those negative integers I am not able to successfully run the trimming process.由于这些负整数,我无法成功运行修剪过程。 I searched the web that mainly discusses several formats for the year, month and day, but not '%H:%M:%S.%f'
.我在网上搜索了主要讨论年、月和日的几种格式,但没有搜索'%H:%M:%S.%f'
。
What may I be overlooking?我可能会忽略什么?
What may I be overlooking?我可能会忽略什么?
time.strptime
docs time.strptime
文档
The default values used to fill in any missing data when more accurate values cannot be inferred are
(1900, 1, 1, 0, 0, 0, 0, 1, -1)
.当无法推断出更准确的值时,用于填充任何缺失数据的默认值为(1900, 1, 1, 0, 0, 0, 0, 1, -1)
。
whilst start of epoch is 1970. You might get what you want by computing delta between what you parsed and default strptime as follows:而纪元的开始是 1970 年。您可能会通过计算您解析的内容和默认 strptime 之间的增量来获得您想要的内容,如下所示:
import datetime
time1 = "00:00:11.352"
delta = datetime.datetime.strptime(time1, "%H:%M:%S.%f") - datetime.datetime.strptime("", "")
time_s = delta.total_seconds()
print(time_s)
output输出
11.352
You need to add the year date (year, month, day) to datetime
, else this will default to 1 January 1900.您需要将年日期(年、月、日)添加到datetime
,否则这将默认为 1900 年 1 月 1 日。
What you do is this:你要做的是:
from datetime import datetime
s = "00:00:11.352"
f = '%H:%M:%S.%f'
datetime.strptime(s, f) # datetime.datetime(1900, 1, 1, 0, 0, 11, 352000)
One way to do this is to append the date-string to the time-string you receive from return_times
一种方法是将日期字符串附加到您从return_times
收到的时间字符串
From https://stackoverflow.com/a/59200108/2681662来自https://stackoverflow.com/a/59200108/2681662
The year 1900 was before the beginning of the UNIX epoch , which was in 1970, so the number of seconds returned by timestamp must be negative. 1900 年在UNIX 纪元开始之前,即 1970 年,因此时间戳返回的秒数必须为负数。
It's better to use a time
object instead of a datetime
object.最好使用time
对象而不是datetime
对象。
from datetime import time
time_1 = "00:00:11.352"
hours, minutes, seconds = time_1.split(":")
print(time(hour=int(hours), minute=int(minutes), second=int(float(seconds)),
microsecond=int(float(seconds) % 1 * 1000000)))
您可以将时间字符串拆分为小时、分钟、秒和毫秒,通过一些简单的数学计算,您可以获得以毫秒为单位的整个时间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.