Adding click and typing sounds after the recording, on the right frame, with ffmpeg

Short answer: don't hand ffmpeg one input and one adelay per click. Render every hit into a single WAV that runs on the recording's own timeline, then treat that WAV exactly like the microphone track: same trim, same atempo chain, then amix with normalize=0 so nothing gets quieter. On a real 34 second take with 57 clicks and key presses, every hit landed within a frame of the press, plain and sped up to 1.5x.

This is how the Sounds feature in Framok, my screen recorder for Windows, works under the hood. It shipped in 0.5.0 on 19 Sep. Here's the pipeline, the bit of the test that lied to me, and the one rule that kept the app's own sounds out of the recording.

The problem

The recorder already logs every mouse press, key press and scroll notch while you record, with a time in milliseconds, because the automatic zoom needs them. So the job sounded easy: play a click at each press time.

Two things make it harder than that:

  • The export isn't the recording. People trim the start and end and speed the whole take up. A click at 12.400 s in the recording might be at 6.933 s in the export, or not in it at all.
  • There can be a lot of hits. My 34 second test take had 57. A ten minute tutorial with typing could have thousands.

Why not one adelay per click

The usual answer to "ffmpeg add sound effect at a specific time" is one input per sound, delayed into place and mixed:

ffmpeg -i video.mp4 -i click.wav -i click.wav \
  -filter_complex "[1:a]adelay=1200|1200[c1];[2:a]adelay=3450|3450[c2];[0:a][c1][c2]amix=inputs=3[a]" \
  -map 0:v -map "[a]" -c:v copy out.mp4

adelay takes a delay in milliseconds for each channel, separated by | (ffmpeg docs), so every sound is its own input with its own delay.

That's fine for three sounds. For a few thousand it falls apart:

  • The command gets huge. A command line on Windows tops out at 32,767 characters (Microsoft Learn), and a few thousand inputs and delay chains go straight past that. ffmpeg 7 can read the filter graph from a file with -/filter_complex graph.txt (ffmpeg docs), but that's still thousands of inputs for ffmpeg to open.
  • amix turns everything down. Its normalize option is on by default, which scales the inputs instead of just adding them (ffmpeg docs). The more inputs, the quieter each one, your voice included. Turning it off with normalize=0 just adds them, and the docs warn that can clip if the inputs aren't levelled.
  • Trims and speed changes have to be done to every delay by hand, and any rounding shows up as clicks drifting off their frames.

Step 1: one schedule, used everywhere

The editor builds a list of hits from the logged events: time, which sample, gain and playback rate. The same list drives the live preview in the editor and the export, so what you hear while scrubbing is what lands in the file.

It's built in source time (milliseconds into the original recording, before any trim or speed), which turns out to be the whole trick. More on that in step 3.

The rules that make typing sound like a person:

  • Speed decides the sample. Key presses under 90 ms apart get the shortest half of the set, a little quieter, with no key release sounds. Over 250 ms apart they get the fuller hits.
  • Every hit is nudged. Up to 4 percent in pitch and 1.5 dB in gain, so a burst never sounds like a machine gun.
  • Never the same sample twice in a row from the same pool.
  • Space, Enter and Backspace get their own sounds where the keyboard has them.

The random nudges come from a small seeded generator (mulberry32) rather than Math.random(), so the preview, the export and the tests all build exactly the same list. That made the timing test below possible.

Step 2: render it offline, in pieces

The schedule is rendered with the browser's Web Audio API, in an OfflineAudioContext rather than a live one. An offline context doesn't play to your speakers; it "generates it, as fast as it can, and outputs the result to an AudioBuffer" (MDN). So the render doesn't wait for real time, and the hits are scheduled by the same code that plays them live in the editor.

Each hit is an AudioBufferSourceNode started at its time, with the pitch nudge set through playbackRate. start(when) takes the time in seconds on the context's own clock (MDN), and changing playbackRate shifts the pitch because "no pitch correction is applied on the output" (MDN). MDN calls source nodes "very inexpensive to create" and the decoded samples can be reused, so one node per hit is fine.

Two details that mattered:

  • One minute at a time. A long take would need the whole track in memory at once, so it renders in 60 second pieces at 48 kHz mono, each one starting 1.5 s early so the tail of a hit just before the cut carries over. Each piece is converted to 16 bit samples and streamed to a WAV file on disk, and the 44 byte header gets its final length patched in at the end.
  • The WAV is as long as the recording. Not as long as the export. It sits on the same timeline as the mic and system audio files, silence included.

Step 3: mix it like the mic

Because the WAV is on the source timeline, the export doesn't need to know anything about clicks. It does to the sounds file exactly what it already did to the microphone file: trim it with -ss and -to on the input, speed it up with atempo, set its volume, and mix. For a 34 second take with system audio (it lives in the screen recording), mic and sounds, trimmed 2 s in and played at 1.5x, the command comes out like this, with video.mp4 being the rendered picture with no audio yet:

ffmpeg -i video.mp4 \
  -ss 2 -to 34 -i screen.webm \
  -ss 2 -to 34 -i mic.webm \
  -ss 2 -to 34 -i sounds.wav \
  -filter_complex "[1:a]atempo=1.5000,volume=1[a0];[2:a]atempo=1.5000,volume=1[a1];[3:a]atempo=1.5000,volume=1[a2];[a0][a1][a2]amix=inputs=3:duration=longest:normalize=0[aout]" \
  -map 0:v:0 -map "[aout]" -c:v copy -c:a aac -b:a 192k -shortest out.mp4

A few notes on the pieces. -ss and -to before an -i trim that input as it's read, so all three audio files start at the same moment. atempo takes 0.5 to 100 per instance, and the docs suggest chaining instances when you need more (ffmpeg docs), so a take slowed to 0.25x gets atempo=0.5,atempo=0.5. And normalize=0 on amix keeps every track at the volume the user set.

The win is that there's one code path. If a trim or a speed change is right for the voice, it's right for the clicks, because they go through the same three filters. Nothing gets recalculated per hit.

Step 4: prove it

"Sounds about right" wasn't good enough, so there's a test that exports a real take and measures where the sounds actually landed:

  1. Export with system audio turned off, so only the added sounds are in the file.
  2. Decode the export's audio to mono 48 kHz and turn it into a loudness envelope, one value per 5 ms.
  3. For every click and key in the schedule, work out where it should be after the trim and speed (source time minus the trim, divided by the speed), then look for an onset near there: a 5 ms window at least 2.5 times louder than the quietest of the three before it.
  4. At normal speed the window is 16 ms either side, one frame at 60 fps. Sped up it widens to 30 ms, because atempo smears the start of a sound by a few milliseconds. That's still under a frame at 30 fps.

The result on my 34 second take, exported twice (plain, and trimmed 2 s with 1.5x speed): 57 hits measured each time, none off by more than a frame.

The false alarm at 1.5x

The first run of the sped up check failed on about a dozen hits. I assumed the tempo chain was drifting and went looking for a timing bug.

There wasn't one. Every "miss" was a hit that couldn't be heard on its own: a click sitting inside the zoom whoosh, or a key landing in the tail of the key before it. An onset detector can't find the start of a sound that starts while another one is still ringing. The sounds were in the right place; the test was measuring something it couldn't see.

The fix was in the test, not the app. A hit now counts as masked, and is skipped, if it lands within 80 ms of the previous hit, while the previous sample is still sounding, or within half a second of a zoom whoosh. Those gaps are measured in source time, so speeding the take up doesn't change which hits get checked. The same reason means the check uses a short click style and a mechanical keyboard: a typewriter's long ring masks most of a fast typing burst, and the run ends with too few hits to judge.

Keeping the app's own sounds out

Just before Sounds, Framok got three interface sounds of its own: a cue when the countdown starts, another when recording stops, and a chime when an export finishes. With system audio on, a recorder records whatever the PC plays, its own sounds included.

The rule: the start cue plays during the countdown, and capture waits for it to finish plus 600 ms before it begins. The stop cue only plays once every capture track has closed.

The 600 ms is there because a test failed. I recorded two 4 second takes with system audio on and checked the captured audio in 100 ms windows. The first version, without the margin, had the tail of the start cue at full scale in the first 100 ms. With the margin, every window was silent.

What I'd tell anyone doing this

  • Render first, mix second. One WAV on the source timeline turns thousands of timed sounds into one more audio track.
  • Put everything through the same trim and tempo chain. Clicks drift when they're on a different code path from the audio they sit next to.
  • Seed your randomness if a test has to agree with the output.
  • Know what your test can't hear. Masked hits look exactly like timing bugs.

The feature itself

In the editor it's one switch. Turn on Add sounds and Framok lays a click on every press, a key on every keystroke, a soft tick per scroll notch and a whoosh as the camera zooms. There are five click styles and four real keyboards plus Soft, each with its own volume, and the recorder keeps the timing and the kind of key, never what you typed.

The Framok editor's Sounds section: an Add sounds switch, five click styles (Soft, Mouse, Mechanical, Chunky, Wooden) and a click volume slider at 80 percent, next to a support inbox being typed into

Where Framok doesn't help

  • Only recordings made in Framok. The sounds come from the events it logged while recording, so it can't add them to a video from OBS or your phone.
  • Built in sounds only. You can't load your own sample files yet.
  • Windows only. Windows 10 or 11, 64 bit. A Mac version has a waitlist at the bottom of framok.com.
  • No free trial on the download from framok.com. A 7 day trial comes with the Microsoft Store version, which isn't out yet, and paid licences have a 14 day money back guarantee.
  • The installer isn't code signed yet, so SmartScreen warns the first time. Click More info, then Run anyway.

Framok is $29 once for the life of the product, or $5 a month and cancel any time. Both cover 2 of your own PCs.

See Framok

Has anyone found a cleaner way to line sounds up with ffmpeg than rendering them first? Keen to hear it if so.

Sources

More from the blog

All posts RSS feed