weirdnet-media
Media processing for Weirdnet clients and nodes: the audio and video work apps do before sending
media to a model and after getting speech back. Pure Rust where it can be (WAV, PCM, resampling,
chunking, plans, subtitles), and a hardened ffmpeg / ffprobe runner for everything else, with a
fake so tests never need ffmpeg. Part of Weirdnet by
Weird Brains.
There are no defaults that stand in for a model's limits. Plans take what the model reports
(audio_max_seconds, max_request_bytes, max_input_chars, frame counts and sizes) and refuse,
naming the missing setting, when it is absent. Nothing is fetched: inputs are files the caller
already chose or bytes it holds.
Audio
probe:wav_header/wav_fileread WAV headers in Rust (PCM 8/16/24/32-bit, float 32/64, extensible headers, streamed sizes);probe::probereads a WAV in Rust and anything else throughffprobeinto aMediaInfo(container, duration, first video and audio stream).pcm:Pcm(interleaved 16-bit),decode_wav,encode_wav,normalize_wav(16 kHz mono),Pcm::to_mono,Pcm::resample(windowed sinc, low-passed going down),Pcm::slice_ms,crossfade(equal power),concatwithJoin::{Cut, Crossfade, Pause}(every piece must share rate and channels),tonefor fixtures.vad:silence_cuts(pcm, max_ms): 30 ms RMS frames, each cut at the quietest frame in the last fifth of its window, no overlap;frame_rmsandcuts_from_rmsfor energy measured elsewhere.transcribe:plan(pcm, &TranscribeLimits)sends a clip whole when it fits, cuts it at silences when it is longer thanaudio_max_seconds(narrowed bymax_request_bytes), and refuses a clip over the request limit when noaudio_max_secondsis known.prompt_fromgives the next piece's prompt (the last 200 characters).realignshifts each piece's segments onto the clip; a piece without segments becomes onecoarsesegment.speak:TextPlansplits narration or a script (SpeechText::ScriptofScriptTurns) into pieces that fitmax_input_chars: paragraph, then sentence, then clause, then word, never inside a word, never mixing speakers. Withoutmax_input_charsit refuses text over 4 000 characters.TextPlan::joinsgives a 10 ms crossfade inside a paragraph and a 300 ms pause between paragraphs and turns, forpcm::concat.subtitles:srtandvttfrom segments (coarse cues get aNOTEin WebVTT).audio:normalize/normalize_pcm(WAV in Rust, anything else through ffmpeg),extract(the first audio stream of an audio or video file,-vn, a range, bounded output) andencodetoAudioFormat::{Wav, Mp3, Opus, Flac}.waveform:Waveform, an immutable interleavedf32buffer whose clones share the samples (so it can be handed to an audio thread), for editing and mixing wherePcm's 16 bits would round every step. Gain (linear and dB), fades (FadeCurve::{Linear, EqualPower}from weirdnet-dsp), peak and RMS normalise, a peak ceiling (gain reduction, not clipping), constant-power pan, mono and stereo, slicing, andmixofMixSources with a gain and a start offset (capped at 20 minutes at 48 kHz).from_pcmandto_pcmconvert to and fromPcm; everyi16value survives the round trip.from_wavreads 8, 16, 24 and 32-bit integer and 32 and 64-bit float WAV (extensible headers, any chunk order, streamed sizes, never allocating from a header's claim);to_wav(WavFormat)writes 16-bit, 24-bit or 32-bit float.
Video
video::FrameSource:probe,frame_at(ms, w, h),scenes(threshold),keyframes(),audio(start, len).FfmpegSourceimplements it over anyExec;FakeSourcegenerates frames for tests; phone decoders can implement it too.plan_frames(Mode, range, n, &Marks)withMode::{Uniform, Scene, Hybrid, Keyframes, Every { ms }}: sorted, unique, inside the range, never more thann.extract_frames(source, &FrameRequest)(count, longest edge, mode, range, byte bound),thumbnail,soundtrack,clip(stream copy to fragmented MP4, Matroska or WebM, bounded),fit,label,clip_range,parse_scenes,parse_keyframes,is_video_path.
The hardened runner
ffmpeg::Runner runs one Job at a time (an Input, options before and after it, an output
bound) and never through a shell:
-nostdin -hide_banner -loglevel error -protocol_whitelist file,pipeand a demuxer allowlist (-format_whitelist: WAV, MP3, FLAC, Ogg, AAC, MP4/MOV, Matroska/WebM, AVI). Playlists,concat, image sequences andlavfiare refused, and so are URLs and protocol prefixes in any job option.- Input only as
file:<canonical path>(a regular file underLimits::max_input_bytes) or bytes onpipe:0; output only topipe:1, read with a bound, the child killed past it. Limits: wall time (120 s), address space on Unix (1 GiB), niceness (10), decoder threads (2), and on Linux a new network namespace when unprivileged user namespaces allow it.- stderr is kept only on failure, truncated to 2 KiB. Nothing is logged.
PrivateDir: a 0700 temp dir removed on drop.
ffmpeg::Exec is the seam. FakeRunner validates jobs the same way, records them, and answers
from rules or a FakeMedia description (probe JSON, frames, WAV of the right length, scenes,
keyframes). Binaries::locate(Some("YOUR_APP_FFMPEG")) finds ffmpeg from an environment variable,
beside the executable (or a macOS bundle's Helpers), then PATH.
Example
[]
= "0.2"
use audio;
use ;
use srt;
use ;
let runner = locate?;
let pcm = normalize_pcm?;
let plan = plan?;
let mut results = Vecnew;
let mut prompt = Stringnew;
for i in 0..plan.pieces.len
let transcript = realign?;
write?;
Long narration:
use ;
use TextPlan;
let plan = text?;
let pieces = plan.pieces.iter.map.?;
let audio = concat?;
write?;
Mixing without losing level, then writing a 32-bit float WAV:
use ;
let kick = tone?.fade_out;
let hat = tone?;
let bar = mix?;
write?;
Frames for a vision model:
use ;
use ;
let runner = locate?;
let source = new;
// count and edge come from the model's reported limits
let = extract_frames?;
Testing
cargo test runs everything against FakeRunner and generated WAV fixtures. The tests in
tests/real_ffmpeg.rs run when ffmpeg and ffprobe are installed (or WEIRDNET_MEDIA_FFMPEG
names an ffmpeg binary with ffprobe beside it) and return early otherwise. They include hostile
files: a text file named .wav, an ffconcat list, an HLS playlist pointing at a URL, an SDP
file, a truncated FLAC, and a long clip under a short output bound and time limit.
License
BSD-3-Clause, Copyright (c) 2026, Weird Brains LLC.