Skip to content

Encode Raw Video

George Stoyanov edited this page Jun 18, 2018 · 2 revisions

Raw Video Encoding

Raw video is a lossless format for storing video files. Another benefit of this format is that it is decoding the encoded video and all the frames are holding all the information so you can easily extract a specific frames from it. Unfortunately this comes at the price which is the size of the output raw video. The size is of these files is really big, so you should be careful when you want to encode long video files always be sure that you have enough free space.

Please note that when you are encoding a video sequence to raw video the audio stream gets automatically cut out so you need to encode separately the audio stream in a different container and eventually after you re-encode the video to some other container to mux the video and audio streams back together. Information how you can do that could be found here

YUV or Y4M

Both YUV and Y4M are lossless formats for storing raw video footage. The main difference between them is that Y4M is actually containing information about the resolution of the picture, scan type, frame rate and the color space, while the YUV does not which makes Y4M more suitable for working since you don't need to specify these parameters every time you want to encode or transcode to and from Y4M. This means that technically speaking Y4M is not entirely raw video since it contains headers and metadata.

Encoded to RAW video Encoding (Y4M)

ffmpeg -i inputfile.mp4 outputfile.y4m - the syntax is pretty simple in this case. You need to specify the input and output file and that's all. This will preserve all the video parameters of the input file. Of course you can change them by defining the resolution -s 1280x720 to set the resolution to 1280x720, this is not necessary if the resolution of the input file is the same. You can similarly change the pixel format. You can see a list of all pixel formats issuing the command ffmpeg -pix_fmts and then define it with -pix_fmt yuv422p for example but this is also not necessary since y4m conversion preserves the color space.

RAW to Encoded Video Encoding (Y4M)

ffmpeg -i inputfile.y4m -c:v libx264 -preset ultrafast outputfile.mp4 - as you can see here the command is also pretty simple. You just need to define the input, output file and the desired codec. The preset part is optional. You can see more information how to encode to H.264 at the following link

Encoded to RAW video Encoding (YUV)

Here the command is similar to the Y4M command ffmpeg -i inputfile.mp4 -c:v rawvideo outputfile.yuv. As you can see you just need to define the input, output file and the video codec which could be even omitted since ffmpeg gets it from the .yuv extension.

RAW to Encoded video Encoding (YUV)

Here is where things get trickier. Since YUV format doesn't store any data about the video parameters, you need to define them: ffmpeg -f rawvideo -s 1920x1080 -r 50 -pix_fmt yuv420p -i inputfile.yuv -c:v libx264 -preset ultrafast -qp 0 outputfile.mp4 - here as you can see the command is way more complex and it requires you to define the input format, resolution, frame rate, pixel format and of course the input, output file, codec for the output file and the preset, which is an optional parameter. Again you can find more information about H.264 encoding at link