How to Read Raw YUV Video in FFmpeg
Reading raw YUV video files with FFmpeg requires explicitly defining the input parameters because raw files do not contain metadata headers. This guide explains how to use FFmpeg command-line options to specify the pixel format, resolution, and frame rate of your raw YUV data to ensure successful decoding and conversion.
The Core Command Structure
Because raw YUV files lack a header to describe their contents, you
must tell FFmpeg how to interpret the raw stream before
specifying the input file. This means the input parameters must be
placed before the -i flag in your command.
The basic command template is:
ffmpeg -f rawvideo -pix_fmt [format] -s [width]x[height] -r [fps] -i input.yuv output.mp4Parameter Breakdown
-f rawvideo: Demuxer flag that tells FFmpeg to treat the input as raw video data.-pix_fmt: Specifies the pixel format of the raw YUV data. Common formats includeyuv420p,yuv422p,yuv444p, anduyvy422.-s(or-video_size): Defines the resolution in pixels (width x height), such as1920x1080or1280x720.-r(or-framerate): Sets the frame rate of the input video, such as30,60, or24.-i input.yuv: The path to your raw YUV input file.
Practical Examples
Example 1: Standard 1080p YUV 4:2:0 at 30 FPS
To convert a raw YUV420p video with a resolution of 1920x1080 at 30 frames per second into an MP4 file, use the following command:
ffmpeg -f rawvideo -pix_fmt yuv420p -s 1920x1080 -r 30 -i input.yuv output.mp4Example 2: 720p YUV 4:2:2 at 60 FPS
If your raw data uses a higher chroma subsampling format like YUV 4:2:2 at 1280x720 and 60 frames per second:
ffmpeg -f rawvideo -pix_fmt yuv422p -s 1280x720 -r 60 -i input.yuv output.mp4Example 3: Playing Raw YUV Directly
You can also use ffplay (FFmpeg’s media player) to
preview raw YUV files without converting them first. The syntax for
defining input parameters remains the same:
ffplay -f rawvideo -pix_fmt yuv420p -s 1920x1080 -r 30 input.yuvFinding Supported Pixel Formats
If you are unsure of the exact string for your pixel format, you can view all formats supported by your FFmpeg installation by running:
ffmpeg -pix_fmts