Audio Conversion
Recordings come in many formats: a phone records M4A or 3GP, a browser WebM, a dictaphone WAV. Each transcription model reads only some of them, and each speech model writes only some. When the runtime has an audio converter, a PipeTranscribe step whose model cannot read a recording reads it converted into a format the model reads, and a PipeSpeechGen step asking for a format its model does not write has the model speak in a format it writes, then delivers the speech converted into the one asked. Without a converter, which is the default, nothing is converted and such a step is refused as it always was.
Every recording that passes through the converter also has its length measured, by decoding it, so the transcription model's length limit applies to it even when the recording did not state its length.
The converter, its configuration and its trace record belong to the Pipelex runtime. The MTHDS standard only says which formats a model reads or writes; how a runtime gets a model to read or write another is the runtime's business.
Turning it on
Conversion is off until [runtime.audio_conversion] names a backend:
# .pipelex/pipelex.toml
[runtime.audio_conversion]
backend = "ffmpeg" # "none" (the default), "ffmpeg", or a backend a plugin registers
max_source_bytes = 50_000_000
[runtime.audio_conversion.ffmpeg]
binary = "ffmpeg"
timeout_seconds = 300
Every value above except backend is the default. Audio Conversion Configuration describes each key. With backend = "none", every check and every step behaves exactly as if conversion did not exist.
A backend that is named but cannot be used stops Pipelex at startup with runtime.misconfigured, rather than at the first recording a run reads: an ffmpeg binary that is not found, or a backend name that neither the runtime nor any plugin provides. A converter service that is down at startup is not a misconfiguration; a step that needs it then fails as the converter being unavailable.
The backends
- ffmpeg runs FFmpeg on this machine, once per conversion, in a temporary directory, reading only the file it is given (
-protocol_whitelist file, so a playlist or any format naming other files is refused), keeping the first audio stream and dropping video, subtitles and metadata, writing no more thanmax_output_bytes(-fs, so a conversion cannot fill the disk, and one that passes that size is refused as too large rather than kept truncated), and killing its whole process group at the timeout. It is meant for a developer's machine and for tests. It is not an isolation boundary: FFmpeg reads the file with the rights of the process that runs Pipelex, so never use it on a server that converts recordings you did not make. The FFmpeg build must have the encoders of the formats your models read:libopusfor Ogg Opus,libmp3lamefor MP3. A conversion that needs an encoder the build lacks fails withruntime.misconfigured, and one stopped by a full disk withruntime.unexpected, so neither blames a valid recording. - A plugin's backend converts in whatever way the plugin chooses. Pipelex's hosted platform registers one that calls its own converter service, which runs FFmpeg in an isolated container. See Writing a converter plugin.
Each conversion is stamped with the version of the converter that made it, as its converter-version metadata: for the ffmpeg backend, the version ffmpeg -version names on its first line, read once and kept for the life of the process.
What a step converts
A recording a transcription model cannot read
A PipeTranscribe step reads its recording converted when the runtime has a converter, the run is live, the model's spec lists the audio formats it reads, the recording's format is known and is not one of them, and the recording's format is one the runtime hands a converter (any of the audio formats Pipelex recognises, from wav and mp3 to m4a, webm, 3gp and amr). Otherwise the step reads the original.
The conversion target is read off the model's spec. A transcription model may name it with the valued constraint conversion_target, one of ogg, mp3, wav and flac, which must be among the formats it reads. A model that names none is converted into Ogg Opus when it reads ogg, else into the first of MP3, FLAC and WAV it reads. The recording is converted to one channel at 16 kHz, what speech recognition reads, at 16 kbit/s for Ogg Opus and 32 kbit/s for MP3; FLAC and WAV are lossless. A model that reads none of these formats has nothing a converter writes, so a recording it cannot read is refused as before.
Only the model's input changes. The Audio the model receives points at the conversion, with its media type, a file name carrying the new extension, and its measured duration_seconds; the recording's title is kept. The step's output, the run's inputs and every later step still see the recording as it was given, and the conversion stays a derived object, never a value of the run.
The length of a recording
A transcription model may limit the length of a recording with max_audio_duration_seconds, and the step refuses a longer recording with input.too_large, in seconds, before the model is called. That check needs the recording's length. A converted recording always carries it. A recording the model reads as it is, whose Audio states no duration_seconds, is measured by the converter when the model limits the length, so the limit applies to every recording the converter sees. A recording the measurement cannot take, larger than max_source_bytes or one the converter cannot decode, is not refused for it: the model reads it as it is, of unknown length, as it would without a converter, and the runtime logs a warning. Without a converter, the length is checked only when the Audio states it, and the model's provider answers for the rest.
Speech in a format its model does not write
A PipeSpeechGen step whose output_format the model's spec does not list among the formats it writes is refused with model.capability_unsupported without a converter. With one, the step passes when the method loads and before the call, the model is asked for a format it writes, Ogg Opus first, then MP3, then WAV, so long speech stays within the max_source_bytes the converter reads after the model has been paid, and the speech is converted into the asked format before the step delivers it: one channel at 24 kHz, at 32 kbit/s for Ogg Opus and 64 kbit/s for MP3. The delivered Audio keeps the text it speaks as its source_text and is stored as any generated file is, under the run's generated/ prefix. A step asking for a format its model writes gets the model's speech as it is, and so does speech whose format the runtime cannot tell from its media type, since a converter cannot read a recording of unknown format: the runtime logs a warning and delivers it unconverted.
What is checked, and when
The step and its checks apply one rule, so a recording a check accepts is the one the step converts:
- When the method loads, a
PipeSpeechGenasking for a format its model does not write passes when the runtime has a converter. - Before the call, in a dry run as in a live one, a recording whose format the model does not read passes when the runtime has a converter that can make a format the model reads, and the length is checked against the recording's stated length; in a live run, after the conversion or the measurement, the length is checked again, now that it is known.
- Without a converter, a recording the model cannot read is refused with
input.file_type_unsupported, and the refusal's message says when a converter,[runtime.audio_conversion], would have converted it.
The run setup's file check, which refuses a file input certain to reach a step that cannot read it, does not walk recordings: a recording is checked at its PipeTranscribe step.
Dry runs convert nothing. A dry run calls no converter, measures nothing, and its speech mock keeps the step's own format.
Where a conversion is kept
A recording converted for a model is converted once and kept, so that the next step, or the next run, reading the same recording for a model of the same target reuses it.
- A source in storage (
pipelex-storage://…), with a storage provider that keeps object metadata and writes an object only when its key is free, as every provider Pipelex ships does, has its conversion beside it, in the same directory, at<source stem>--<source extension>--<target label>.<extension>: the Ogg Opus conversion oforg/assets/memo.m4aisorg/assets/memo--m4a--ogg-1ch-16000hz-16k.ogg. The target label names the format, the channels, the sample rate and, for a lossy format, the bitrate, so a model with another target converts again, and the source's extension stays in the name, so two recordings that differ only by it keep apart. A source whose file name is not a stem of letters, digits,_and-with one alphanumeric extension has its conversion named by the SHA-256 of that file name instead, still beside it. - A source outside storage, an
http(s)URL, a local path or adata:URL, has no key to stand beside, and a source in a plugin's storage that keeps no metadata, or whose create-only write is a lookup then a write, has none it can trust: nothing there could say which recording an object beside it came from, and two runs racing on a source replaced between them could each be answered with the other's conversion. Its conversion goes under the run's storage scope, named by the SHA-256 of the source's bytes and its format:<storage scope>/<sha256>--<format>--<target label>.<extension>, the format beingaudiowhen it is unknown. Every step of the run that reads the same recording then converts it once, and a purge of the run erases the conversion.
Both names obey the storage-URI grammar a hosted plane holds every pipelex-storage:// address to: every path segment and the file name's stem are made of letters, digits, _ and -, and the file name has one alphanumeric extension. A converted recording is an address the model's provider reads, so a name with a dot in its stem would be refused before the model is called.
Every conversion is written create-only with its media type and the metadata entries converter-version, audio-target, the target label, duration-seconds, the length of the converted sound, and source-sha256, the SHA-256 of the recording it was converted from. The recording is therefore read before its conversion is looked up, and a conversion found at its key whose audio-target names the same target and whose source-sha256 names this recording's bytes is reused, whatever its converter-version, with the length its metadata gives. A source replaced at the same key is not read through the conversion of the recording it replaced: the kept conversion names other bytes, so it is another object, and the run converts the new recording under its storage scope. Under the run's storage scope the key itself names the recording's SHA-256, so a conversion a storage provider keeping no metadata holds there is reused too, its length measured again: on the kept object when it is within max_source_bytes, and otherwise on its source, since a lossless conversion can be larger than a recording within that limit while keeping its length. A kept object whose length must be measured and whose measurement runs out of time is not held against it: its source is measured instead. A kept object whose length must be measured and that the converter cannot decode is not this conversion: beside a source, the run then keeps its own conversion under its storage scope, and under the run's storage scope it fails the step with runtime.unexpected, never blamed on the recording. An object at the key beside a source that names another target or another source, or no source at all, cannot be replaced, so the run converts the recording and keeps its own conversion under its storage scope instead. The key under the run's storage scope cannot be replaced either, and only the runtime writes there, so an object there naming another target fails the step with runtime.unexpected, a fault of the storage rather than of the recording. When two steps convert the same recording at once, the first write wins and both read the stored conversion.
Speech converted into the format a step asks for is the step's output, not a derived object, so it is stored as any generated file is and is not reused. The model's own speech it was converted from is the derived object: it is kept under the run's storage scope as <storage scope>/<sha256>--speech.<extension>, so the run's generated/ files hold the delivered speech alone, and a purge of the run erases it. Where the model's speech is kept is decided once it is written, from the format the model actually wrote rather than the one it was asked for, so speech the step delivers as its model wrote it, in the format the step asks for or in one the runtime cannot tell, is stored under generated/ too, and only speech the step converts is kept as a derived object.
The run's read scope applies to the source as to every file a step reads: a source outside it is refused before anything is converted.
What the trace records
Each recording a step reads or delivers converted is recorded on the step's node as an audio_converted trace event, written as soon as the step has the conversion and before its model is called, so a step that fails afterwards still records the conversion it paid for. The event carries the step's node, the recording's input, why it was converted (model_input or speech_output), the original's format, the target label, whether the conversion was kept or made, the converter's name and version, the converted sound's length and, for a conversion this step made, how long it took. A measurement alone writes no event. Execution Graph Tracing lists its fields. The execution graph does not draw it.
Each conversion or measurement also logs A step converts or measures a recording, with the step, the input, the purpose, the formats, whether the result was measured, kept or made, the converter's version and the length.
Errors
A conversion adds no error code: each outcome takes the code the error model already has for it. Every failure of a recording names the input it came from, and the step retries none of them; what is worth retrying, the backend retries before it fails. Speech a model wrote is no input, so a failure to convert it names none.
| What happened | Code |
|---|---|
| The converter could not read the recording: damaged, not audio at all, holding no sound (FFmpeg decoding it reaches no time), of a format the backend does not read, or FFmpeg exiting with any error other than the machine's below | input.file_unreadable, its reason being conversion_failed |
The source is larger than max_source_bytes: refused before it is read when it is in storage, a local file or a data URL; downloaded under that limit, or under [runtime.network] max_fetch_bytes when that is smaller, when it is at a URL. Its advice names the setting whose limit applied |
input.too_large, with limit and size in the unit bytes |
The conversion passes max_output_bytes as the ffmpeg backend writes it |
input.too_large, with limit and size in the unit bytes |
| The conversion ran out of time: FFmpeg was killed at its timeout, or the converter service gave up | input.too_large, with neither limit, size nor unit, since the seconds spent converting are no length of the recording |
| The recording, converted or measured, is longer than the transcription model takes | input.too_large, with limit and size in the unit seconds |
| The converter is busy or unreachable after the backend's own retries | runtime.unavailable, naming the dependency audio_converter; an orchestrator retries the step as it retries that code |
| The converter could not read the speech a model wrote | provider.response_invalid, naming the speech model's provider, its defect being malformed |
The speech a model wrote is larger than max_source_bytes, or its conversion ran out of time or passed max_output_bytes |
runtime.unexpected |
The converter answered something that is not the file it was asked for, such as a conversion into another target, the ffmpeg backend could not write its output because the disk is full, or the key a run keeps a conversion under holds another target's object or one the converter cannot decode |
runtime.unexpected |
| A step needs a conversion in a process that has no converter, or on a host whose content generator does not convert | runtime.misconfigured, naming the component runtime.audio_conversion |
| The configured backend cannot be used at startup | runtime.misconfigured, at startup |
The ffmpeg build lacks the encoder a conversion needs, such as libopus |
runtime.misconfigured, naming the component runtime.audio_conversion |
Without a converter, a recording a transcription model cannot read is refused as before with input.file_type_unsupported, and a speech format its model does not write with model.capability_unsupported.
Distributed execution
On a host runtime such as Temporal, a step's code may run in workflow code, which does no input or output. So the step only decides whether to convert or measure, and finding, making or measuring the recording is a leaf of the content generator, make_readable_audio, which the host dispatches as an activity like every other leaf. The leaf, readable_audio_of_recording in pipelex.cogt.content_generation.audio_conversion_generate, writes the conversion to storage and returns its URI in an Audio, never its bytes, so a recording never crosses the host's boundary. A host whose content generator does not implement this leaf fails the step that needs a conversion with runtime.misconfigured rather than converting in workflow code, and a deployment with no converter is not affected. The step decides from the converter configured in the process running it, and the leaf converts with the one configured in the process running it, so configure [runtime.audio_conversion] the same way in every process of the deployment.
Writing a converter plugin
A plugin contributes a backend by registering a factory under a backend name of its own; none and ffmpeg are the runtime's and are refused, and two plugins registering one name fail at discovery naming both:
from pipelex.plugins.contract import PLUGIN_API_VERSION
from pipelex.plugins.registrar import PluginRegistrar
from pipelex.tools.audio_conversion.audio_conversion_config import AudioConversionConfig
from pipelex.tools.audio_conversion.audio_converter_protocol import AudioConverterProtocol
def _make_service_converter(config: AudioConversionConfig) -> AudioConverterProtocol:
return ServiceAudioConverter(max_source_bytes=config.max_source_bytes)
class AudioServicePlugin:
name = "audio-service"
targets_api = PLUGIN_API_VERSION
def register(self, registrar: PluginRegistrar) -> None:
registrar.add_audio_converter(method="converter_service", factory=_make_service_converter)
A deployment then sets backend = "converter_service". register only registers: the factory is called once at startup, never at registration, with the whole [runtime.audio_conversion] table, so it may build its client then; anything else the converter needs, such as its service's address or credentials, is the plugin's own configuration. The converter implements AudioConverterProtocol (pipelex.tools.audio_conversion.audio_converter_protocol):
converter_name, the backend's name, which the trace records;convert_audio(data=…, source_format=…, target=…), which returns aConvertedAudio: the converted bytes, theAudioTargetit was asked for (format, channels, sample rate and bitrate), the converted sound's length in seconds measured by decoding it, the converter's name and its version;measure_audio(data=…, source_format=…), which returns aMeasuredAudio: the recording's length in seconds, measured by decoding it, and the converter's name and version.
source_format is the recording's format key, such as m4a, or None when it is unknown. Every failure is one of the errors of pipelex.tools.audio_conversion.exceptions, which carry the codes of the table above: AudioUnreadableByConverterError for a recording the converter cannot read or that holds no sound, AudioTooLargeToConvertError for one past the converter's size or its time, AudioConverterUnavailableError for a converter busy or unreachable, and AudioConverterAnswerError for an answer that is not the file asked for. A converter retries what its service's answers say is worth a retry before raising, and never reads anything but the bytes it is given. The step names the input; the converter does not know it.
Running the integration tests
The tests that convert real recordings run FFmpeg on this machine and skip when it is not installed:
.venv/bin/pytest -q tests/integration/pipelex/tools/audio_conversion
They make a two-second tone with FFmpeg itself, convert it from M4A and WAV into each target format, check each conversion's length, and check that bytes that are no recording are refused as unreadable. The test shards of tests-check.yml and tests-full-check.yml install Ubuntu's FFmpeg before they run, so CI runs these tests rather than skipping them.
Related Documentation
- Audio Conversion Configuration - Every key of
[runtime.audio_conversion] - Transcription and speech generation models - The formats a model reads or writes, and its
conversion_target - PipeTranscribe - The step that reads a recording
- PipeSpeechGen - The step that speaks a text
- Office File Conversion - The same seam for Word, Excel and PowerPoint files
- Execution Graph Tracing - The
audio_convertedtrace event