Cloudinary Blog

How to Compress Video Size Automatically With One Line of Code

How to Compress Video Size Automatically With One Line of Code

As technologies advance apace, the volume of online videos continues to increase fast. Instagram, WhatsApp, Facebook, YouTube, Musical.ly are just a few platforms among many that massively share and distribute videos around the clock.

When compared to other media files, such as those for audios and images, video files are larger, consuming a lion’s share of data bandwidth. Compressing video files enables faster downloads, uploads, and streaming online.


Sign up for Cloudinary free today!


For people in remote areas and developing regions, where download and upload speeds usually range between 720 kbytes and 1.5 Mbytes per second, compressing video files is not optional—it’s mandatory! To do that and deliver videos of a smaller file size to users with Cloudinary, you as developers can choose either of these two ways:

  • Compression as part of the upload request
  • On-the-fly compression during delivery

Update
On October 29th, 2019 Cloudinary released the new auto-quality setting for content-aware video compression. By using the q_auto parameter, Cloudinary intelligently encodes video using the most optimized settings, for the best trade-off between file size and quality. Read more.

Compression As Part of the Upload Request

This technique involves applying compression transformation to the videos during upload before storage. Cloudinary makes the process a walk in the park with just one line of code in Ruby, Python, or PHP:

Ruby

Copy to clipboard
Cloudinary::Uploader.upload("cool_video.mp4", :resource_type => :video, :quality => 60)

Python

Copy to clipboard
cloudinary.uploader.upload("cool_video.mp4", resource_type = "video", "quality" = "60")

PHP

Copy to clipboard
\Cloudinary\Uploader::upload("my_video.mp4", [ "resource_type" => "video", "quality" => "60"]);

In the code above, you specify the quality parameter to manipulate the video’s quality and size before Cloudinary stores the video in the cloud.

Note
If you’re not sure what number to assign to the quality parameter, just type auto. Cloudinary then automatically adjusts the compression quality for your video by applying the optimal balance between the video’s file size and quality.

On-the-Fly Compression During Delivery

With this Cloudinary technique, you upload videos straight to the cloud and then apply the quality compression parameter when delivering them to users. You can also serve videos in the formats that pertain to the various web browsers and mobile devices.

You configure quality on a 0-100 scale. The higher the video quality, the larger the video size; the lower the video quality, the smaller the video size. To compress video size on the fly, adjust their quality parameter in a codeline. See the examples below.

Node.js

Copy to clipboard
cloudinary.video("dog", {quality: 50})

Java

Copy to clipboard
cloudinary.url().transformation(new Transformation().quality(50)).videoTag("dog");

Python

Copy to clipboard
CloudinaryVideo("dog").video(quality=50)

On-the-fly video compression through the URL

Copy to clipboard
https://res.cloudinary.com/demo/video/upload/q_50/dog.mp4

In q_50 in the above URL, q stands for quality; 50 is the number of your choice on a 0-100 scale. Front-end developers, you can drop those components in your app out of the box, like this: React.js

Copy to clipboard
<Video publicId="dog" ><Transformation quality="50" /></Video>

Vue.js

Copy to clipboard
<cld-video publicId="dog" ><cld-transformation quality="50" /></cld-video>

dog represents the name of the uploaded video, which is usually the public ID (publicId) on the Cloudinary storage.

Conclusion

Compress video size automatically with Cloudinary making media much more accessible. Furthermore, Cloudinary’s drop-in tools and services are effective, simple, and intuitive, saving you ample time to focus on your project.

For details, check out the following Cloudinary documentation:

About Cloudinary

Cloudinary provides easy-to-use, cloud-based media management solutions for the world’s top brands. With offices in the US, UK and Israel, Cloudinary has quickly become the de facto solution used by developers and marketers at major companies around the world to streamline rich media management and deliver optimal end-user experiences.

For more information, visit www.cloudinary.com or follow us on Twitter


Further Reading on Video Manipulation

Recent Blog Posts

The Benefits of Headless DAMs

Headless is not a buzzword anymore. In fact, the concept of headless architecture is gaining momentum due to the flexibility it offers for composing new experiences and for tackling the undue complexity of an ever-evolving technology stack. That’s because while the evolution of the martech landscape has enabled disruptive, digital innovations, the approach of buying point solutions for solving specific challenges can expose companies to the complicated nature of new technologies, systems, and platforms.

Read more

Building Display Ads With Transparent Video

By Afzaal Ahmad Zeeshan
Build Web Ads With Transparent Video to Attract User Engagement

Billions of views on the Internet every day drive one of the biggest industries on the planet: advertising. The sheer size of that market and the competitive nature of vying for consumer attention results in a constant need for innovation. Readers are jaded, and display ads are blind spots.

Read more
How Cloudinary's Media Optimizer Helps E-Businesses Deliver Superior Web Performance

As a technology company, Cloudinary owes its success to its ability to build solutions that address the most critical challenges you, our customers, face. The companies we serve run the gamut of digital businesses—retailers and direct-to-consumer brands, media and entertainment, travel and hospitality—which, coincidentally, all care about the same things.

Read more
How to Auto-Tag Video With Markers on Cloudinary for Easy Navigation

A picture is worth a thousand words, and that also holds true for video, one minute of which, according to Dr. James McQuivey of Forrester Research, is worth 1.8 million words. That's why online stores rely on rich media to promote products and sales. Images and videos impart a real sense of involvement with a purchase—a car, a vacation getaway, an apartment rental—setting your business apart from the competition.

Read more