Skip to content

feat: render WebP on request, a fifth the size of the PNG - #3

Merged
rainhead merged 3 commits into
ffmpeg-onlyfrom
shrink-images
Oct 21, 2026
Merged

rainhead merged 3 commits into
ffmpeg-onlyfrom
shrink-images

Conversation

@rainhead

@rainhead rainhead commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

The bout page loads every spectrogram tile it scrolls past, and each tile is drawn at a fraction of its pixel size: a 10 s tile is 1877 px wide and the timeline shows it at 267 px by default. The 541 tiles of one bout come to about 420 MB of PNG. This lets the caller ask for WebP instead, which is about a fifth of the size at no visible cost.

What changes

parameters.format is png (the default, so nothing changes for a caller that does not ask) or webp, and parameters.quality sets WebP's quality, with 100 meaning lossless. The image is stored with the matching Content-Type and the response reports it as content_type. The caller picks the key's extension, since the function writes to whatever key it is given; the orcasite side is orcasound/orcasite#1086.

On the production segment in events/spectrogram_job.json, rendered at Lambda's CPU share:

format quality size encode time
png 552 KB 0.90 s
webp 80 113 KB 0.88 s
webp 100 (lossless) 328 KB 1.20 s

Left is the PNG, right the WebP at quality 80. The top row is each at the size the timeline draws a tile; the bottom row is 160×120 px from the middle of each at 3×, where the WebP's smoothing of the noise floor shows.

PNG and WebP tiles at display size and magnified

What I ruled out

A 256-colour paletted PNG, through ffmpeg's palettegen and paletteuse, was 349 KB but shifted some pixels by one level, so it is neither lossless nor as small as lossless WebP. Lossless WebP decodes pixel-identical to the PNG. libwebp's slowest effort level saved another 8% for a tenth of a second more per tile, which is not worth a knob.

Verification

tests/smoke.py now renders the synthetic clip as PNG, WebP and lossless WebP inside the built image as Lambda runs it, checks each file's header for its type and dimensions, and checks that the stored and reported content types agree. On pink noise the lossy and lossless WebPs come out about the same size, since noise is the worst case for a lossy encoder, so the test only requires both to beat the PNG. I also rendered the real segment through the handler in all three formats and confirmed an unknown format is refused with a clear error.

WebP sides are at most 16,383 px, which at the default hop length is a clip of about 87 s. A longer clip asked for as WebP is refused before rendering, which I checked on a 90 s clip; the same clip renders as PNG. CodeRabbit's local review raised this; its other suggestion, to fall back to PNG for such a clip, I declined because the caller has already named a .webp key.

This stacks on #2 and should merge after it. Merging deploys, and orcasite keeps getting PNGs until it asks for WebP.

🤖 Generated with Claude Code

rainhead and others added 3 commits September 29, 2026 16:06
The Python stack (librosa, numba, OpenCV, matplotlib) is replaced by one
ffmpeg command: showspectrumpic draws the spectrogram as intensity and
pseudocolor applies matplotlib's viridis, which showspectrumpic's own
palette does not match. The image is sized from the decoded sample count
so tiles keep the archive's width, and the dB window is converted to
ffmpeg's full-scale reference by an offset fitted against a production
image. On the same segment the result has production's luminance
distribution to within 0.01 and the same width.

The request now carries presigned GET and PUT URLs and explicit render
parameters, echoed back in the response, so the function knows nothing
about S3. The previous bucket/key shape still works, via boto3 from the
base image, until orcasite has switched.

Image 2.27 GB -> 773 MB, no caches to seed, render 2 s at 1024 MB.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
A spectrogram tile is drawn at a fraction of its pixel size in orcasite's timeline, and the browser downloads every tile it scrolls past, so the archive's RGB PNGs cost far more than they show. The caller can now ask for WebP with `format` and set `quality`, where 100 is lossless. On a production segment, at Lambda's CPU share: PNG 552 KB in 0.90 s, WebP at quality 80 113 KB in 0.88 s, lossless WebP 328 KB in 1.20 s. PNG stays the default so nothing changes for callers that do not ask.

The image is stored with the matching Content-Type and the response reports it. The smoke test renders all three.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
WebP sides are at most 16,383 px, which a clip of about 87 seconds reaches at the default hop length. Say so before rendering instead of letting ffmpeg fail obscurely.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@rainhead
rainhead added this pull request to stack #4 October 21, 2026 19:18
@rainhead rainhead changed the title shrink images feat: render WebP on request, a fifth the size of the PNG Oct 21, 2026
@rainhead
rainhead merged commit 99b66c1 into main Oct 21, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant