人工按P键植入广告
import cv2
import os
import random
video_dir = 'C:\\Elag\\video\\'
video_list = os.listdir(video_dir)
random.shuffle(video_list)
video_capture = cv2.VideoCapture('C:\\Users\\user\\PycharmProjects\\Elag\\gmly.mp4')
c = 10
fps = video_capture.get(5)
print(video_capture.isOpened())
print("fps", video_capture.get(5))
print("COUNT", video_capture.get(7))
size = (int(video_capture.get(3)), int(video_capture.get(4)))
print(size)
frame_index = 0
flag = 0
success, bgr_image = video_capture.read()
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
v = cv2.VideoWriter('gmly_ad.mp4', fourcc, fps, size)
while success:
v.write(bgr_image)
cv2.imshow('frame', bgr_image)
success, bgr_image = video_capture.read()
frame_index = frame_index + 1
c = cv2.waitKey(int(1000 / fps))
if (c == 112):
print('pause')
video_file = ''
for filename in video_list:
if filename[-4:] == '.mp4':
video_file = video_dir + filename
print(video_file)
video_list.remove(filename)
break
ad = cv2.VideoCapture(video_file)
fps1 = ad.get(5)
size1 = (int(ad.get(3)), int(ad.get(4)))
if size1 != (1280, 720):
ad.release()
else:
success1, bgr = ad.read()
while success1:
cv2.imshow('ad', bgr)
v.write(bgr)
success1, bgr = ad.read()
cv2.waitKey(int(1000 / fps1))
ad.release()
cv2.destroyWindow('ad')
cv2.waitKey(0)
if c == 27:
cv2.destroyAllWindows()
break
video_capture.release()
v.release()
每隔90秒自动植入广告
import cv2
import os
import random
video_dir = 'C:\\Elag\\video\\'
video_list = os.listdir(video_dir)
random.shuffle(video_list)
video_capture = cv2.VideoCapture('C:\\Users\\user\\PycharmProjects\\Elag\\gmly.mp4')
fps = video_capture.get(5)
print(video_capture.isOpened())
print("fps", video_capture.get(5))
print("COUNT", video_capture.get(7))
size = (int(video_capture.get(3)), int(video_capture.get(4)))
print(size)
frame_index = 0
flag = 0
delay = 90
iscontinue = False
success, bgr_image = video_capture.read()
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
v = cv2.VideoWriter('gmly_ad.mp4', fourcc, fps, size)
while success:
v.write(bgr_image)
cv2.imshow('frame', bgr_image)
success, bgr_image = video_capture.read()
frame_index += + 1
k = cv2.waitKey(int(1000 / fps))
if frame_index % int(delay * fps) == 0:
flag += 1
print('pause')
video_file = ''
for filename in video_list:
if filename[-4:] == '.mp4':
video_file = video_dir + filename
print(video_file)
video_list.remove(filename)
break
ad = cv2.VideoCapture(video_file)
fps1 = ad.get(5)
count1 = ad.get(7)
print(ad.isOpened())
print("fps1", ad.get(5))
print("COUNT1", ad.get(7))
size1 = (int(ad.get(3)), int(ad.get(4)))
print(size1)
if size1 != (1280, 720):
print('release')
ad.release()
else:
print('ad')
success1, bgr = ad.read()
while success1:
cv2.imshow('ad', bgr)
v.write(bgr)
success1, bgr = ad.read()
cv2.waitKey(int(1000 / fps1))
ad.release()
cv2.destroyWindow('ad')
print('finish')
if k == 27:
cv2.destroyAllWindows()
break
video_capture.release()
v.release()