annotools.audio¶
Cut and resample audio to 16-bit PCM WAV, the format every audio model accepts. Requires
annotools[media] (PyAV).
audio ¶
Audio clipping and resampling via PyAV (annotools[media]).
clip_audio ¶
clip_audio(
uri: str,
*,
start: float | None = None,
end: float | None = None,
sample_rate: int | None = None,
) -> tuple[bytes, dict[str, Any]]
Cut [start, end) seconds from the first audio stream of uri and return 16-bit PCM WAV bytes.
WAV keeps the clip self-describing and losslessly decodable by every audio model; resampling is optional so a caller can hit a model's expected rate (16 kHz for most speech models) in one step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
Local path or fsspec URL of an audio or video file with an audio stream. |
required |
start
|
float | None
|
Start time in seconds (>= 0); |
None
|
end
|
float | None
|
End time in seconds (> |
None
|
sample_rate
|
int | None
|
Output sample rate in Hz (>= 1); |
None
|
Returns:
| Type | Description |
|---|---|
bytes
|
|
dict[str, Any]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
For an invalid range or rate, a start beyond the source, a source without audio, or content that is not decodable; the message names the URI. |
FileNotFoundError
|
When the URI does not exist. |
OSError
|
For other read failures. |
ImportError
|
When PyAV is not installed ( |
Examples:
>>> from annotools import clip_audio
>>> wav, meta = clip_audio(
... "talk.wav", start=2, end=5, sample_rate=16000
... )
>>> meta["duration"], meta["sample_rate"]
(3.0, 16000)
References
- Spec:
.agents/knowledge/spec/clip-audio.md(annotools repository). - Gemini bills audio at 32 tokens per second:
.agents/knowledge/references/mllm-models.md(verified 2026-08-27).
Source code in src/annotools/audio.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |