import cv2
import numpy as np# 读取视频
cap = cv2.VideoCapture('D:/video/j.mp4')
# 读取gif动图
gif = cv2.imread('D:/video/右上.jpg')# 获取视频的宽度和高度
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))# 获取图片的宽度和高度
gif_w = gif.shape[0]
gif_h = gif.shape[1]# 定义编码格式
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# 定义视频写入对象
out = cv2.VideoWriter('D:/video/1.mp4', fourcc, 20.0, (w, h))while True:ret, frame = cap.read()if ret == True:# 在视频中添加图片frame[0:gif_w, 0:gif_h] = img# 写入视频out.write(frame)cv2.imshow('frame', frame)if cv2.waitKey(1) & 0xFF == ord('q'):breakelse:breakcap.release()
out.release()
cv2.destroyAllWindows()