Skip to content

recorder: pad odd source dimensions before libx264 encode#246

Merged
Sayan- merged 1 commit into
mainfrom
hypeship/fix-odd-viewport-recording
May 21, 2026
Merged

recorder: pad odd source dimensions before libx264 encode#246
Sayan- merged 1 commit into
mainfrom
hypeship/fix-odd-viewport-recording

Conversation

@Sayan-
Copy link
Copy Markdown
Contributor

@Sayan- Sayan- commented May 21, 2026

Summary

libx264 with yuv420p requires both width and height to be divisible by 2 (4:2:0 chroma subsampling). When the captured display has an odd vertical (or horizontal) pixel count, ffmpeg fails to open the encoder with width/height not divisible by 2 and exits almost immediately — but Start() only waits 250ms for a startup error, so the API still returns 200 and the caller is left with a zero-byte mp4.

This change adds a -vf pad=ceil(iw/2)*2:ceil(ih/2)*2 filter so odd source dimensions are padded by a single pixel of black before encode. Even dimensions are unchanged.

Repro

$ ffmpeg -f lavfi -i color=red:size=641x481:rate=5 -t 1 -c:v libx264 -pix_fmt yuv420p -y out.mp4
[libx264] width not divisible by 2 (641x481)
[enc:libx264] Could not open encoder before EOF
$ ls -la out.mp4
-rw-r--r-- 1 user user 0 ... out.mp4

With the filter:

$ ffmpeg -f lavfi -i color=red:size=641x481:rate=5 -t 1 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -c:v libx264 -pix_fmt yuv420p -y out.mp4
$ ffprobe out.mp4
width=642 height=482

Test plan

  • go build ./...
  • go test ./lib/recorder/... -v (existing tests pass, new TestFFmpegArgs_PadsOddDimensions covers the args)

Note

Medium Risk
Touches ffmpeg encoding arguments for all recordings, which could subtly affect output dimensions/compatibility if the filter order or padding behavior differs across inputs/platforms.

Overview
Prevents recordings from failing on odd-sized displays by adding -vf pad=ceil(iw/2)*2:ceil(ih/2)*2 to the ffmpeg output args before libx264/yuv420p, ensuring width/height are even.

Adds TestFFmpegArgs_PadsOddDimensions to assert the new -vf filter is present in the generated argument list.

Reviewed by Cursor Bugbot for commit 72b0e42. Bugbot is set up for automated code reviews on this repo. Configure here.

libx264 with yuv420p requires both width and height to be divisible by 2
(4:2:0 chroma subsampling). When the X display has an odd vertical pixel
count, ffmpeg fails to open the encoder and exits almost immediately,
leaving a zero-byte mp4 even though the recording API returned 200.

Adding `-vf pad=ceil(iw/2)*2:ceil(ih/2)*2` pads odd dimensions by a single
pixel of black so the encoder always sees an even-sized frame.
@Sayan- Sayan- marked this pull request as ready for review May 21, 2026 16:10
@firetiger-agent
Copy link
Copy Markdown

Firetiger deploy monitoring skipped

This PR didn't match the auto-monitor filter configured on your GitHub connection:

Any PR that changes the kernel API. Monitor changes to API endpoints (packages/api/cmd/api/) and Temporal workflows (packages/api/lib/temporal) in the kernel repo

Reason: This PR modifies the recorder package to fix ffmpeg encoding, which does not change API endpoints (packages/api/cmd/api/) or Temporal workflows (packages/api/lib/temporal) as specified in the filter.

To monitor this PR anyway, reply with @firetiger monitor this.

@Sayan- Sayan- requested a review from hiroTamada May 21, 2026 16:12
@Sayan- Sayan- merged commit 6218b60 into main May 21, 2026
10 checks passed
@Sayan- Sayan- deleted the hypeship/fix-odd-viewport-recording branch May 21, 2026 16:39
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.

2 participants