在折腾从字幕提取时间段后,起始时间和结束时间,都是pysrt中的SubRipTime类型:
其中有我们此处需要的字段:
- hours
- minutes
- seconds
- milliseconds
用于后续用ffmpeg从mp4中提取mp3设置起始和结束的时间段
但是此处需要:
能方便的把时间信息保存出来,并传递过去
且不要依赖于pysrt的SubRipTime的
去看了半天,感觉是:
timedelta
“class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)¶”
然后也去写代码:
from datetime import timedelta def formatFfmpegTimeStr(timedeltaValue, seperatorHms=":", seperatorMs="."): """ (1) format time to 00:00:03.110, for pass into ffmpeg to use: ffmpeg -i show_65586_video.mp4 -ss 00:00:03.110 -to 00:00:06.110 -b:a 128k extracted_audio_segment.mp3 (2) also use format to 000003110, used for normal file name: audio_000003110_000006110.mp3 """ ffmpegTimeStr = "%02d%s%02d%s%02d%s%03d" % ( timedeltaValue.hours, seperatorHms, timedeltaValue.minutes, seperatorHms, timedeltaValue.seconds, seperatorMs, timedeltaValue.milliseconds) return ffmpegTimeStr startTime = timedelta(hours=0, minutes=0, seconds=2, milliseconds=359) endTime = timedelta(hours=0, minutes=0, seconds=3, milliseconds=763)
结果调试发现此处传入的timedelta的变量,内部根本没有hours,minutes等字段
且seconds也是 整个所有的秒,而不是除去hours,minutes后的秒
所以就不符合要求
希望找到python内置的 日期,时间方面的类
可以保存我所需要的这几个字段,用于后续取用
python only hours minutes seconds milliseconds
好像datetime符合要求?有hour,minute,seconds,millisecond字段
不过要去把此处没有年月日的数据,初始化传入,得到一个datetime,不知道是否合适和可行
“class datetime.time
An idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds (there is no notion of “leap seconds” here). Attributes: hour, minute, second, microsecond, and tzinfo.”
好像又是:
time更合适
“* time – Time independent of the day (Hour, minute, second, microsecond)”
竟然没搜到 hour的字段解释
那么就去找找:
如何初始化python 的time,只使用:hour,minute,second,millisecond
python time.mktime
python create time only hour minute second millisecond
“time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)”
初始化time,这只year是1900?好像很麻烦的样子
现在看来是:
或许datetime的time可以满足要求?
或者直接用dict算了
“class datetime.time
An idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds (there is no notion of “leap seconds” here). Attributes: hour, minute, second, microsecond, and tzinfo.”
【总结】
最后用代码:
import datetime startTime = datetime.time(hour=0, minute=0, second=2, microsecond=359 * 1000) endTime = datetime.time(hour=0, minute=0, second=3, microsecond=763 * 1000) # Example: # ffmpeg -y -i show_65586_video.mp4 -ss 00:00:03.110 -to 00:00:06.110 -b:a 128k show_65586_audio_000003110_000006110.mp3 2> /dev/null startTimeStrFfmpeg = formatFfmpegTimeStr(startTime) endTimeStrFfmpeg = formatFfmpegTimeStr(endTime) def formatFfmpegTimeStr(timeValue, seperatorHms=":", seperatorMs="."): """ (1) format time to 00:00:03.110, for pass into ffmpeg to use: ffmpeg -i show_65586_video.mp4 -ss 00:00:03.110 -to 00:00:06.110 -b:a 128k extracted_audio_segment.mp3 (2) also use format to 000003110, used for normal file name: audio_000003110_000006110.mp3 Note: timeValue is class of datetime.time, NOT time """ millisecond = int(timeValue.microsecond / 1000) ffmpegTimeStr = "%02d%s%02d%s%02d%s%03d" % ( timeValue.hour, seperatorHms, timeValue.minute, seperatorHms, timeValue.second, seperatorMs, millisecond) return ffmpegTimeStr
注意:
此处用的是datetime.time:
import datetime
详见:
“class datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
Instance attributes (read-only):
- time.hour
- In range(24).
- time.minute
- In range(60).
- time.second
- In range(60).
- time.microsecond
- In range(1000000).”
而不是time
import time
因为time本身没有time(hour=xxx)这种初始化方法
详见:
转载请注明:在路上 » 【已解决】Python中可以方便的保存时间的小时分钟秒毫秒的方法