一个学渣 发表于 2024-1-4 11:53

py视频单独时间点如何相加?

本帖最后由 一个学渣 于 2024-1-4 17:44 编辑

比如视频时间点 00:09:48.336,我想加上5.1秒变成 00:14:49.336,该怎么写?
网上有一种变成日期的写法感觉很慢,还非要指定年月日,我只想获得相同格式相加的时间,求大佬解惑

7593454 发表于 2024-1-4 12:44

from datetime import datetime, timedelta

time_str = "00:09:48.336"
time_obj = datetime.strptime(time_str, "%H:%M:%S.%f")
time_to_add = timedelta(seconds=5.1)
new_time_obj = time_obj + time_to_add
new_time_str = new_time_obj.strftime("%H:%M:%S.%f")

milu1123 发表于 2024-1-4 13:04

a = datetime.datetime.now()
b = datetime.datetime(2024, 2, 6, 12, 00)
count_down = (b - a).days

wapjsx 发表于 2024-1-4 14:06

timedelta 是正解!

52pojie11023995 发表于 2024-1-4 16:25

本帖最后由 52pojie11023995 于 2024-1-9 23:24 编辑

# 自己的算法:
def TimeToSec(time):
    result = time.split(":")
    return int(result)*3600+int(result)*60+float(result)
print(TimeToSec("00:09:48.336"))


def SecToTime(sec1):
    list = str(sec1).split(".")
    sec = int(list)
    hour = int(sec // 3600)
    hourStr = ("0"+str(hour))[-2:]
   
    minute = int((sec - hour*3600) // 60)
    minuteStr = ("0"+str(minute))[-2:]
   
    second = int(sec - hour*3600 - minute*60)
    secondStr = ("0"+str(second))[-2:]

    return hourStr+":"+minuteStr+":"+secondStr+"."+list

print(SecToTime(588.336))
print(SecToTime(588.336+12.3))
页: [1]
查看完整版本: py视频单独时间点如何相加?