Running ffmpeg on Serverless

Stem Shovel renders an MP3 of every recording a band gives it: stems for the player, takes from the Idea Recorder, and session recordings of practices and shows. The rendering is ffmpeg, the ffmpeg-static binary, running inside a Vercel function (its own function, for reasons we covered in One Fat Function Was Making Every Page Slow). That was uneventful until a band uploaded a whole show as one FLAC file, and we learned, one failed job at a time, what a serverless function changes about running ffmpeg. This is that list, with the numbers from our own timing runs rather than from memory.
The Disk Is Smaller Than the File
The show was a 650 MB FLAC, about 170 minutes of audio. The job that makes its MP3 downloaded the source to the function's temporary directory first, and failed there with ENOSPC: no space left on device, write. A Vercel function's /tmp holds about 500 MB. The row went back to failed, the page retried it on schedule, and it failed again the same way on every retry until we noticed.
The fix on paper is obvious: do not download it. ffmpeg reads URLs. Hand it the Blob URL and let it stream.
ffmpeg-static Cannot Read https
The bundled ffmpeg (johnvansickle's build, 7.0.2) lists https in ffmpeg -protocols, and ffmpeg -i https://....flac segfaults at once with exit code 139. It did so by the Blob URL and by the raw IP address, so the problem is in TLS rather than DNS, and it did so with and without -reconnect. We did not find the cause and we stopped looking, because curl ... | ffmpeg -i pipe:0 worked the first time.
So large sources are piped. Any source over a budget is read from Blob by Node and written into ffmpeg's standard input, and the job reads it as -i pipe:0:
/**
* A source over this size is not downloaded: each ffmpeg pass reads it
* from Blob on its stdin (`blobBody`), and only the MP3 touches the disk. A
* Vercel function's `/tmp` holds about 500 MB and the MP3 output needs room
* too (192 kbps is about 85 MB an hour); a 650 MB FLAC session recording
* failed with ENOSPC while it downloaded (2026-10-28).
*/
const SOURCE_DISK_BUDGET_BYTES = 256 * 1024 * 1024;
The budget is half the disk because the output needs the other half, and because a session recording is three passes, not one: a stats pass for the channel layout and peaks, a loudness measurement, and the encode. Each pass opens the Blob file afresh and pipes it again. Only the MP3 ever touches the disk. Below the budget nothing changed: the file is downloaded once and every pass reads it from disk, which is simpler and lets ffmpeg seek.
What a Pipe Takes Away
A pipe is not a file, and ffmpeg behaves differently on it in four ways that we had to handle one at a time.
It may not know the length. A piped FLAC still prints its Duration: line, because the STREAMINFO block at the front of the file carries the sample count. A piped WAV prints Duration: N/A, bitrate: 3072 kb/s. The leveling step needs the length to decide whether the file is short enough to measure, so for the WAV case the length is computed from the size over the bitrate. Raw PCM's bitrate is exact, so the estimate is too:
export function ffmpegBitrateDuration(stderr: string, bytes: number): number | null {
const m = /Duration:\s*N\/A,\s*bitrate:\s*(\d+(?:\.\d+)?)\s*kb\/s/.exec(stderr);
if (!m || Number(m[1]) <= 0) return null;
return (bytes * 8) / (Number(m[1]) * 1000);
}
It cannot seek, and ffmpeg may not tell you. An MP4 whose moov atom sits at the end of the file cannot be read from a pipe, because the index ffmpeg needs is behind the data. The surprise is what ffmpeg does about it: it exits 0 and writes an empty MP3. The job would have marked the row done and the band would have had a silent file. Passing -xerror on the streamed encode turns that demuxing error into a non-zero exit, so the row fails and is retried instead.
EPIPE is not an error. Some passes stop reading before the end of the input: a probe, or anything with -t. When ffmpeg closes its stdin early, the Node side that is still writing gets EPIPE, and that is the normal end of a successful run. We swallow it and destroy the source stream so the download stops too.
But a broken input is an error, and ffmpeg will not report it. If the Blob stream breaks mid-file, ffmpeg sees the end of its stdin, encodes what it got, and exits 0. From the exit code alone a truncated show is a finished show. So the runner listens for the input stream's own error and fails the run on it even when ffmpeg succeeded:
// A failure reading the input (the Blob stream breaking mid-file) fails the run even
// when ffmpeg, seeing the end of its stdin, exits 0 on what it got.
let inputError: Error | null = null;
The one piece of that handling that took thought was the ordering: an error on the input after ffmpeg has already closed its stdin is the EPIPE case and is fine; an error before that is the truncation case and is not.
Time is a budget too. The jobs function's maxDuration went from 300 to 800 seconds, the Pro plan's ceiling, and the per-run ffmpeg timeout from 240 to 600 seconds, so a stuck child is killed with time left to mark its row failed. For scale: that 170-minute FLAC encodes to MP3 in 90 seconds on our 16-core VM, and the function is slower than that, because ffmpeg's audio filters run on one thread.
Filters Have Wildly Different Prices
The function's time limit made us measure things we had never timed. The first surprise was in the Auto EQ, which needs a recording's long-term level in each of ten octave bands. The graph is the obvious one: fold to mono, asplit into ten branches, an octave-wide bandpass on each, and a measurement at the end of each branch.
With astats as the measurement and each branch mapped to a null output, the graph took 70 seconds on a 45-minute MP3. Folding to mono first brought it to 42 seconds. Ending each branch in anullsink instead of a mapped output brought it to 34 seconds. Replacing astats with volumedetect brought it to 7 seconds, of which 3 seconds is the decode.
volumedetect reports mean_volume, which is the branch's RMS in dBFS like the RMS level dB line of astats, rounded to a tenth of a dB and offset by about 3 dB. The offset is the same for every band, and a fit that aligns curves never sees it. The graph as shipped:
return [
`aformat=channel_layouts=mono,asplit=${bands.length + 1}${labels}[out]`,
...bands.map((f, i) => `[b${i}]bandpass=f=${f}:width_type=o:w=1,volumedetect,anullsink`),
].join(";");
One parsing note, since you will hit it: ffmpeg prints the Parsed_volumedetect_N summaries last to first, and prints a first set of empty instances (n_samples: 0) before the real ones. The parser sorts by N and lets a later line replace an earlier one.
Loudness: The Chain That Timed Out
The expensive one was leveling. Session recordings arrive at every level, and a phone's mono capture folded to both channels is +6 dB louder than it was, which exposed clipping that had been hiding on one side. We decided every session recording's MP3 should land at -14 LUFS integrated with a -1 dBTP true-peak ceiling, the same target the browser-side normalizer uses for exported mixes (Measuring Loudness in the Browser covers that side).
ffmpeg's loudnorm is the standard answer. It is two passes: a first over the whole file with print_format=json, which prints its measurement as a JSON block on stderr after the progress line, every value a number inside a string, and a second that passes those numbers back as measured_* and offset with linear=true, asking for one gain across the file. A silent or very short input prints -inf, which the parser must refuse. We built that, put a declipper in front of it for captures that had hit full scale, verified it on a clipped test file (83 clipped samples repaired, -14.3 LUFS out), and shipped it on October 26.
The same day it failed on a real show. The chain ran past the function's 300 seconds, and the page retried it on schedule with the same result each time. So we timed every filter on a 45-minute stereo file on one core of the VM:
loudnormmeasurement pass: 72 secondsloudnormapply pass with the MP3 encode: 82 secondsadeclipat its defaults: about 110 seconds, on top of the encode- a plain 192 kbps MP3 encode: 24 seconds
ebur128=peak=true: 12 secondsastats: under a second
loudnorm resamples to 192 kHz internally for its true-peak work, and that is where its cost goes. ebur128 measures the same integrated loudness and true peak for a sixth of the price. And adeclip's cost is in the autoregressive model it fits per window, so a shorter window, less overlap and a lower order are the levers: adeclip=window=30:overlap=50:arorder=4 runs in about 4 seconds and repairs a clipped file as well as the defaults, with no flat samples left and peaks rebuilt within 0.2 dB. (Overlap below 50 is rejected outright, if you were about to try.)
The chain that replaced it the same day: one ebur128 pass for the integrated loudness and true peak, one linear volume gain computed from them, and alimiter at -1 dBFS with a fast attack to catch what the gain or the declipper's rebuilt peaks put over. The gain is held back so that at most 3 dB of the loudest peaks go into the limiter; a peaky set stays quieter than the target rather than squashed:
const gain = Math.min(
target.integrated - m.integrated,
target.truePeak - m.truePeak + maxLimitDb,
);
Measurement and encode together now cost under a second per minute of audio on one core (about 0.26 and 0.52 seconds a minute respectively), so a 90-minute set fits the function with room to spare. A longer set, or one whose length is unknown, gets the limiter alone.
The Rules We Keep Now
Know the disk. The function's /tmp is about 500 MB, the output needs room, and a source that will not fit gets piped. The pipe costs you duration on headerless formats, seeking, and ffmpeg's honesty about broken input, and each of those needs its own line of code.
Never trust exit 0 from a piped run on its own. Pass -xerror, watch the input stream's own errors, and let EPIPE through.
Time your filters on a real file before you commit to a chain. Ten astats and ten volumedetect sinks give the same curve; one costs ten times the other. loudnorm and ebur128 give the same measurement; one costs six times the other. A function with a time limit is the first environment most of us have run ffmpeg in where that difference decides whether the job finishes at all.