使用OpenCV在Python中构建回旋镖视频的更有效方法?

时间:2018-08-27 06:42:02

标签: python opencv opencv3.0

我正在尝试从视频中截取一个小片段,并以飞旋镖风格将其保存到文件中(向前播放,然后反向播放)。

使用Python和OpenCV,是否有比将每个帧存储到缓冲区更好的方法?

谢谢

import numpy as np
import cv2

inputPath = './kitten.mov'
outputPath = './boomerang.mp4'
(startFrameIndex, endFrameIndex) = (50,110)       # starting/ending frames

cap = cv2.VideoCapture(inputPath)
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
size = (int(width), int(height))
print('size = ' + str(size))
fourcc = int(cv2.VideoWriter_fourcc(*'MP4V'))

writer = cv2.VideoWriter(outputPath, fourcc, fps, size)

frameBuffer = []

# grab the frames in the forward direction

cap.set(cv2.CAP_PROP_POS_FRAMES, startFrameIndex)               # set the next frame to be read
ret = True                                                      # if the frame frame has been returned
while (cap.isOpened() and ret and writer.isOpened()):
    ret, frame = cap.read()
    frameNumber = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
    if frameNumber <= endFrameIndex:
        frameBuffer.append(frame)
    else:
        break

for frame in frameBuffer:                                       # write the frames forward
    writer.write(frame)

for frame in reversed(frameBuffer):                             # write the frames in reverse
    writer.write(frame)

writer.release()
cap.release()

0 个答案:

没有答案