opencv 实现视频倒放

        之前使用ffmpeg命令倒放视频占用的内存有点多(不知道是否参数设置的问题),所以用opencv实现视频倒放实现了一下视频倒放。

import cv2
import os
import numpy as np
def video_Inverted(videoFileName,outputFileName):
    cap = cv2.VideoCapture(videoFileName)
    fps = cap.get(cv2.CAP_PROP_FPS)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fourcc = cv2.VideoWriter_fourcc(*'MJPG')
    videoWriter = cv2.VideoWriter(outputFileName+".avi", fourcc, fps, (width, height))
    frame_index = 0
    while (cap.isOpened()):
        ret, frame = cap.read()
        if ret:
            cv2.imwrite(str(frame_index)+".png",frame)
            frame_index = frame_index+1
        else:
            break
    for frame_re_index in range(frame_index,0,-1):
        print("读取张",frame_re_index)
        videoWriter.write(cv2.imread(str(frame_re_index)+".png"))
        #os.remove(str(frame_re_index)+".png")
    cap.release()
    videoWriter.release()
    cv2.destroyAllWindows()


if __name__ == "__main__":
    video_Inverted("***.mp4","output")

你可能感兴趣的:(opencv,音视频,ffmpeg)