最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python中可以方便的保存时间的小时分钟秒毫秒的方法

Python crifan 1537浏览 0评论
在折腾从字幕提取时间段后,起始时间和结束时间,都是pysrt中的SubRipTime类型:
其中有我们此处需要的字段:
  • hours
  • minutes
  • seconds
  • milliseconds
用于后续用ffmpeg从mp4中提取mp3设置起始和结束的时间段
但是此处需要:
能方便的把时间信息保存出来,并传递过去
且不要依赖于pysrt的SubRipTime的
去看了半天,感觉是:
timedelta
https://docs.python.org/3/library/datetime.html#timedelta-objects
“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内置的 日期,时间方面的类
可以保存我所需要的这几个字段,用于后续取用
time — Time access and conversions — Python 3.7.1 documentation
python only hours minutes seconds milliseconds
How to get min, seconds and milliseconds from datetime.now() in python? – Stack Overflow
好像datetime符合要求?有hour,minute,seconds,millisecond字段
不过要去把此处没有年月日的数据,初始化传入,得到一个datetime,不知道是否合适和可行
python – How to get just the hours and minutes from a datetime object – Stack Overflow
How to get min, seconds and milliseconds from datetime.now() in Python
8.1. datetime — Basic date and time types — Python 3.4.9 documentation
“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更合适
Python DateTime, TimeDelta, Strftime(Format) with Examples
“* time – Time independent of the day (Hour, minute, second, microsecond)”
16.3. time — Time access and conversions — Python 3.4.9 documentation
竟然没搜到 hour的字段解释
“The time value as returned by gmtime(), localtime(), and strptime(), and accepted by asctime(), mktime() and strftime(), is a sequence of 9 integers. The return values ofgmtime(), localtime(), and strptime() also offer attribute names for individual fields.”
那么就去找找:
如何初始化python 的time,只使用:hour,minute,second,millisecond
python time.mktime
Python time mktime()方法 | 菜鸟教程
Python3 time mktime()方法 | 菜鸟教程
Python时间mktime()方法 – Python教程™
【python】详解time模块功能asctime、localtime、mktime、sleep、strptime、strftime、time等函数以及时间的加减运算 – brucewong0516的博客 – CSDN博客
python create time only hour minute second millisecond
converting a time string to seconds in python – Stack Overflow
“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?好像很麻烦的样子
python – How do I convert seconds to hours, minutes and seconds? – Stack Overflow
datetime – Date/time value manipulation – Python Module of the Week
现在看来是:
或许datetime的time可以满足要求?
或者直接用dict算了
class datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
https://docs.python.org/3.2/library/datetime.html#available-types
“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
详见:
7.1. datetime — Basic date and time types — Python v3.2.6 documentation
“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)这种初始化方法
详见:
16.3. time — Time access and conversions — Python 3.4.9 documentation

转载请注明:在路上 » 【已解决】Python中可以方便的保存时间的小时分钟秒毫秒的方法

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.246 seconds, using 22.13MB memory