How to Blur the Background of a Video using Python?

A popular way to resize a video is by using background blur. This is commonly used to turn a vertical video into a landscape format with blurred edges. To do this in your Python script, you'll need a video editing API. Here's how to blur a video's background in just a few lines of Python code:

Input Code
import requests

json = {
 'source': {
  'output_format': 'mp4',
  'width': 1920,
  'height': 1080,
  'elements': [
   {
    'type': 'video',
    'clip': True,
    'color_overlay': 'rgba(0,0,0,0.15)',
    'blur_radius': 57,
    'source': 'https://cdn.creatomate.com/demo/vertical.mp4',
    'volume': '0%'
   },
   {
    'type': 'video',
    'source': 'https://cdn.creatomate.com/demo/vertical.mp4',
    'fit': 'contain'
   }
  ]
 }
}

response = requests.post(
 'https://api.creatomate.com/v1/renders',
 headers={
  # Find your API key under 'Project Settings' in your account:
  # https://creatomate.com/docs/api/rest-api/authentication
  'Authorization': 'Bearer Your-API-Key',
  'Content-Type': 'application/json',
 },
 json=json
)

# Wait a minute, then visit the URL provided in the response:
print(response.json())

Output Video

How it works: This method works with any video format, such as square, portrait, or vertical. No matter what the input video resolution, it will seamlessly adjust to the target video output format.

There are no external packages needed, since the API uses a map structure to specify the video composition. The only package you need is "requests," which comes preinstalled with Python.

Begin by selecting the desired output resolution, such as 1080p (1920 x 1080). Then, define two video elements. The first element represents the background video, which can be blurred to your preference by adjusting the "blur_radius" property. Additionally, you can darken the background slightly using the "color_overlay" property. Remember to mute the background video by using the "volume" property to remove the audio track from the element.

The second element is the foreground video. Make sure to set the "fit" property to "contain" to ensure it perfectly fits within the frame of the final video. Note that neither element requires a specified duration since the output video length is automatically calculated.

If you prefer a visual editor, check out the video editor. This online tool allows you to design custom video templates using an interactive editor. You can then export these designs to your Python script to automate any video processing task.

Edit this Code Example in the Video Editor

Easily design your own video templates using the online editor. Then, export your templates as JSON and integrate them into your Python application to create any kind of video – completely through code.

Related Python Video Editing Questions

Start automating today

Start with a full-featured trial with 50 credits, no credit card required.
Get started for free