Welcome folks today in this blog post we will be using the ffmpeg
library to animate the logo
image at different locations
inside the video in python using ffmpeg
library in command line. All the full source code of the application is shown below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import os # Prompt the user for the video path video_path = input("Enter the path of the video: ") # Prompt the user for the logo image path logo_path = input("Enter the path of the logo image: ") # Prompt the user for the location choice location_choice = input("Enter the location choice (top, right, bottom, center): ") # Determine the x and y coordinates based on the location choice if location_choice == "top": x = "10" y = "10" elif location_choice == "right": x = "main_w-overlay_w-10" y = "(main_h-overlay_h)/2" elif location_choice == "bottom": x = "(main_w-overlay_w)/2" y = "main_h-overlay_h-10" elif location_choice == "center": x = "(main_w-overlay_w)/2" y = "(main_h-overlay_h)/2" else: print("Invalid location choice") exit() # Build the FFmpeg command ffmpeg_command = f"ffmpeg -i {video_path} -i {logo_path} -filter_complex \"[0:v][1:v]overlay=x={x}:y={y}\" -pix_fmt yuv420p -c:a copy output.mp4" # Execute the FFmpeg command os.system(ffmpeg_command) print("Video with logo image added at the specified location has been saved as output.mp4") |
As you can see we have the simple python
script in which we are having the simple command line
app in which we are asking the user
to input the path
of the video to attach the image
logo and then we are asking the location
in which you need to insert the logo. It can be either top,left
center and bottom. And then we are calculating the coordinates accordingly and then we are executing the ffmpeg command using the os
module.