OpenCV 视频植入广告

人工按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')  # 读入视频文件
# video_capture = cv2.VideoCapture('Bing_S01E60_Wellies_ad.avi')  # 读入视频文件
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):# p 按键
        print('pause')
        video_file = ''
        for filename in video_list:
            # print(filename)
            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:  # 按下ESC退出
        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:  # 按下ESC退出
        cv2.destroyAllWindows()
        break
video_capture.release()
v.release()

你可能感兴趣的:(OpenCV,python)