Blending of two videos using Python
Last Updated : 03 Jan, 2023
Prerequisite: Addition and Blending of images using OpenCV in Python
In this article, we will be showing you how to effectively blend two videos together. But for that, we will first see what is Alpha Blending.
Alpha Blending
Alpha blending is a curved mix of two tones taking into consideration transparency impacts in computer illustrations. The estimation of alpha in the color code goes from 0.0 to 1.0, where 0.0 addresses a completely transparent tone, and 1.0 addresses a completely dark tone. The estimation of the subsequent color when color Value1 with an alpha value of Alpha is drawn over a foundation of color Value0 is given by:
Value = Value0(1.0 - Alpha) + Value1(Alpha)
The alpha part might be utilized to mix to red, green and blue segments similarly, as in 32-bit RGBA, or, then again, there might be three alpha qualities determined relating to every one of the essential tones for spectral tone shifting.
Now, the second thing we will be using is OpenCV which is a library of programming functions that focuses on real-time computer vision. But, before moving forward we will keep a note of a few things.
Important Points
In order for our program to work perfectly, you've to make sure:
- The resolution and frame rate of the two input videos must be exactly same (in code it's using 1920x1080 format so use that only, otherwise you've to set the 'h', 'w' values accordingly with a resolution of your input videos which we will see later in the code)
- The code is written, taking into consideration that the background video's duration is slightly greater than or equal to the foreground video's duration, so it's preferable for you as well-to-do the same with your background and foreground input videos otherwise you've to assign the "ret" value to the background instead of foreground which we will see later in the code
- Rename your video files with some unique name because sometimes it might cause an error if there are two video files of the same name and the path of the two input video files must be provided accurately as well.
- The foreground video must be a combination of some solid color background (preferably black color) and with some movement of the subject on it.
Methodology
We will be taking two videos as input, one will be our background video and the second one will be our foreground video. The main work is to remove the solid color background of this foreground video so that we're left with the subject only (of foreground video) with its transparent background. After making this change, the foreground video will then be put on the background video, which will give an illusion of a single video with some effect on it but actually, there will be two videos blended together. This magic is done with the help of OpenCV and Python.
Step-by-step Approach
- Import required modules
- Add paths and capture both the input videos.
- Iterate through each frame of both the videos and blend them together using Alpha Blending.
- Play the output video generated.
Implementation
Input:
Foreground Video
Background Video
Notice that the foreground video contains only the particles with a solid black background, thus for better results, it is preferable to take foreground videos and background videos like these.
Python3 # importing necessary packages import numpy as np import cv2 # assigning path of foreground video path_1 = r"C://Users//Lenovo//Desktop//Python Workshop//z.mp4" fg = cv2.VideoCapture(path_1) # assigning path of background video path_2 = r"C://Users//Lenovo//Desktop//Python Workshop//v.mp4" bg = cv2.VideoCapture(path_2) h, w = 1080, 1920 while True: # Reading the two input videos # we have taken "ret" here because the duration # of bg video is greater than fg video, ret, foreground = fg.read() # if in your case the situation is opposite # then take the "ret" for bg video _, background = bg.read() # if foreground array is not empty which # means actual video is still going on if ret: # creating the alpha mask alpha = np.zeros_like(foreground) gray = cv2.cvtColor(foreground, cv2.COLOR_BGR2GRAY) alpha[:, :, 0] = gray alpha[:, :, 1] = gray alpha[:, :, 2] = gray # converting uint8 to float type foreground = foreground.astype(float) background = background.astype(float) # normalizing the alpha mask inorder # to keep intensity between 0 and 1 alpha = alpha.astype(float)/255 # multiplying the foreground # with alpha matte foreground = cv2.multiply(alpha, foreground) # multiplying the background # with (1 - alpha) background = cv2.multiply(1.0 - alpha, background) # adding the masked foreground # and background together outImage = cv2.add(foreground, background) # resizing the masked output ims = cv2.resize(outImage, (980, 540)) # showing the masked output video cv2.imshow('Blended', ims/255) # if the user presses 'q' then the # program breaks from while loop if cv2.waitKey(1) & 0xFF == ord('q'): break # if the actual video is over then there's # nothing in the foreground array thus # breaking from the while loop else: break print('Video Blending is done perfectly')
Output:
Notice that in this output video we can see that the sand is moving in the air in a hot desert area, which in reality is the blended output of the particle video(used as foreground) and desert video(used as background). Also, you can always press "q" from your keyboard to break the loop and quit the program.
So in this way, you can blend two videos and if done correctly, this method can also provide a professional edit look as well.
Similar Reads
Faces Blur in Videos using OpenCV in Python OpenCV is an open-source cross-platform library for various operating systems, including Windows, Linux, and macOS, for computer vision, machine learning, and image processing. With the help of OpenCV, we can easily process images and videos to recognize objects, faces, or even someone's handwriting
3 min read
Get video duration using Python - OpenCV OpenCV is one of the most popular cross-platform libraries and it is widely used in Deep Learning, image processing, video capturing, and many more. In this article, we will learn how to get the duration of a given video using python and computer vision. Prerequisites: Opencv moduledatetime module
1 min read
Playing Youtube Video using Python In this article, we will see how we can play youtube video in python. In order to play youtube videos in python we need pafy and vlc module. Pafy is a Python library to download YouTube content and retrieve metadata. Below is the command to install pafy pip install pafy VLC : is a python library to
2 min read
Python | Play a video using OpenCV OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos. Letâs see how to play a video using the OpenCV Python. To capture a video, we need to crea
2 min read
Creating a Slow Motion Video Using OpenCV - Python In this article, we will try to create a slow-motion video using OpenCV( Open Source Computer Vision) library in Python. OpenCV ( is an open-source computer vision and machine learning software library. Now a video is basically a set of moving pictures moving at a rate of about 24 frames per second
4 min read
Display date and time in videos using OpenCV - Python OpenCV-Python is a library of Python bindings designed to solve computer vision problems. It can process images and videos to identify objects, faces, or even the handwriting of a humanNote: For more information, refer to Introduction to OpenCVÂ Display date and time in videos It sometimes becomes n
2 min read