import cv2
import numpy as np
# 在视频中插入文字、图片
def video_edit(video_path):
# 打开视频获取视频信息
video = cv2.VideoCapture(video_path)
fps = video.get(cv2.CAP_PROP_FPS)
frameCount = video.get(cv2.CAP_PROP_FRAME_COUNT)
size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# 新建视频
videoWriter = cv2.VideoWriter('trans.mp4', cv2.VideoWriter_fourcc(*'MP4V'), fps, size)
# 读取视频
success, frame = video.read()
index = 1
while success:
# 为视频添加字幕信息
cv2.putText(frame, 'fps: ' + str(fps), (0, 200), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,0,255), 5)
cv2.putText(frame, 'count: ' + str(frameCount), (0, 300), cv2.FONT_HERSHEY_SIMPLEX,2, (255,0,255), 5)
cv2.putText(frame, 'frame: ' + str(index), (0, 400), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,0,255), 5)
cv2.putText(frame, 'time: ' + str(round(index / 24.0, 2)) + "s", (0,500), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,0,255), 5)
# cv2.imshow("new video", frame)
cv2.waitKey(int(1000 / int(fps)))
# 添加图片在此增加frame
videoWriter.write(frame)
success, frame = video.read()
index += 1
video.release()
# 生成特定文字视频
def video_create():
fps = 60
size = (1920, 1080)
videoWriter = cv2.VideoWriter('test.mp4', cv2.VideoWriter_fourcc(*'MP4V'), fps, size)
for i in range(60):
i = i + 1
# 生成白色图片
frame = np.zeros([1080, 1920, 3], np.uint8) + 255
# 打标,图片,要添加的文字,文字添加到图片上的位置,字体的类型,字体大小,字体颜色,字体粗细
cv2.putText(frame, str(i), (800, 600), cv2.FONT_HERSHEY_SIMPLEX, 10, (0, 0, 0), 5)
cv2.waitKey(int(1000 / int(fps)))
videoWriter.write(frame)
|