Python应用:计时器"时:分:秒"字符串与秒数相互转换
在调整字幕时间时发现不是等间隔时间偏移,而是间隔呈线性增加。真不知这帮做字幕的人怎么搞的,不会是播放器计算时间偏移差错吧?
不管他了,如果要修改字幕时间,首先要有一对方便地进行"HH:MM:SS"格式跟计时器秒数相互转换的函数。于是就写了一个模块:
[color="#808080"]
[color="#808080"]# timeItv.py
[color="#808080"]# encoding:gb2312
[color="#0000ff"]import [color="#000080"]sys[color="#000000"],[color="#000080"]re
[color="#808080"]# 将计时器"时:分:秒"字符串转换为秒数间隔
[color="#0000ff"]def [color="#000000"]time2itv(sTime):
[color="#000000"]p[color="#008000"]=[color="#64a2e6"]"^([0-9]+):([0-5][0-9]):([0-5][0-9])$"
[color="#000000"]cp[color="#008000"]=[color="#000080"]re[color="#008000"].[color="#808000"]compile[color="#000000"](p)
[color="#0000ff"]try[color="#000000"]:
[color="#000000"]mTime[color="#008000"]=[color="#000000"]cp[color="#008000"].[color="#000000"]match(sTime)
[color="#0000ff"]except [color="#ff0000"]TypeError[color="#000000"]:
[color="#0000ff"]return [color="#64a2e6"]"[InModuleError]:time2itv(sTime) invalid argument type"
[color="#0000ff"]if [color="#000000"]mTime:
[color="#000000"]t[color="#008000"]=[color="#808000"]map[color="#000000"]([color="#808000"]int[color="#000000"],mTime[color="#008000"].[color="#000000"]group(1,2,3))
[color="#0000ff"]return [color="#000000"]3600[color="#008000"]*[color="#000000"]t[0][color="#008000"]+[color="#000000"]60[color="#008000"]*[color="#000000"]t[1][color="#008000"]+[color="#000000"]t[2]
[color="#0000ff"]else[color="#000000"]:
[color="#0000ff"]return [color="#64a2e6"]"[InModuleError]:time2itv(sTime) invalid argument value"
[color="#808080"]# 将秒数间隔转换为计时器"时:分:秒"字符串
[color="#0000ff"]def [color="#000000"]itv2time(iItv):
[color="#0000ff"]if [color="#808000"]type[color="#000000"](iItv)[color="#008000"]==[color="#808000"]type[color="#000000"](1):
[color="#000000"]h[color="#008000"]=[color="#000000"]iItv[color="#008000"]/[color="#000000"]3600
[color="#000000"]sUp_h[color="#008000"]=[color="#000000"]iItv[color="#008000"]-[color="#000000"]3600[color="#008000"]*[color="#000000"]h
[color="#000000"]m[color="#008000"]=[color="#000000"]sUp_h[color="#008000"]/[color="#000000"]60
[color="#000000"]sUp_m[color="#008000"]=[color="#000000"]sUp_h[color="#008000"]-[color="#000000"]60[color="#008000"]*[color="#000000"]m
[color="#000000"]s[color="#008000"]=[color="#000000"]sUp_m
[color="#0000ff"]return [color="#64a2e6"]":"[color="#008000"].[color="#000000"]join([color="#808000"]map[color="#000000"]([color="#808000"]str[color="#000000"],(h,m,s)))
[color="#0000ff"]else[color="#000000"]:
[color="#0000ff"]return [color="#64a2e6"]"[InModuleError]:itv2time(iItv) invalid argument type"
[color="#0000ff"]if [color="#008000"]__name__==[color="#64a2e6"]"__main__"[color="#000000"]:
[color="#808080"]# 仅供测试
[color="#000000"]sTime[color="#008000"]=[color="#64a2e6"]"1223:34:15"
[color="#000000"]itv[color="#008000"]=[color="#000000"]time2itv(sTime)
[color="#0000ff"]print [color="#000000"]itv [color="#808080"]# 4404855
[color="#0000ff"]print [color="#000000"]itv2time(itv) [color="#808080"]# 1223:34:15
[color="#808080"]# 不合约定的参数
[color="#0000ff"]print [color="#000000"]time2itv([color="#64a2e6"]"12:34:95"[color="#000000"])
[color="#0000ff"]print [color="#000000"]time2itv([color="#64a2e6"]"sfa123"[color="#000000"])
[color="#0000ff"]print [color="#000000"]time2itv(itv)
[color="#0000ff"]print [color="#000000"]itv2time([color="#64a2e6"]"451223"[color="#000000"])
[color="#0000ff"]print [color="#000000"]itv2time(sTime)
[color="#000000"]