1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2024-11-02 17:22:31 +01:00

[utils,common] Merge format_srt_time and _subtitles_timecode

format_srt_time uses a comma as the delimiter between seconds and
milliseconds while _subtitles_timecode uses a dot. All .srt examples I
found on the Internet uses a comma, so I use a comma in the merged
version. See http://matroska.org/technical/specs/subtitles/srt.html and
http://devel.aegisub.org/wiki/SubtitleFormats/SRT
This commit is contained in:
Yen Chi Hsuan 2015-05-12 13:04:54 +08:00
parent 1c7e2e64f6
commit c1c924abfe
3 changed files with 7 additions and 13 deletions

View File

@ -1072,9 +1072,6 @@ def extract_automatic_captions(self, *args, **kwargs):
def _get_automatic_captions(self, *args, **kwargs): def _get_automatic_captions(self, *args, **kwargs):
raise NotImplementedError("This method must be implemented by subclasses") raise NotImplementedError("This method must be implemented by subclasses")
def _subtitles_timecode(self, seconds):
return '%02d:%02d:%02d.%03d' % (seconds / 3600, (seconds % 3600) / 60, seconds % 60, (seconds % 1) * 1000)
class SearchInfoExtractor(InfoExtractor): class SearchInfoExtractor(InfoExtractor):
""" """

View File

@ -7,6 +7,7 @@
from ..utils import ( from ..utils import (
ExtractorError, ExtractorError,
float_or_none, float_or_none,
srt_subtitles_timecode,
) )
@ -39,8 +40,8 @@ def _fix_subtitles(self, subs):
'%s\r\n%s --> %s\r\n%s' '%s\r\n%s --> %s\r\n%s'
% ( % (
num, num,
self._subtitles_timecode(item['startMillis'] / 1000.0), srt_subtitles_timecode(item['startMillis'] / 1000.0),
self._subtitles_timecode(item['endMillis'] / 1000.0), srt_subtitles_timecode(item['endMillis'] / 1000.0),
item['text'], item['text'],
) for num, item in enumerate(subs, 1)) ) for num, item in enumerate(subs, 1))

View File

@ -1835,12 +1835,8 @@ def parse_dfxp_time_expr(time_expr):
return 3600 * int(mobj.group(1)) + 60 * int(mobj.group(2)) + float(mobj.group(3)) return 3600 * int(mobj.group(1)) + 60 * int(mobj.group(2)) + float(mobj.group(3))
def format_srt_time(seconds): def srt_subtitles_timecode(seconds):
(mins, secs) = divmod(seconds, 60) return '%02d:%02d:%02d,%03d' % (seconds / 3600, (seconds % 3600) / 60, seconds % 60, (seconds % 1) * 1000)
(hours, mins) = divmod(mins, 60)
millisecs = (secs - int(secs)) * 1000
secs = int(secs)
return '%02d:%02d:%02d,%03d' % (hours, mins, secs, millisecs)
def dfxp2srt(dfxp_data): def dfxp2srt(dfxp_data):
@ -1872,8 +1868,8 @@ def parse_node(node):
end_time = begin_time + parse_dfxp_time_expr(para.attrib['dur']) end_time = begin_time + parse_dfxp_time_expr(para.attrib['dur'])
out.append('%d\n%s --> %s\n%s\n\n' % ( out.append('%d\n%s --> %s\n%s\n\n' % (
index, index,
format_srt_time(begin_time), srt_subtitles_timecode(begin_time),
format_srt_time(end_time), srt_subtitles_timecode(end_time),
parse_node(para))) parse_node(para)))
return ''.join(out) return ''.join(out)