官术网_书友最值得收藏!

Using the properties – playing a video backwards

To see how we can use the aforementioned properties, we are going to understand the read_video_file_backwards.py script, which uses some of these properties to load a video and output it backwards, showing the last frame of the video first and so on. We are going to use the following properties:

  • cv2.CAP_PROP_FRAME_COUNT: This property provides the total number of frames
  • cv2.CAP_PROP_POS_FRAMES: This property provides the current frame

The first step is to get the index of the last frame:

# We get the index of the last frame of the video file
frame_index = capture.get(cv2.CAP_PROP_FRAME_COUNT) - 1

Therefore, we set the current frame to read to this position:

 # We set the current frame position
capture.set(cv2.CAP_PROP_POS_FRAMES, frame_index)

This way, we can read this frame as usual:

 # Capture frame-by-frame from the video file
ret, frame = capture.read()

Finally, we decrement the index in order to read the next frame from the video file:

 # Decrement the index to read next frame
frame_index = frame_index - 1

The full code is provided in the read_video_file_backwards.py script. This script can be easily modified to save the resulting video playing backwards (not only showing it). This script is proposed in the Question section.

主站蜘蛛池模板: 屏东市| 安顺市| 曲水县| 溧水县| 巴东县| 华宁县| 色达县| 华亭县| 晋州市| 达拉特旗| 临沭县| 平南县| 大悟县| 大丰市| 临漳县| 玛沁县| 揭阳市| 宜宾县| 高雄市| 信丰县| 南和县| 宁远县| 隆尧县| 赣榆县| 凉山| 秭归县| 尖扎县| 临朐县| 通海县| 平利县| 曲周县| 南安市| 松原市| 辉县市| 奉节县| 威远县| 马公市| 麻栗坡县| 皮山县| 乌海市| 龙口市|