Cloudinary Blog

How to Compress Video Size Automatically With One Line of Code

By
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, TikTok 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 kb and 1.5 Mb 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:


Further Reading on Video Manipulation

Recent Blog Posts

Our $2B Valuation

By
Blackstone Growth Invests in Cloudinary

When we started our journey in 2012, we were looking to improve our lives as developers by making it easier for us to handle the arduous tasks of handling images and videos in our code. That initial line of developer code has evolved into a full suite of media experience solutions driven by a mission that gradually revealed itself over the course of the past 10 years: help companies unleash the full potential of their media to create the most engaging visual experiences.

Read more
Direct-to-Consumer E-Commerce Requires Compelling Visual Experiences

When brands like you adopt a direct–to-consumer (DTC) e-commerce approach with no involvement of retailers or marketplaces, you gain direct and timely insight into evolving shopping behaviors. Accordingly, you can accommodate shoppers’ preferences by continually adjusting your product offering and interspersing the shopping journey with moments of excitement and intrigue. Opportunities abound for you to cultivate engaging customer relationships.

Read more
Automatically Translating Videos for an International Audience

No matter your business focus—public service, B2B integration, recruitment—multimedia, in particular video, is remarkably effective in communicating with the audience. Before, making video accessible to diverse viewers involved tasks galore, such as eliciting the service of production studios to manually dub, transcribe, and add subtitles. Those operations were costly and slow, especially for globally destined content.

Read more
Cloudinary Helps Minted Manage Its Image-Generation Pipeline at Scale

Shoppers return time and again to Minted’s global online community of independent artists and designers because they know they can count on unique, statement-making products of the highest quality there. Concurrently, the visual imagery on Minted.com must do justice to the designs into which the creators have poured their hearts and souls. For Minted’s VP of Engineering David Lien, “Because we are a premium brand, we need to ensure that every single one of our product images matches the selected configuration exactly. For example, if you pick an 18x24 art print on blue canvas, we will show that exact combination on the hero images in the PDF.”

Read more
Highlights on ImageCon 2021 and a Preview of ImageCon 2022

New year, same trend! Visual media will continue to play a monumental role in driving online conversions. To keep up with visual-experience trends and best practices, Cloudinary holds an annual conference called ImageCon, a one-of-a-kind event that helps attendees create the most engaging visual experiences possible.

Read more