In our first example in this chapter, we saved an uncompressed AVI video. This led to a very quick appearance of the message informing us of reaching the limit of 500 MB. The number of frames captured until the appearance of the message was 401, which equals to the duration of approximately 13.37 seconds. Quite a large size for such a small video!
Let's do some math to understand how this works. As we recall from the previous chapter, the memory that an uncompressed 8-bit video consumes can be calculated by multiplying its total number of pixels, by the number of frames by three (the number of color channels). The resulting size is counted in bytes.
We will now try to verify that our resulting video file is as large as we expected it to be, using Command Window. First we will get the size of our video file:
>> vidInfo = dir('E:VideosAcquisition est.avi'), % get file info >> fileSize = vidInfo.bytes % save filesize in bytes
The output of the previous code is as follows:
fileSize = 498986856
Now, let's calculate our expected video file size based on its dimensions:
>> vidObj = VideoReader('E:VideosAcquisition est.avi'), % load video >> expSize = vidObj.Width * vidObj.Height * vidObj.NumberOfFrames * 3
The output of the previous code is as follows:
expSize = 498908160
As you can observe, the actual video file is a little bigger than expected (approximately 58 KB). This is caused by the information overhead added by the encoder to construct the actual video file. If you want to reproduce the results in your own computer, you should, of course, change the path containing your video.
Let's now make a compressed video like the one you created in your second exercise. For the sake of comparison, we have used a resolution of 720 x 480 and placed it in the same directory as test.avi
. We have positioned the camera so that it looks at a window with no apparent motion for one second. The Frames per trigger field was set to 25
, the Trigger
type field to Immediate, and the Number of triggers field to 25
. We gave the video the filename testStill.mp4
and chose also to log it to the RAM and export it to the Workspace window as testStill
. Let's see how its frames look:
Hence, the process will be the same for getting the actual size of the file:
>> vidInfo = dir('E:VideosAcquisition estStill.mp4'), % get file info >> fileSize = vidInfo.bytes % save filesize in bytes
The output is as follows:
fileSize = 166262
For the expected size of the uncompressed equivalent video, we use the number of frames, which is 25
:
>> expSize = 720 * 576 * 25 * 3
The output is as follows:
expSize = 31104000
This is the power of compression. Using MP4 compression, we have managed to limit the size of our video from an approximate expected 31 MB to approximately 166 KB. Not bad! The ratio of compression equals to:
>> compressionRatioStill = fileSize / expSize
The output is as follows:
compressionRatioStill = 0.0053
Now, let's repeat the same thing in a scene with motion.
For this experiment, we will start waving a pen in front of the camera for the duration of the video. This will show us if the videos with high motion have a different compression ratio than those without much motion. We'll use the same settings as in the previous section and name our video testMotion.mp4
. We will also export the frames to a variable called testMotion
. Let's see how the frames look:
The motion included is now obvious, as is the distortion of the images caused by the fact that our video is interlaced (recall the previous chapter). The expected size of the uncompressed video remains the same. However, it is intriguing to see what happens with the actual compressed video size:
>> vidInfo = dir('E:VideosAcquisition estMotion.mp4'), % get file info >> fileSize = vidInfo.bytes % save filesize in bytes
The output is as follows:
fileSize = 527062
So, it's true that the videos that include a lot of motion are bigger that is they have a larger compression ratio than the ones that include little motion. The actual ratio on this occasion is:
>> compressionRatioMotion = fileSize / expSize
The output is as follows:
compressionRatioMotion= 0.0169
If you divide the compression ratio of the testMotion.mp4
video to that of the testStill.mp4
video, you will find that it is approximately 3.2 times larger. The higher the compression ratio is, the smaller the space savings derived from the compression process.