Extract Color Accurate PNG Frames Using FFmpeg
This article provides a straightforward guide on how to extract a single frame from a video as a PNG image using FFmpeg while preserving exact color accuracy. You will learn the specific command-line arguments and video filters needed to prevent color shifting, gamma issues, and color space mismatches during the YUV-to-RGB conversion process.
When converting video frames (which are typically stored in YUV color space) to PNG images (which use the RGB color space), FFmpeg can sometimes apply incorrect color matrices. This results in washed-out colors, crushed blacks, or subtle color shifts. To maintain perfect color accuracy, you must explicitly define the color space conversion parameters.
The Color-Accurate Command
To extract a highly accurate PNG frame from an HD video (which typically uses the BT.709 color standard), use the following command:
ffmpeg -ss 00:01:30 -i input.mp4 -vf "scale=in_color_matrix=auto:out_color_matrix=bt709" -pix_fmt rgb24 -vframes 1 output.pngParameter Breakdown
-ss 00:01:30: Seeks to the specific timestamp (1 minute and 30 seconds) from which you want to extract the frame. Placing this before the input file (-i) ensures fast seeking.-i input.mp4: Specifies your source video file.-vf "scale=in_color_matrix=auto:out_color_matrix=bt709": This is the crucial filter for color accuracy. It forces FFmpeg to read the input color matrix automatically and accurately convert it to the BT.709 color matrix, which is the standard for high-definition video.-pix_fmt rgb24: Forces the output PNG to use the standard 24-bit RGB pixel format. Without this, FFmpeg might output to a pixel format likergb8or a YUV-based PNG, which causes compatibility and color rendering issues in image viewers.-vframes 1: Tells FFmpeg to export exactly one frame.
Adjusting for SD and HDR Videos
If you are working with standard definition (SD) video or high-dynamic-range (HDR) video, you must match the color matrix to the source material:
For Standard Definition (SD / DVD) Videos (BT.601):
ffmpeg -ss 00:01:30 -i input.mp4 -vf "scale=in_color_matrix=auto:out_color_matrix=bt601" -pix_fmt rgb24 -vframes 1 output.pngFor Ultra High Definition (UHD / 4K / HDR) Videos (BT.2020):
ffmpeg -ss 00:01:30 -i input.mp4 -vf "scale=in_color_matrix=auto:out_color_matrix=bt2020" -pix_fmt rgb24 -vframes 1 output.png
By explicitly declaring the input and output color matrices alongside
the standard rgb24 pixel format, FFmpeg will bypass
default, inaccurate conversion presets and yield a mathematically
accurate PNG representation of the video frame.