diff --git a/faster_whisper/__init__.py b/faster_whisper/__init__.py index 08b3009..93ebfce 100644 --- a/faster_whisper/__init__.py +++ b/faster_whisper/__init__.py @@ -1 +1,2 @@ from faster_whisper.transcribe import WhisperModel +from faster_whisper.utils import format_timestamp diff --git a/faster_whisper/utils.py b/faster_whisper/utils.py new file mode 100644 index 0000000..6b03050 --- /dev/null +++ b/faster_whisper/utils.py @@ -0,0 +1,21 @@ +def format_timestamp( + seconds: float, + always_include_hours: bool = False, + decimal_marker: str = ".", +): + assert seconds >= 0, "non-negative timestamp expected" + milliseconds = round(seconds * 1000.0) + + hours = milliseconds // 3_600_000 + milliseconds -= hours * 3_600_000 + + minutes = milliseconds // 60_000 + milliseconds -= minutes * 60_000 + + seconds = milliseconds // 1_000 + milliseconds -= seconds * 1_000 + + hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else "" + return ( + f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}" + )