Video Compression Hacks You Should Live By

Today, our guest blogger Eddie Segal explains compression methods, why they're important, and techniques on optimizing your videos

Written by Eddie Segal • Last Updated: • General •

Video compression techniques

[Image Source]

Video compression is a process that reduces the size of a video file by changing the format or contents of video data. Compression is accomplished using codecs, software or hardware that encode and decode video in different formats. Codecs are also what allow you to play multiple types of video in a single player. In this article, you’ll learn how to optimize video compression.

Why Is Video Compression Important?

There are several reasons why developers use video compression, including:

  • To save storage space—smaller files take less space and cost less to store.
  • Enables you to convert video for other players—different players require data to be stored in different formats. You can use compression to convert raw video into playable formats.
  • Makes streaming easier—smaller file sizes take less bandwidth to transfer. Lower bandwidth requirements mean more of your video can is sent before bandwidth creates a bottleneck and causes buffering.
  • Decreases page load times—like with streaming, smaller video sizes load faster, reducing user wait times before video is usable.

Methods of Compression

There are two types of compression you can use, depending on your needs and video purposes.

Lossless Compression

Lossless compression preserves all data stored in the original file. To reduce file size, repeating data patterns in the file are deduplicated through the use of references. The first instance of a pattern is kept and other instances are replaced with a pointer to the retained instance.

Lossless compression is typically used when it is important to retain all video data, such as in archives or master copies. H.265, VP9, and Motion JPEG 2000 are all examples of lossless compression codecs you can use for video.

Lossy Compression

Lossy compression removes unnecessary data from video files to reduce file size. This data might include metadata, video frames, color information, or individual pixels. You can use lossy compression to create significantly smaller file sizes than lossless in exchange for permanent quality reduction. Depending on the level of compression you perform, this quality change may be unnoticeable to viewers.

Lossy compression is used for most video purposes, including streaming, website use, and DVD or Blu-Ray. MPEG-4, AV1, and Windows Media Video (WMV) are all examples of lossy compression codecs you can use for video.

Compress Video Size Techniques

When compressing videos, there are several methods you can use to reduce file size. You can use a combination of these methods or just one, depending on your desired result.

Interframe Prediction

Interframe prediction uses algorithms to determine how frames within a video change and removes extra, static information accordingly. For example, say you have a video with a person moving in the foreground and an unchanging background. You don’t need to save the background information in each frame because it doesn’t change. Instead, you can retain only those frames in which the background does change and reference the previous background in the remaining frames.

Unfortunately, this method isn’t perfect and can lead to artifacts in your video. Artifacts are image or sound information that are incorrectly carried forward. For example, you may experience artifacts as disconnected limbs or overlapping speech.

The above image can give you a better idea of how this works. In it, I-frames are whole images, with no missing information. P-frames are predicted images, containing a reference to information from the previous frame and any information that has changed. B-frames are similar to P-frames except B-frames reference both the previous and following frames. 

Image Resizing

Another method of compressing video is to change the resolution, also known as resizing. Resizing reduces the number of pixels saved per frame. For example, reducing the resolution from 1280x720 to 640x360 reduces pixel amounts from 921,600 to 230,400.

When a video size is reduced, the compression algorithm evaluates the current pixels and tries to retain those with the most important information, such as color or hue changes. However, when pixels are removed, detail is lost. 

Creating multiple video resolutions and serving the one that best suits a user’s bandwidth is how adaptive video streaming works. For this, the same video is compressed to multiple degrees with video quality decreasing as smaller resolutions are created.

Reducing Frame Rate

A less commonly used method is to reduce the frame rate of a video. Frame rate determines how smooth a video appears. High action videos require higher frame rates while low action videos can use lower. When you change the frame rate, you remove frames and show each remaining frame for a longer period. 

The downside of this method is that it can make video appear jerky or disjointed. Reductions in frame rate also tend to be more obvious to viewers, even if a video contains low action.

Video Compression Hacks for Developers

When compressing your videos, there is a wide variety of tools and techniques you can use. To ensure that you can compress videos as effectively as possible with minimal degradation, try to follow the tips offered below.

Prioritize HTML5 Supported Formats

The growing standard for websites is HTML5, particularly as Flash support decreases. To stay at the forefront of web development then, you should make sure to include those formats preferred by HTML5. For example, MP4 or WebM. Do keep in mind, however, that not all browsers fully support HTML5. To support video for older browsers, make sure to include fallback formats that are supported.

Choose Your Compression Wisely

When compressing video you need to first choose the type of compression you need (lossy or lossless) and then choose your codec. While compression type is determined by quality and integrity needs, codec is determined by destination. Video players typically do not support every codec so you must optimize your video to ensure that the standard you choose is supported.

Use Responsive Video

To ensure that you are supporting the broadest range of users, you should make multiple versions of videos available. You can then serve these videos according to user bandwidth, device specifications, or preference. This is commonly done through media queries or via dropdowns that enable users to manually choose. Offering different versions ensures that your video load times are as fast as possible and improves user experience.

Use a Backup Repository

Although you are unlikely to serve raw, uncompressed video to users it is a good idea to retain a master version as a backup. Each version of a video you create should be produced from the master copy. This ensures that as much quality as possible is retained. If you further compress already compressed videos you end up with a lower quality copy than if you created videos from a master.

Conclusion

Video compression can help you save on storage space and cut back on overhead. Smaller files are also easier on bandwidth consumption. However, not all compression techniques are made alike. Be sure to choose the right compression method for your files. If you need high quality, then lossy is probably not for you. And remember—keep the master copy as a backup and compress only copies.

When creating videos, how do you compress your videos? Do you use YouTube's algorithm? Post your comments below and let's discuss.

ASP.NET 8 Best Practices on Amazon

ASP.NET 8 Best Practices by Jonathan Danylko


Reviewed as a "comprehensive guide" and a "roadmap to excellence" with over 120 Best Practices for ASP.NET Core 8, Jonathan's first book by Packt Publishing explores proven techniques for every phase of the SDLC.

Learn industry-standard concepts to improve your coding, debugging, and deployment of ASP.NET Core websites.

Order now on Amazon.com button

Picture of Eddie Segal

Eddie Segal is an electronics engineer with a Master’s Degree from Be’er Sheva University, a big data and web analytics specialist, and also a technology writer. In my writing, I cover subjects ranging from cloud computing to agile development to cybersecurity and deep learning.

comments powered by Disqus