Encoding YUV Files with FFmpeg and Converting to Y4M

I just finished a project that involved encoding lots of downloaded YUV test files, which is something I never tried before. The problem is that there’s no resolution or frame rate information in the file header, so you have to detail all this in the command string.

My first crack at the problem evolved into this command string, obviously for a 25 fps 1080p file. Use this up to and including the -i switch in the normal command string.

ffmpeg -f rawvideo -vcodec rawvideo -s 1920x1080 -r 25 -pix_fmt yuv420p -i input.yuv

This worked fine but seemed verbose so I tried this one which also worked. Simpler is always better, so I’ll use this one going forward unless it breaks and I have to try the one above:

ffmpeg -s 1920x1080 -r 25 -pix_fmt yuv420p -i input.yuv

Of course, if you convert the YUV file to a Y4M file, you don’t have to include the descriptive information in FFmpeg or any analysis program. That’s because the Y4M file has this information in the file header. To convert the YUV to Y4M I first tried this:

ffmpeg -s 1920x1080 -r 25 -pix_fmt yuv420p -i input.yuv -c:v copy input.y4m

Which failed.

So, I dropped the c:v copy and tried this:

ffmpeg  -s 1920x1080 -r 25 -pix_fmt yuv420p -i input.yuv input.y4m

Which worked and produced a file exactly the same size as the YUV. Here are the two peas sitting in the pod.

I encoded a file using the Y4M file and the output was identical to the YUV with a much simpler command string.

Author’s note:

After posting this article, I received the following message from Lou Logan, who wrote:

You are using -s, -r, and -pix_fmt input options for the rawvideo demuxer. You should be using the proper rawvideo private options instead. These are -video_size, -framerate, and -pixel_format. You can see demuxer info with “ffmpeg -h demuxer=rawvideo”.

Here’s what the help file says:

To verify these comments, I changed the command string to this:

ffmpeg -video_size 1920x1080 -framerate 25 -pixel_format yuv420p -i input.yuv output_correct.y4m

Which produced what appeared to be the identical file:

 

 

Identical results notwithstanding, the best practice is to use what’s specified in the help file, so from here on out, I’ll use the syntax that Mr. Logan suggested. Thanks Lou!

Having successfully converted the YUV file to Y4M, I can use the input.y4m file in FFmpeg and other tools like the Moscow State University Video Quality Measurement Tool and VMAF-Master and not have to specify resolution, format, and frame rate, simplifying the lot and reducing the risk of error.

About Jan Ozer

Avatar photo
I help companies train new technical hires in streaming media-related positions; I also help companies optimize their codec selections and encoding stacks and evaluate new encoders and codecs. I am a contributing editor to Streaming Media Magazine, writing about codecs and encoding tools. I have written multiple authoritative books on video encoding, including Video Encoding by the Numbers: Eliminate the Guesswork from your Streaming Video (https://amzn.to/3kV6R1j) and Learn to Produce Video with FFmpeg: In Thirty Minutes or Less (https://amzn.to/3ZJih7e). I have multiple courses relating to streaming media production, all available at https://bit.ly/slc_courses. I currently work as www.netint.com as a Senior Director in Marketing.

Check Also

Take the Bitmovin Video Developer Survey

Contribute to the one of the most valuable sources of industry data by completing the …

Speech-to-text In Premiere Pro – Fast, Easy, Accurate, and Free

This video tutorial teaches you how to convert speech-to-text in Premiere Pro. I’ve been using …

Streaming Media 101: Training for App & Player Development/Testing Professionals

Leave a Reply

Your email address will not be published. Required fields are marked *