Implementation:Openai Whisper Get Writer
Overview
get_writer() is a factory function that returns a callable writer for serializing Whisper transcription results into standard output formats. It is the public API for output formatting, backed by a hierarchy of ResultWriter subclasses.
Source
- File:
whisper/utils.py:L296-318(get_writer),whisper/utils.py:L85-106(ResultWriterbase),whisper/utils.py:L109-116(WriteTXT),whisper/utils.py:L238-248(WriteVTT),whisper/utils.py:L251-262(WriteSRT),whisper/utils.py:L265-284(WriteTSV),whisper/utils.py:L287-293(WriteJSON) - Import:
from whisper.utils import get_writer - Repository: https://github.com/openai/whisper
Signature
def get_writer(output_format: str, output_dir: str) -> Callable[[dict, TextIO, dict], None]:
Parameters
| Parameter | Type | Description |
|---|---|---|
output_format |
str |
One of "txt", "vtt", "srt", "tsv", "json", or "all" |
output_dir |
str |
Directory path where output files will be written |
Inputs and Outputs
- Inputs: Transcription result dictionary with keys:
text,segments,language - Outputs: Written file(s) in the specified format in the output directory
Writer Hierarchy
All writers inherit from ResultWriter, which provides the base __call__ method for file handling. Each subclass implements the specific formatting logic:
| Writer Class | Format | Extension | Description |
|---|---|---|---|
WriteTXT |
Plain text | .txt |
Text content only, no timestamps or metadata |
WriteVTT |
WebVTT | .vtt |
Web subtitle format with WEBVTT header and HH:MM:SS.mmm timestamps
|
WriteSRT |
SubRip | .srt |
Standard subtitle format with sequential indices and HH:MM:SS,mmm timestamps
|
WriteTSV |
Tab-separated | .tsv |
Tabular format with millisecond timestamps |
WriteJSON |
JSON | .json |
Full structured output including all metadata |
When output_format="all", the function returns a composite writer that invokes all five format writers, producing one file of each type.
Usage Examples
from whisper.utils import get_writer
# Get a single writer
writer = get_writer("srt", "./output")
writer(result, audio_path) # Writes output/audio.srt
# Get all writers
writer = get_writer("all", "./output")
writer(result, audio_path) # Writes .txt, .vtt, .srt, .tsv, .json
Output Format Details
TXT Output
Writes the text from each segment, concatenated. No timing information.
VTT Output
WEBVTT
00:00:00.000 --> 00:00:02.400
Hello, this is a test.
00:00:02.400 --> 00:00:05.800
This is the second segment.
SRT Output
1
00:00:00,000 --> 00:00:02,400
Hello, this is a test.
2
00:00:02,400 --> 00:00:05,800
This is the second segment.
TSV Output
start end text
0 2400 Hello, this is a test.
2400 5800 This is the second segment.
JSON Output
Serializes the full result dictionary to JSON, including all segment metadata (tokens, probabilities, compression ratios).
Key Notes
- The
output_dirmust exist before calling the writer. - Output filenames are derived from the audio file path (e.g.,
speech.mp3producesspeech.srt). - The
ResultWriterbase class handles file creation and encoding (UTF-8). - The
"all"option is useful for archival purposes when multiple consumers need different formats.