To convert a raw YUV file to MP4, you have to tell the converter exactly what is inside the file - width, height, frame rate and pixel format - because headerless .yuv files contain only pixel data and no description of it. With FFmpeg the basic command is ffmpeg -f rawvideo -pix_fmt yuv420p -s 1920x1080 -r 25 -i input.yuv -c:v libx264 -crf 18 -pix_fmt yuv420p output.mp4. Get any of those values wrong and the result will be garbled, skewed or the wrong colour.
This guide explains what raw YUV files are, how to work out the right parameters when nobody has told you, how to check your guess using file size, and how to handle Y4M files, 10-bit sources and image sequences.
Why raw YUV files are different
An ordinary video file such as an MP4 or MKV is a container: it holds compressed streams plus metadata describing resolution, frame rate, colour and timing. A raw .yuv file has none of that. It is simply frame after frame of uncompressed pixel values written one after another. You will typically meet these files from codec test suites, sensor development boards and capture pipelines.
Because there is no header, the software opening the file cannot guess. If you tell it the frame is 1920 pixels wide when it is really 1280, every row will wrap at the wrong point and you will see diagonal streaks. If you tell it the data is 4:2:0 when it is really 4:2:2, the colour planes will be misaligned and you will see green and purple smears.
The four values you need
- Width and height - the frame dimensions in pixels, such as 1920x1080 or 352x288 (CIF, common in older test sequences).
- Pixel format - how the samples are laid out. The most common is
yuv420p(also called I420): a full-resolution Y plane followed by quarter-size U and V planes. - Frame rate - raw files do not store timing, so you choose it. If you pick 25 when the source was 50, playback will run at half speed but no frames are lost.
- Bit depth - 8-bit samples use one byte each; 10-bit and 12-bit samples are normally stored in two bytes, little-endian (FFmpeg's
yuv420p10le, for example).
Colour information matters too. Raw data does not say whether it uses BT.601, BT.709 or BT.2020 primaries, or limited (TV) or full (PC) range. If colours look slightly washed out or crushed after conversion, the range or matrix is probably being interpreted wrongly.
Common pixel formats and frame sizes
Each pixel format produces a predictable number of bytes per frame. This is the single most useful check you have, because the file size must be an exact multiple of the frame size.
| Format | Layout | Bytes per frame | 1920x1080 frame |
|---|---|---|---|
| yuv420p / I420 | Planar Y, U, V (4:2:0) | W x H x 1.5 | 3,110,400 |
| YV12 | Planar Y, V, U (4:2:0) | W x H x 1.5 | 3,110,400 |
| NV12 / NV21 | Y plane plus interleaved UV or VU | W x H x 1.5 | 3,110,400 |
| yuv422p | Planar 4:2:2 | W x H x 2 | 4,147,200 |
| YUY2 / UYVY | Packed 4:2:2 | W x H x 2 | 4,147,200 |
| yuv444p | Planar 4:4:4 | W x H x 3 | 6,220,800 |
| RGB24 / BGR24 | Packed RGB | W x H x 3 | 6,220,800 |
| RGBA / BGRA | Packed RGB with alpha | W x H x 4 | 8,294,400 |
| yuv420p10le | Planar 4:2:0, 16-bit containers | W x H x 3 | 6,220,800 |
Note that several formats share a frame size. I420, YV12 and NV12 are all 1.5 bytes per pixel, so file size alone cannot tell them apart. If you choose I420 and blues and reds appear swapped, try YV12 (which stores V before U) or NV12.
Working out unknown parameters
If the file came with no documentation, start from its size in bytes. Divide by candidate frame sizes and look for a whole number. For example, a file of 155,520,000 bytes divided by 3,110,400 gives exactly 50, so it is plausibly 50 frames of 1080p 4:2:0 8-bit. Divided by 4,147,200 it gives 37.5, which rules out 1080p 4:2:2.
File names often carry clues. Test sequences are frequently named like foreman_352x288_30.yuv or clip_1920x1080_10bit_420.yuv. Once you have a candidate, preview a single frame before encoding the whole file:
ffplay -f rawvideo -pixel_format yuv420p -video_size 1920x1080 input.yuv
A correct guess shows a clean picture. Diagonal shearing means the width is wrong; a picture that drifts or rolls between frames usually means a header or padding you have not accounted for; strange colours point to the pixel format or plane order.
Converting with FFmpeg
For an 8-bit 4:2:0 source, the command in the introduction is all you need. For a 10-bit source you want to keep at 10-bit, encode to HEVC with a 10-bit output format:
ffmpeg -f rawvideo -pix_fmt yuv420p10le -s 3840x2160 -r 50 -i input.yuv -c:v libx265 -crf 18 -pix_fmt yuv420p10le output.mp4
If the target is wide compatibility rather than fidelity, add -pix_fmt yuv420p to the output side so the result is 8-bit 4:2:0, which plays almost everywhere. A 4:2:2 or 4:4:4 H.264 file is valid but many players and browsers will refuse it. You can also tag the colour so players interpret it correctly, for example -colorspace bt709 -color_primaries bt709 -color_trc bt709 on the output.
If you need a lossless intermediate rather than a delivery file, encode to FFV1 in MKV instead; our guide to FFV1 lossless video archiving covers the settings. Choosing between MP4 and MKV for the result is covered in MKV vs MP4.
Y4M and image sequences
Y4M (YUV4MPEG2) files are raw YUV with a short text header per file and a marker before each frame. The header records width, height, frame rate, interlacing, aspect ratio and chroma format, so you do not need to specify anything: ffmpeg -i input.y4m -c:v libx264 -crf 18 output.mp4 just works. If you are producing raw files yourself, Y4M is a much safer choice than headerless YUV.
Image sequences such as numbered PNG, TIFF or DPX frames carry resolution and pixel format in every image, but not a frame rate. Supply one on the input side: ffmpeg -framerate 24 -i frame_%05d.png -c:v libx264 -crf 18 -pix_fmt yuv420p output.mp4.
Common mistakes
- Putting the frame rate in the wrong place. For raw input,
-ror-frameratemust come before-ito describe the source. Placed after, it changes the output rate by dropping or duplicating frames. - Forgetting odd dimensions. 4:2:0 encoders need even width and height. Crop or pad if the source is, say, 1281 pixels wide.
- Assuming big-endian. Most 10-bit and 12-bit raw files are little-endian. If a 10-bit preview looks like noise, try the
bevariant.
Doing it with Movie Encoder
Movie Encoder includes a RAW Video Import Wizard for headerless YUV and RGB files - including YUV420P/I420, YV12, NV12, NV21, YUV422P, YUV444P, YUY2, UYVY, RGB24, BGR24, RGBA, BGRA, GRAY8 and 10/12-bit planar formats - as well as Y4M and image sequences. You set width, height, frame rate, pixel format, offsets, colour range, primaries, transfer, interlacing and aspect ratio, and it calculates the expected frame size and warns you if the file size does not match.
A preview frame lets you confirm the guess before encoding, and you can save the settings as a profile for files from the same source. From there the clip can be encoded to H.264, H.265, AV1 or a lossless format like any other input, with the command preview showing the exact FFmpeg arguments used.
Frequently asked questions
Why does my converted YUV video look diagonal and striped?
Diagonal streaks almost always mean the width is wrong, so each row wraps in the wrong place. Check the frame dimensions and confirm that the file size divides evenly by the frame size.
What is the difference between YUV and Y4M?
Both hold uncompressed YUV frames, but Y4M adds a small text header recording resolution, frame rate and chroma format. That means a converter can open Y4M without you entering any parameters.
Should I convert raw YUV to H.264 or a lossless format?
Use H.264 or H.265 when you need a small file to view or share. If the footage is test material or a master you may need to process again, a lossless codec such as FFV1 keeps every sample exactly.
