From 99dd9126fd4d9afc0870434645b629f4e78411ce Mon Sep 17 00:00:00 2001 From: Jonas Depoix Date: Mon, 7 Jun 2021 08:52:29 +0200 Subject: [PATCH] improved readability of time conversion --- youtube_transcript_api/formatters.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/youtube_transcript_api/formatters.py b/youtube_transcript_api/formatters.py index a2006cb..ceeadfd 100644 --- a/youtube_transcript_api/formatters.py +++ b/youtube_transcript_api/formatters.py @@ -5,9 +5,9 @@ import pprint class Formatter(object): """Formatter should be used as an abstract base class. - + Formatter classes should inherit from this class and implement - their own .format() method which should return a string. A + their own .format() method which should return a string. A transcript is represented by a List of Dictionary items. """ @@ -95,15 +95,11 @@ class WebVTTFormatter(Formatter): '00:00:06.930' """ time = float(time) - hours= int(time) // 3600 - time= time - hours*3600 - mins, secs = ( - int(time) // 60, - int(time) % 60, - ) + hours, remainder = divmod(time, 3600) + mins, secs = divmod(remainder, 60) ms = int(round((time - int(time))*1000, 2)) return "{:02d}:{:02d}:{:02d}.{:03d}".format(hours, mins, secs, ms) - + def format_transcript(self, transcript, **kwargs): """A basic implementation of WEBVTT formatting. @@ -127,7 +123,7 @@ class WebVTTFormatter(Formatter): self._seconds_to_timestamp(duration) ) lines.append("{}\n{}".format(time_text, line['text'])) - + return "WEBVTT\n\n" + "\n\n".join(lines) + "\n" def format_transcripts(self, transcripts, **kwargs):