---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://framesfield.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
title: "Fix FFmpeg Width or Height Not Divisible by 2 Crash"
description: "Replace scale=W:-1 with scale=W:-2 whenever resizing by one axis. For unscaled odd-dimension PNGs or screen captures, use pad=ceil(iw/2)*2:ceil(ih/2)*2:0:0:black (pixel-sharp UI) or scale=trunc(iw/2)*2:trunc(ih/2)*2,setsar=1 before -pix_fmt yuv420p."
canonical: "https://framesfield.com/fixes/ffmpeg-odd-width-height-not-divisible-by-2-yuv420p"
---

# Fix FFmpeg Width or Height Not Divisible by 2 Crash

> Replace scale=W:-1 with scale=W:-2 whenever resizing by one axis. For unscaled odd-dimension PNGs or screen captures, use pad=ceil(iw/2)*2:ceil(ih/2)*2:0:0:black (pixel-sharp UI) or scale=trunc(iw/2)*2:trunc(ih/2)*2,setsar=1 before -pix_fmt yuv420p.

- **Canonical URL:** https://framesfield.com/fixes/ffmpeg-odd-width-height-not-divisible-by-2-yuv420p
- **Category:** FFmpeg & Delivery
- **Error Signature:** `[libx264 @ 0x...] width not divisible by 2 (1081x1920) | height not divisible by 2 (1280x719) | Error while opening encoder for output stream #0:0`
- **Last Verified:** 2026-12-01

## Observed Symptom

Encoding an AI-generated image sequence, cropped screen recording, or aspect-ratio scaled clip (`-vf scale=1080:-1`) with `libx264` or `libx265` and `-pix_fmt yuv420p` aborts immediately before frame 1 with a width or height not divisible by 2 error.

## Technical Root Cause

Standard 4:2:0 chroma subsampling (`yuv420p`) stores one shared color sample for every 2x2 block of luma pixels, making odd pixel dimensions mathematically illegal for H.264 and H.265 encoders—a limit triggered constantly by `scale=W:-1` math or hand-dragged macOS screen selections.

## Under the Hood

In `yuv420p` video (required by Apple Safari, QuickTime, iOS, YouTube, Instagram, and hardware decoders), the luminance channel (`Y`) sits at full resolution `W × H`, while both chrominance planes (`U` and `V`) are stored at exactly half width and half height (`W/2 × H/2`). If either `W` or `H` is an odd integer—such as `1439×899` from a macOS marquee screen capture or `1080×1351` from an odd aspect-ratio calculation—`W/2` cannot resolve to an integer chroma grid, so `libx264` and `libx265` immediately refuse to open the encoder.

The single most common self-inflicted trigger is using `-1` inside FFmpeg’s `scale` filter (for instance `-vf scale=1280:-1` or `-vf scale=-1:1080`). The `-1` placeholder instructs FFmpeg to compute the matching dimension purely from aspect ratio without checking parity; scaling a `1920×1079` or `1366×768` capture down to `1080` wide can easily yield an odd height like `607` or `1919`. Replacing `-1` with `-2` tells FFmpeg to preserve aspect ratio while automatically rounding to the nearest even integer divisible by 2.

When you are NOT resizing the video—only fixing an already-odd source canvas such as a crisp SaaS UI recording or Figma PNG—choosing the right parity operator matters for visual sharpness. Using `scale=trunc(iw/2)*2:trunc(ih/2)*2` resamples the entire frame by 1 pixel, which slightly softens 1 px UI text and borders. For razor-sharp UI captures and brand end-cards, `pad=ceil(iw/2)*2:ceil(ih/2)*2:0:0:black` (adding a single 1 px border) or `crop=floor(iw/2)*2:floor(ih/2)*2:0:0` (trimming 1 px off the edge) satisfies `yuv420p` with zero pixel resampling.

## Before vs. After

### Broken (Broken Commands (scale=-1 Yields Odd Height or Unpadded Odd Screen Capture))

```
# Fails whenever computed height or raw PNG dimension is odd (e.g. 1280x719 or 1439x899):
ffmpeg -i raw_capture.mov -vf "scale=1280:-1" -c:v libx264 -pix_fmt yuv420p broken.mp4
# [libx264] height not divisible by 2 (1280x719)
```

### Verified Fix (Verified Even-Parity Commands (scale=-2 for Resize, pad ceil for Pixel-Sharp UI))

```
# 1. Aspect-safe resize to 1280px wide with automatic even height:
ffmpeg -y -i raw_capture.mov -vf "scale=1280:-2,setsar=1" \
  -c:v libx264 -crf 18 -pix_fmt yuv420p -movflags +faststart resized_even.mp4

# 2. Zero-blur 1px pad for odd UI captures or Figma PNGs (preserves 1:1 sharpness):
ffmpeg -y -i odd_ui_1439x899.mov -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2:0:0:black,setsar=1" \
  -c:v libx264 -crf 18 -pix_fmt yuv420p -movflags +faststart sharp_even.mp4
```

Using `-2` in `scale` guarantees even dimensions during resizing, while `pad=ceil(iw/2)*2:ceil(ih/2)*2` fixes odd source frames without resampling sharp UI pixels or altering aspect geometry.

## Copy-Paste Fix (fix-even-dimensions.sh)

```bash
# 1. Inspect source dimensions first to spot odd width or height
ffprobe -v error -select_streams v:0 -show_entries stream=width,height,pix_fmt -of csv=p=0 input.mp4

# 2. Option A (Scaling by width or height): ALWAYS use -2 instead of -1
ffmpeg -y -i input.mp4 \
  -vf "scale=1080:-2,setsar=1" \
  -c:v libx264 -profile:v high -level 4.1 -crf 18 -pix_fmt yuv420p \
  -movflags +faststart -c:a aac -b:a 192k -ar 48000 out_scaled_even.mp4

# 3. Option B (UI / Screen Recordings / Sharp Typography — Zero Resampling Blur):
# Adds at most 1px of clean padding on right/bottom edge only when odd
ffmpeg -y -i odd_screen_capture.mov \
  -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2:0:0:black,setsar=1" \
  -c:v libx264 -profile:v high -level 4.1 -crf 18 -pix_fmt yuv420p \
  -movflags +faststart -c:a aac -b:a 192k -ar 48000 out_padded_sharp.mp4

# 4. Option C (Exact Canvas Fit + Safe Truncation for Mixed AI Video Batches):
ffmpeg -y -i odd_ai_clip.mp4 \
  -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2,setsar=1" \
  -c:v libx264 -profile:v high -level 4.1 -crf 18 -pix_fmt yuv420p \
  -movflags +faststart -c:a copy out_truncated_even.mp4
```

## Step-by-Step Resolution

1. **Identify whether the odd dimension comes from the source file or a scale=-1 filter:** Run ffprobe to read source width and height. Even if the source file is even (like 1920x1080), a downstream crop or scale=750:-1 expression can generate an odd intermediate dimension.
2. **Choose -2 for scaling, pad ceil for pixel-sharp UI, or crop floor for edge-to-edge video:** Use scale=W:-2 whenever resizing, pad=ceil(iw/2)*2:ceil(ih/2)*2:0:0:black for crisp SaaS captures where 1px resampling blur is unacceptable, or crop=floor(iw/2)*2:floor(ih/2)*2:0:0 when a 1px black border would be visible on white pages.
3. **Append setsar=1 and confirm even dimensions with ffprobe:** Adding setsar=1 after scale or pad prevents non-square pixel aspect ratios (like 1081:1080) from triggering subtle stretching in web players, then verify both numbers divide cleanly by 2.

## Verification Check (Confirm Output Width and Height Are Both Even Integers With yuv420p)

```bash
ffprobe -v error -select_streams v:0 -show_entries stream=width,height,sample_aspect_ratio,pix_fmt -of csv=p=0 out_padded_sharp.mp4
```

**Expected healthy output:**

```text
1440,900,1:1,yuv420p   # Both width & height divisible by 2, SAR 1:1, yuv420p
```

## Prevention Guardrails

1. **Ban scale=W:-1 and scale=-1:H across all team FFmpeg scripts:** Always default to -2 (or -4 / -8 for strict codec macroblock alignment) so aspect-preserving batch scripts never crash on unexpected user uploads or odd AI crops.
2. **Place the even-parity filter at the end of your video filtergraph:** If you crop, rotate (transpose), or overlay after scaling, put pad or scale=trunc(iw/2)*2:trunc(ih/2)*2 as the final geometry stage right before format=yuv420p.
3. **Match pad background color to your UI canvas on light-mode demos:** When padding an odd light-mode product capture for embedding on a white landing page, use pad=ceil(iw/2)*2:ceil(ih/2)*2:0:0:#FFFFFF (or crop 1px) so a 1px black hairline never appears along the right edge.

## FAQs

### Why does scale=trunc(iw/2)*2:trunc(ih/2)*2 work without crashing while scale=1080:-1 fails?

trunc(iw/2)*2 divides the input width by 2, drops any .5 remainder, and multiplies back by 2—guaranteeing an even integer on every input. By contrast, -1 computes exact mathematical aspect ratio height without forcing divisibility by 2.

### Should I use pad, crop, or scale to fix a 1-pixel odd screen recording?

Use pad=ceil(iw/2)*2:ceil(ih/2)*2 (with matching canvas color) or crop=floor(iw/2)*2:floor(ih/2)*2 for UI demos and typography because both keep original pixels 1:1 sharp. Reserve scale=trunc(iw/2)*2:trunc(ih/2)*2 for photographic or AI footage where a 1px resample is invisible.

### Why did my video still fail even though I wrote scale=1080:-2?

Check whether a subsequent filter—such as crop, pad, rotate, or an overlay input—modified dimensions afterward, or whether you passed a single odd fixed axis (like scale=1081:-2). Both final width and final height entering libx264 must be even.

### Do H.264 exports work better if dimensions are divisible by 16 instead of just 2?

H.264 uses 16x16 macroblocks internally, so multiples of 16 (such as 1280x720 or 1920x1088) offer marginally higher compression efficiency, though libx264 handles any mod-2 resolution (including standard 1920x1080) cleanly using hidden bitstream crop flags.

### Why did FFmpeg allow odd dimensions when I omitted -pix_fmt yuv420p?

Without -pix_fmt yuv420p, RGB PNG inputs cause libx264 to switch silently into yuv444p (High 4:4:4 Predictive), which does not subsample chroma and therefore permits odd dimensions—at the cost of breaking playback completely in Safari, QuickTime, and iOS.
