In this article, we will use the OpenCV library to add dynamic backgrounds to the video through a detailed Python script. This script will guide you how to read two video files, one as foreground and the other as background, and synthesize them into a video where a specific area of the foreground video will display the contents of the background video.
Add background dynamic picture-in-picture to the video.
Code parsing and commenting
# Video file pathinput_video_path = 'sc/input_video.mp4' # Enter the video file pathoutput_video_path = 'output_video_bg03.mp4' # Output video file pathbackground_video_path = 'sc/bg_03b.mp4' # MP4 video background file path # Background transparency (0.0 is completely transparent, 1.0 is completely opaque)background_opacity = 0.6 # Maximum number of frames for background video (if you need to cut off background video)max_background_frames = 1000 # Read video filescap = (input_video_path) # Read the foreground videobackground_cap = (background_video_path) # Read background video # Get the size of the video frameret, frame = () # Read the first frameif not ret: print("Cannot read video file") raise Exception("Cannot read video file") # If it cannot be read, throw an exception height, width = [:2] # Get the height and width of the frame # Define the dimension variable of a solid black rectangletop_bar_height = int(height * 0.30) # The height of the top black solid rectangle is set to 30% of the total video heightbottom_bar_height = int(height * 0.15) # The height of the bottom black solid rectangle is set to 15% of the total video height # Define the position and size of the maskmask_top_margin = top_bar_height # Mask top distancemask_bottom_margin = bottom_bar_height # Mask bottom distancemask_height = height - mask_top_margin - mask_bottom_margin # Mask heightmask_width = width # Mask widthmask_x = 0 # Mask center x coordinatemask_y = mask_top_margin # Mask center y coordinate # Create a maskmask = ((height, width, 3), dtype=np.uint8) # Create a black mask(mask, (mask_x, mask_y), (mask_x + mask_width, mask_y + mask_height), (255, 255, 255), -1) # Draw a white rectangle in a mask # Define the video writerfourcc = cv2.VideoWriter_fourcc(*'mp4v') # Define the video encoding formatout = (output_video_path, fourcc, 20.0, (width, height)) # Create a video write object # Background video frame counterbackground_frame_index = 0 # traverse video frameswhile (): ret, frame = () # Read the current frame of the foreground video if not ret: break # If the read is finished, the loop will be jumped # Draw a black solid rectangle at the top of the video frame (frame, (0, 0), (width, top_bar_height), (0, 0, 0), -1) # Draw a black solid rectangle at the bottom of the video frame (frame, (0, height - bottom_bar_height), (width, height), (0, 0, 0), -1) # Apply the mask to the frame masked_frame = cv2.bitwise_and(frame, mask) # Read the current frame of the background video background_ret, background_frame = background_cap.read() if not background_ret or background_frame_index >= max_background_frames: # If the background video read fails or reaches the maximum frame count, restart the background video background_cap.set(cv2.CAP_PROP_POS_FRAMES, 0) background_frame_index = 0 background_ret, background_frame = background_cap.read() # Make sure the background frame size matches the video frame background_frame = (background_frame, (width, height)) # Adjust the transparency of the background frame background_frame = background_frame.astype(float) * background_opacity background_frame = (background_frame, 0, 255).astype(np.uint8) # Create a reverse mask inv_mask = cv2.bitwise_not(mask) # Mix the background frame with the mask in the reverse direction masked_background = cv2.bitwise_and(background_frame, inv_mask) # Add two mixed results result_frame = (masked_frame, masked_background) # Save frames (result_frame) # Update background video frame index background_frame_index += 1 # Free up resources() background_cap.release() () ()
Notice
The above is through the cv2 test method, how to run slower when the video file is larger than 10M.
Ending and thinking
With the above code, we successfully added a dynamic background to a video while adding a black solid rectangle to the top and bottom of the video. This technology can be widely used in video editing and special effects production, such as creating backgrounds for news programs, movie effects or enhancing the visual effects of videos.
This project also demonstrates the powerful capabilities of Python and OpenCV in video processing. With simple code, we can achieve complex video synthesis effects. This not only provides convenience for video producers, but also provides enthusiasts with a platform for learning and experimentation.
In the future, we can explore more video processing technologies, such as using deep learning to automatically replace backgrounds in videos, or developing more complex special effects to enhance video content. With the continuous advancement of technology, the boundaries of video processing are also expanding, providing us with unlimited possibilities.
This is the article about using Python and OpenCV to achieve picture-in-picture effects of dynamic backgrounds. For more related Python OpenCV dynamic background content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!