How to Specify Raw Video Format in FFmpeg with -f
This article explains how to use the -f (format)
parameter in FFmpeg to force and specify raw video formats during
encoding, decoding, or transcoding. You will learn why the
-f parameter is necessary for raw video streams, how to
combine it with essential parameters like resolution and pixel format,
and see practical command-line examples for both input and output
files.
Why Use the
-f Parameter for Raw Video?
In FFmpeg, the -f parameter forces the input or output
file format. While container formats like MP4 or MKV have headers that
tell FFmpeg the video’s resolution, frame rate, and pixel format, raw
video files (such as .yuv, .rgb, or
.gray) contain nothing but pixel data.
Because raw files lack headers, FFmpeg cannot automatically detect
their properties. You must use -f rawvideo to tell FFmpeg
how to interpret or write the raw stream.
Specifying Raw Video as an Input
When reading a raw video file, you must place the
-f rawvideo parameter before the input file
(-i). Because raw video has no header, you must also
manually specify the pixel format, video size (resolution), and frame
rate.
Syntax:
ffmpeg -f rawvideo -pix_fmt [pixel_format] -s [width]x[height] -r [fps] -i [input_file] [output_file]Example:
To convert a raw YUV420P video file (resolution 1920x1080 at 30 frames per second) to an MP4 file, use the following command:
ffmpeg -f rawvideo -pix_fmt yuv420p -s 1920x1080 -r 30 -i input.yuv output.mp4Specifying Raw Video as an Output
When exporting to a raw video format, you place
-f rawvideo before the output file. You should
also define the target pixel format using -pix_fmt.
Syntax:
ffmpeg -i [input_file] -f rawvideo -pix_fmt [pixel_format] [output_file]Example:
To extract the video frames of an MP4 file into a raw RGB24 stream:
ffmpeg -i input.mp4 -f rawvideo -pix_fmt rgb24 output.rgbAlternative: Using
the yuv4mpegpipe Format
If you want raw YUV video but wish to avoid manually typing the
resolution and framerate when reading the file, you can use the
yuv4mpegpipe format (Y4M). This format adds a small, simple
header to the raw YUV data containing the video properties.
Writing to Y4M:
ffmpeg -i input.mp4 -f yuv4mpegpipe output.y4mReading from Y4M:
ffmpeg -f yuv4mpegpipe -i input.y4m output.mp4(Note: You do not need to specify -s,
-pix_fmt, or -r when reading a Y4M file, as
FFmpeg reads this information from the Y4M header).