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.

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

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

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

Python

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

PHP

\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

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

Java

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

Python

CloudinaryVideo("dog").video(quality=50)

On-the-fly video compression through the URL

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

<Video publicId="dog" ><Transformation quality="50" /></Video>

Vue.js

<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:


Further Reading on Video Manipulation

Recent Blog Posts

Converting Videos to GIFs in Kotlin-Based Android Apps

Among the image formats available today, Graphics Interchange Format (GIF) works for both static and animated images. A popular use of GIF images—commonly called GIFs—is to repeatedly loop motions, which you cannot in real life. Many people have turned themselves into online celebrities through effective application of GIFs.

Read more
Simple Steps to Make Your Site Accessible With Cloudinary

Web accessibility (a11y) is about gaining an insight into—

It’s a blurry line between accessibility and usability (or user experience) and one might argue that they are actually the same thing. Nonetheless, if your audience can't use your app with ease and confidence, then it’s not accessible enough and the user experience is not good enough.

Read more
Learn three ways in which to tell appealing stories on video.

Video storytelling is one of the most effective means of conveying messages to your audience. It’s also a popular marketing tool because most people are automatically drawn to video. In eCommerce, if accompanied by attractive images and a compelling narrative, video storytelling uniquely connect with potential customers without fail.

Read more