Commit 6218b60
authored
recorder: pad odd source dimensions before libx264 encode (#246)
## 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
- [x] `go build ./...`
- [x] `go test ./lib/recorder/... -v` (existing tests pass, new
`TestFFmpegArgs_PadsOddDimensions` covers the args)
<!-- CURSOR_SUMMARY -->
---
> [!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.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
72b0e42. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
Co-authored-by: Sayan- <1415138+Sayan-@users.noreply.github.com>1 parent 08fbd6d commit 6218b60
2 files changed
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
71 | 86 | | |
72 | 87 | | |
73 | 88 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
498 | 498 | | |
499 | 499 | | |
500 | 500 | | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
501 | 505 | | |
502 | 506 | | |
503 | 507 | | |
| |||
0 commit comments