OpenCV-Python actual combat (7) - OpenCV realizes the effect of vibrato video reverse playback

1. Demand analysis

Reference: Ten lines of Python code to make a video playback artifact . Since I am learning OpenCV recently, I try to use OpenCV to implement it and apply what I have learned (toss around).

  1. The video needs to be reversed, so the video cv.VideoCapture needs to be read ;
  2. Get the picture of each frame of the video using cv.VideoCapture.read() ;
  3. Use a list to save the picture of each frame;
  4. Use cv.VideoWriter to write pictures to the video in reverse order.

2. Realize the effect

2.1 Normal video

Enter a picture description

2.2 Reverse the video

Enter a picture description

3. Read the video

  1. Use cv.VideoCapture to read the video;
  2. Use cap.get to get the frame rate of the video;
  3. Create a list images to save each frame image;
  4. Check whether the video capture is initialized successfully cap.isOpened();
  5. Read the next frame of video image ret, frame = cap.read();
  6. Judging whether the image has been read if ret is True;
  7. Store the read pictures into the image list;
  8. Finish the loop, close the video file or device, release the object;
  9. Returns the video's image list and the video's frame rate.
# 读取视频,将视频按照帧导出图片
def get_video_images(video_path):
  cap = cv.VideoCapture(video_path)
  fps = cap.get(cv.CAP_PROP_FPS)
  images = []
  while cap.isOpened():
    ret, frame = cap.read()  # 读取下一帧视频图像
    if ret is True:
      images.append(frame)
      key = cv.waitKey(round(fps))
      if key == ord('q'):
        break
    else:
      break
  cap.release()
  return {"images": images, "fps": fps}

4. Write video

  1. Get the width and height of the video through the picture;
  2. Set the video format cv.VideoWriter_fourcc;
  3. Create a video write object cv.VideoWriter;
  4. Cycle through the list of video images in reverse order;
  5. Write the next frame of video writer.write;
  6. Finish the loop, turn off video writing, and release the object.
# 转MP4
def create_mp4(filename, fps, images):
  h,w,c = images[0].shape
  fourcc = cv.VideoWriter_fourcc(*'mp4v')
  writer = cv.VideoWriter(filename, fourcc, fps, (w,h))
  for frame in images[::-1]:
    key = cv.waitKey(round(fps))
    if key == ord('q'):
      break
    writer.write(frame)
  writer.release()

5. Complete code

'''
version: 1.0.0
Author: Rattenking
Date: 2023-01-31 10:33:16
'''
import cv2 as cv

# 读取视频,将视频按照帧导出图片
def get_video_images(video_path):
  cap = cv.VideoCapture(video_path)
  fps = cap.get(cv.CAP_PROP_FPS)
  images = []
  while cap.isOpened():
    ret, frame = cap.read()  # 读取下一帧视频图像
    if ret is True:
      images.append(frame)
      key = cv.waitKey(round(fps))
      if key == ord('q'):
        break
    else:
      break
  cap.release()
  return {"images": images, "fps": fps}

# 转MP4
def create_mp4(filename, fps, images):
  h,w,c = images[0].shape
  fourcc = cv.VideoWriter_fourcc(*'mp4v')
  writer = cv.VideoWriter(filename, fourcc, fps, (w,h))
  for frame in images[::-1]:
    key = cv.waitKey(round(fps))
    if key == ord('q'):
      break
    writer.write(frame)
  writer.release()

if __name__ == "__main__":
  imgs = get_video_images('./images/Megamind.avi')
  create_mp4('./images/Megamind.mp4', images=imgs.get("images"), fps=imgs.get("fps"))

6. Summary

  1. OpenCV implements video playback, the principle is to read the video, get the image of each frame, reverse the order of the frames, and store the new video;
  2. If you understand the implementation, you can also realize special effects such as reverse playback of the middle part of the video, zooming in and shaking of the image in the video part.

Guess you like

Origin blog.csdn.net/m0_38082783/article/details/130588006