Troubleshooting of FFmpeg

From LemonWiki共筆
Jump to navigation Jump to search

Troubleshooting of FFmpeg issues


icon_scale_pencil.png This article "Troubleshooting of FFmpeg" is still being written. If there are any incomplete parts, you are welcome to directly edit them. 這篇文章「Troubleshooting of FFmpeg」內容還在撰寫中,如果有不完整的部分,歡迎你直接動手修改


Owl icon.jpg What is FFmpeg? FFmpeg is a leading multimedia framework that supports decoding, encoding, transcoding, streaming, filtering, and playing virtually any multimedia format, from old and obscure to cutting-edge. It's highly portable and runs across various operating systems, including Linux, macOS, Windows, and more. Detail on About FFmpeg

Troubleshooting of FFmpeg[edit]

File Overwrite Issue When Integrating FFmpeg with PHP[edit]

Error condition: When integrating FFmpeg commands with PHP scripts, if the output file already exists, FFmpeg displays an overwrite confirmation prompt. However, this prompt is not visible in the PHP execution environment. This causes the PHP script to pause, waiting for user input, without any visible prompt.

Original command:

ffmpeg -i input.mp4 -vf scale=720:404 -c:v libx264 -crf 23 -preset fast -c:a aac -b:a 128k output.mp4

Prompt in terminal environment:

File 'output.mp4' already exists. Overwrite? [y/N]

Solution: Add parameters to the FFmpeg command to control file overwrite behavior:

First option: Auto-overwrite option:

ffmpeg -y -i input.mp4 -vf scale=720:404 -c:v libx264 -crf 23 -preset fast -c:a aac -b:a 128k output.mp4

Second option: Skip if file exists option:

ffmpeg -n -i input.mp4 -vf scale=720:404 -c:v libx264 -crf 23 -preset fast -c:a aac -b:a 128k output.mp4

Or implement the file overwrite control directly in your PHP script logic.

FFmpeg Fails to Decode Large JPEG Images[edit]

Error condition: When processing high-resolution JPEG images, FFmpeg reports the image size as invalid and aborts decoding entirely.

Original command:

ffmpeg -i "image.jpg" -vf scale=2000:-1 output.jpg

Error output:

[mjpeg @ 0x...] [IMGUTILS @ 0x...] Picture size 25600x14400 is invalid
[image2 @ 0x...] Could not find codec parameters for stream 0: unspecified size
Conversion failed!

Root cause: FFmpeg's internal function `av_image_check_size2()` (in `libavutil/imgutils.c`) validates image dimensions before decoding begins — before the pixel format (`pix_fmt`) is known. Without `pix_fmt`, FFmpeg assumes the worst-case bytes-per-pixel (bpp = 6), then checks:

stride * (height + 128) >= INT_MAX   // INT_MAX = 2,147,483,647

For a 25600×14400 image with bpp = 6:

stride = 25600 × 6 = 153,600
result = 153,600 × (14400 + 128) = 2,231,500,800  →  exceeds INT_MAX ✗

The actual pixel format of the image is `yuvj420p` (bpp = 1.5), which would pass the check:

stride = 25600 × 1.5 = 38,400
result = 38,400 × (14400 + 128) = 557,875,200  →  within INT_MAX ✓

FFmpeg cannot read `pix_fmt` before running the size check, so the image is incorrectly rejected. Passing `-vcodec mjpeg` or `-pix_fmt` does not help — the check runs before any codec parameters take effect.

Solution:

Use ImageMagick instead of FFmpeg for large JPEG files:

magick "image.jpg" -resize 2000x2000\> "output.jpg"

The `\>` flag shrinks only — no enlargement if the image is already smaller than the target.

Or use Pillow to pre-scale before passing to FFmpeg:

python3 -c "
from PIL import Image
Image.MAX_IMAGE_PIXELS = None
img = Image.open('image.jpg')
img.thumbnail((4000, 4000))
img.save('temp.jpg', quality=90)
" && ffmpeg -i temp.jpg -vf scale=2000:-1 output.jpg

Note: `Image.MAX_IMAGE_PIXELS = None` disables Pillow's decompression bomb protection. Only use this for trusted image sources.[1][2]

Further readings[edit]

References[edit]