This chapter aims to document how the author accelerates video playback, such as increasing it by 100 times, it seems cv2 is not able to set 1000 FPS, hence I decided to ignore some frame so as to accelerate the video.

# Importing all necessary libraries
import cv2
import os
import math


def makeVideo(path, video_name):

    # open file
    cam = cv2.VideoCapture(path)
    try:
        if not os.path.exists('data'):
            os.makedirs('data')
    except OSError:
        print('Error: Creating directory of data')

    # get FPS of input video
    fps = cam.get(cv2.CAP_PROP_FPS)
    fps_new = 100
    # 总帧数(frames)
    frames = cam.get(cv2.CAP_PROP_FRAME_COUNT)
   

    height = 0;
    width = 0;
    layers = 0;

    video = cv2.VideoWriter(video_name, 0, fps_new, (int(cam.get(3)), int(cam.get(4))) )
    index = 0;

    allFrames = []
    add = 0
    ttt = 0
    while cam.isOpened():
        text = "index={}".format(index)
        ret, frame = cam.read()
        if not ret:
            break
        #####make some condition adjustment here, need to repeat add video.write(frame) in some condition

        if index>2317 and index<28060:

            if  ttt % 10 == 0:
                video.write(frame)
            ttt = ttt + 1
        index = index + 1
        """
        if index<2016:
            tt = 1000 - index  ##max = 1016,
            length = math.floor(tt / 30)  ###max 30
            t = 0
            for t in range(length):
                video.write(frame)

        if index>=28060 and index<=28319:
            t = 0
            for t in range(math.floor(fps_new/fps)):
                video.write(frame)

        if index>=2016 and index<=2316:
            t = 0
            for t in range(math.floor(fps_new/fps)): ###50 20 1000
                video.write(frame)

        if index>2317 and index<28060: #skip this guy 2317
            buffer = 3000
            if index<=2317+buffer:
                tt = index - 2317  ##max = buffer,
                length = math.floor(fps_new/fps) - math.floor(tt/60)
                t = 0
                for t in range(length):
                    video.write(frame)
            elif index>=28060-buffer:
                tt = abs(index - 28060) ##max = buffer
                length = math.floor(fps_new/fps) - math.floor(tt / 60)
                t = 0
                for t in range(length):
                    video.write(frame)
            else:
                video.write(frame)
            """

    cam.release()
    video.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':

    video_name = 'New_Camera_standard2.mp4'
    pathVideo = "E:\\Camera_2.mp4"
    makeVideo(pathVideo, video_name)



By Admin

Think-Math Website