2323TranscriptFormat = Literal ["csv" , "json" , "srt" , "txt" , "vtt" ]
2424TRANSCRIPT_FORMATS : tuple [TranscriptFormat ] = typing .get_args (TranscriptFormat )
2525
26+ SECONDS_IN_MINUTE = 60
27+ SECONDS_IN_HOUR = SECONDS_IN_MINUTE * 60
28+ SECONDS_IN_DAY = SECONDS_IN_HOUR * 24
29+
2630
2731class LineError (NamedTuple ):
2832 time : float
@@ -34,37 +38,33 @@ def seconds_to_time(seconds: float) -> str:
3438 """Convert seconds to time.
3539
3640 Args:
37- seconds (float): the number of seconds
41+ seconds (float): the number of seconds. It must be less than 2**32.
3842
3943 Returns:
4044 str: the time in the format :
4145 -"mm:ss.ms" if less than 1 hour
4246 -"hh:mm:ss" if less than 1 day
4347 -"dd hh:mm:ss" if at least 1 day
48+
49+ Raises:
50+ ValueError: if seconds > 2**32
4451 """
52+ if seconds > 2 ** 32 :
53+ # conversion from float to int here will lose precision
54+ msg = "seconds >= 2**32 are not supported because of loss of precision."
55+ raise ValueError (msg )
56+
4557 seconds = float (seconds )
46-
47- if seconds < 3600 : # Less than 1 hour
48- minutes , seconds = divmod (seconds , 60 )
49- minutes = int (minutes )
50- if seconds == int (seconds ): # No decimal part in seconds
51- return f"{ minutes :02d} :{ int (seconds ):02d} "
52- # Handle fractional seconds
53- int_part , dec_part = str (seconds ).split ("." )
54- dec_part = dec_part [:2 ] # Keep only two decimal places
55- int_part = int (int_part )
56- return f"{ minutes :02d} :{ int (int_part ):02d} .{ dec_part } "
57-
58- elif seconds < 86400 : # Less than 1 day
59- hours , remaining_seconds = divmod (seconds , 3600 )
60- minutes , seconds = divmod (remaining_seconds , 60 )
61- return f"{ int (hours ):02d} :{ int (minutes ):02d} :{ int (seconds ):02d} "
62-
63- else : # 1 day or more
64- days , remaining_seconds = divmod (seconds , 86400 )
65- hours , remaining_seconds = divmod (remaining_seconds , 3600 )
66- minutes , seconds = divmod (remaining_seconds , 60 )
67- return f"{ int (days )} d { int (hours ):02d} :{ int (minutes ):02d} :{ int (seconds ):02d} "
58+ days , seconds = divmod (seconds , SECONDS_IN_DAY )
59+ hours , seconds = divmod (seconds , SECONDS_IN_HOUR )
60+ minutes , seconds = divmod (seconds , SECONDS_IN_MINUTE )
61+
62+ if days > 0 :
63+ return f"{ int (days )} d { int (hours ):02d} :{ int (minutes ):02d} :{ round (seconds ):02d} "
64+ if hours > 0 :
65+ return f"{ int (hours ):02d} :{ int (minutes ):02d} :{ round (seconds ):02d} "
66+ return f"{ int (minutes ):02d} :{ seconds :05.2f} "
67+
6868
6969def seconds_to_srt_time (seconds : float ) -> str :
7070 """Convert seconds to SRT time format.
@@ -76,11 +76,10 @@ def seconds_to_srt_time(seconds: float) -> str:
7676 str: the time in the format "hh:mm:ss,ms"
7777 """
7878 seconds = float (seconds )
79- hours , remainder = divmod (seconds , 3600 )
80- minutes , seconds = divmod (remainder , 60 )
81- int_seconds , dec_seconds = str (seconds ).split ("." )
82- dec_seconds = dec_seconds [:3 ]
83- return f"{ int (hours ):02d} :{ int (minutes ):02d} :{ int (int_seconds ):02d} ,{ dec_seconds } "
79+ hours , remainder = divmod (seconds , SECONDS_IN_HOUR )
80+ minutes , seconds = divmod (remainder , SECONDS_IN_MINUTE )
81+ seconds_str = f"{ round (seconds , 3 ):06.3f} " .replace ("." , "," )
82+ return f"{ int (hours ):02d} :{ int (minutes ):02d} :{ seconds_str } "
8483
8584
8685@dataclass
@@ -467,7 +466,7 @@ def _init_format(self):
467466
468467 case (_, None ):
469468 if self .output .is_dir ():
470- self .output = self . output / self .input .stem
469+ self .output /= self .input .stem
471470 self .format = "all"
472471 else :
473472 self .format = self .output .suffix [1 :]
0 commit comments