diff options
author | pukkandan <pukkandan.ytdlp@gmail.com> | 2021-10-19 22:58:14 +0530 |
---|---|---|
committer | pukkandan <pukkandan.ytdlp@gmail.com> | 2021-10-19 22:58:25 +0530 |
commit | aa7785f860be0bae7135ee32fe0ef4f0ab00bbc1 (patch) | |
tree | 1c30ab046f81222cabb000b8ac9f8bf098e687b1 /yt_dlp/utils.py | |
parent | 9fab498fbf38dca24ef215d4789b13dd24d7952d (diff) | |
download | hypervideo-pre-aa7785f860be0bae7135ee32fe0ef4f0ab00bbc1.tar.lz hypervideo-pre-aa7785f860be0bae7135ee32fe0ef4f0ab00bbc1.tar.xz hypervideo-pre-aa7785f860be0bae7135ee32fe0ef4f0ab00bbc1.zip |
[utils] Standardize timestamp formatting code
Closes #1285
Diffstat (limited to 'yt_dlp/utils.py')
-rw-r--r-- | yt_dlp/utils.py | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 28431ac73..b88257bc2 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -2342,14 +2342,25 @@ def decodeOption(optval): return optval +_timetuple = collections.namedtuple('Time', ('hours', 'minutes', 'seconds', 'milliseconds')) + + +def timetuple_from_msec(msec): + secs, msec = divmod(msec, 1000) + mins, secs = divmod(secs, 60) + hrs, mins = divmod(mins, 60) + return _timetuple(hrs, mins, secs, msec) + + def formatSeconds(secs, delim=':', msec=False): - if secs > 3600: - ret = '%d%s%02d%s%02d' % (secs // 3600, delim, (secs % 3600) // 60, delim, secs % 60) - elif secs > 60: - ret = '%d%s%02d' % (secs // 60, delim, secs % 60) + time = timetuple_from_msec(secs * 1000) + if time.hours: + ret = '%d%s%02d%s%02d' % (time.hours, delim, time.minutes, delim, time.seconds) + elif time.minutes: + ret = '%d%s%02d' % (time.minutes, delim, time.seconds) else: - ret = '%d' % secs - return '%s.%03d' % (ret, secs % 1) if msec else ret + ret = '%d' % time.seconds + return '%s.%03d' % (ret, time.milliseconds) if msec else ret def _ssl_load_windows_store_certs(ssl_context, storename): @@ -4855,7 +4866,12 @@ def parse_dfxp_time_expr(time_expr): def srt_subtitles_timecode(seconds): - return '%02d:%02d:%02d,%03d' % (seconds / 3600, (seconds % 3600) / 60, seconds % 60, (seconds % 1) * 1000) + return '%02d:%02d:%02d,%03d' % timetuple_from_msec(seconds * 1000) + + +def ass_subtitles_timecode(seconds): + time = timetuple_from_msec(seconds * 1000) + return '%01d:%02d:%02d.%02d' % (*time[:-1], time.milliseconds / 10) def dfxp2srt(dfxp_data): |