Python Video Editing and Optimization

The days of needing a graphic designer to edit lots of of movies manually are over. Today, Python scripts make it straightforward to scale video manufacturing in your tasks and produce high-quality movies in bulk, for the very best person expertise.

It’s no surprise writing applications in Python has turn out to be the environment friendly and cost-effective approach to edit and personalize movies. Automating video edits utilizing Python makes it doable for firms to ship marketing campaign movies to customers and hold product movies up-to-date on web sites with out stress. 

Python offers some highly effective libraries, together with MoviePY, Scikit-video, and VidGear, that allow you to edit your movies with just a few traces of code. 

Cloudinary is one other instrument that gives comparable performance. It’s a cloud-based picture and video administration service that gives wealthy modifying capabilities accessible through an easy-to-use Python SDK. Cloudinary additionally offers superior options that make an enormous distinction, for instance cropping with AI to make sure that the vital components of the video are saved inside the image all through, video optimization to enhance internet efficiency, and adaptive streaming for a greater person expertise.

In this text, you’ll see examples of how one can automate Python video modifying and optimization with Cloudinary, together with:

  • Trimming and concatenating video clips.
  • Adding subtitles, watermarks, and background music.
  • Non-linear modifying within the type of video overlays.
  • Removing backgrounds. 

You’ll additionally discover ways to make the most of AI to smart-crop your movies and methods to optimize your movies for ideally suited internet efficiency and clean streaming in your web site.

You would possibly wish to apply the modifying and optimization strategies within the code snippets under to your individual movies. To try this, comply with the directions to create a Cloudinary account, add a number of movies, and configure. 

Then, change the general public IDs (the distinctive identifiers for the movies) within the code snippets under with these of the belongings you uploaded.

Here’s methods to get began with Cloudinary:

  1. Register here.
  2. Drag and drop the movies you wish to use within the Media Library. Keep a document of their public IDs so you possibly can reference them later in your code.
Public ID

3. Install Cloudinary.

  • In a terminal in your Python3 setting, run the next code:
pip3 set up cloudinary
pip3 set up python-dotenv

4. Configure Cloudinary.

Code language: PHP (php)

When writing your individual purposes, comply with your group’s coverage on storing secrets and techniques and don’t expose your API secret.
Don’t retailer your .env underneath model management for optimum safety.

5. In your challenge, create a brand new file referred to as Copy and paste the next into this file:

from dotenv import load_dotenv

import cloudinary

config = cloudinary.config(safe=True)

print("Your Cloudinary Credentials:")
print(config.cloud_name, config.api_key)Code language: PHP (php)

The following examples present a gamut of video modifying and optimization potentialities utilizing Python and Cloudinary. The objective is to offer you a small style of what you are able to do with Cloudinary, present you methods to implement the edits, and encourage you to begin creating your individual. 

You can check out every instance individually, and you possibly can see a full code sandbox under or on GitHub.

The following operate returns the URL of an authentic video saved in Cloudinary:

def getOriginal(publicId):
 return originalURLCode language: PHP (php)

The public ID, the video’s distinctive identifier, is handed in to operate. Cloudinary returns the video’s URL, which can be utilized to ship the video in its authentic type.

Here’s the video:

The following examples show how to edit the original video by adding a few parameters.

This code snippet edits the video in numerous ways: 

Resizing and cropping: {"aspect_ratio":"4:3", "crop":"fill", "y":"100", "width":"400"}

Trimming: {"duration": "5"}

Concatenating a cropped, resized, and trimmed video stored in Cloudinary with public ID docs/video_features_tutorial/makeup:

{"duration": "5", "flags": "splice", "overlay": "video:docs:video_features_tutorial:makeup"},
{"aspect_ratio": "4:3", "crop": "fill", "y":"130", "width": "400"},
{"flags": "layer_apply" }

Adding background music, stored in Cloudinary with public ID video_features_tutorial/romeo_and_juliet:

{"overlay": "video:docs:video_features_tutorial:romeo_and_juliet"},
{"flags": "layer_apply"}

Adding a watermark, stored in Cloudinary with public ID cloudinary_icon, resized and placed in the north east corner or the video: 

{"overlay": "cloudinary_icon"},
{"width": "40", "x":"10", "y":"10" },
{"flags": "layer_apply", "gravity": "north_east"}

Adding an SRT file stored in Cloudinary with public ID video_features_tutorial/ as subtitles: 

{"overlay": {"resource_type": "subtitles", "public_id":
{"flag": "layer_apply" }

def concatenated():
 videoURL = cloudinary.CloudinaryVideo("docs/video_features_tutorial/hair").build_url(transformation=[
   {"aspect_ratio":"4:3", "crop":"fill", "y":"100", "width":"400"},
   {"duration": "5"},
   {"duration": "5", "flags": "splice", "overlay": "video:docs:video_features_tutorial:makeup"},
   {"aspect_ratio": "4:3", "crop": "fill", "y":"130", "width": "400"},
   {"flags": "layer_apply" },
   {"overlay": "video:docs:video_features_tutorial:romeo_and_juliet"},
   {"flags": "layer_apply"},
   {"overlay": "cloudinary_icon"},
   {"width": "40", "x":"10", "y":"10" },
   {"flags": "layer_apply", "gravity": "north_east"},
   {"overlay": {"resource_type": "subtitles", "public_id": "docs/video_features_tutorial/"}},
   {"flag": "layer_apply" }])
 return videoURLCode language: JavaScript (javascript)

Here’s the edited video:

Here’s an example of non-linear editing. You can apply one video over the other as an overlay to have them play at the same time: 

def overlay():
 videoURL = cloudinary.CloudinaryVideo("docs/sdk/go/exercise1").build_url(transformation=[
   {"overlay": "video:exercise2"},
   {"flags": "layer_apply","gravity":"north_east","start_offset":"2.0"}
 return videoURLCode language: PHP (php)

Here’s the video:

In this example, the blue background in this product video is switched to a beach scene:

def removeBackground():
 {'width': 500, 'crop': "scale"},
 {'overlay': "video:docs:bluescreen_watches"},
 {'flags': "relative", 'width': "0.6", 'crop': "scale"},
 {'color': "#0e80d8", 'effect': "make_transparent:20"},
 {'flags': "layer_apply", 'gravity': "north"},
 {'duration': "15.0"}
 return videoURLCode language: PHP (php)

Here’s the video:

When you crop videos, especially in bulk, you risk cutting out the important parts and ruining the video. Cloudinary prevents this from happening by using AI to make sure that the main subjects of the video are followed throughout. 

Cropping this video causes the gymnast to move in and out of view because, the crop by default just captures the center of the video:

def getGAutoOrig():
 videoURL = cloudinary.CloudinaryVideo("olympic_gymnast.mp4").build_url(transformation=[
  return videoURLCode language: PHP (php)

Here’s the video, cropped with out AI utilized. Notice the gymnast doesn’t keep within the body all through:

Adding the gravity:auto parameter ensures that the gymnast remains in focus:

def gAuto():
 videoURL = cloudinary.CloudinaryVideo("olympic_gymnast.mp4").build_url(transformation=[
 return videoURLCode language: JavaScript (javascript)

Cloudinary can reduce the size of your videos to improve your web performance with:

  • f_auto: Delivers the video using the best format for the requesting browser.
  • q_auto: Delivers the video using the optimal compression that balances the smallest size without sacrificing visual quality.

def fqAuto():
  videoURL = cloudinary.CloudinaryVideo("docs/sunglasses").build_url(transformation=[
  return videoURLCode language: PHP (php)

Here’s the optimized video:

Cloudinary can ensure that your videos are delivered with the least interruption, at every second and from any browser, using adaptive bitrate streaming

def autoStreaming():
  videoURL=cloudinary.utils.cloudinary_url("docs/waterfall.m3u8", streaming_profile="auto", resource_type="video")
  return videoURLCode language: PHP (php)

Here’s the streaming video:

You can see all these examples built-in in a full Python program on this code sandbox: 

These are simply among the Python video modifying and optimization potentialities that Cloudinary can provide. Cloudinary offers related modifying capabilities to Python libraries akin to MoviePY, Scikit-video, and VidGear, together with some Cloudinary-unique capabilities besides. Leverage Cloudinary’s smart-cropping, optimization, and adaptive bitrate streaming to enhance your web site’s efficiency and person expertise.

You’re invited to discover the expanse of editing and management options that Cloudinary affords. Let us know that are your favorites!

Recommended For You

Leave a Reply