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

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.

主站蜘蛛池模板: 古丈县| 平泉县| 永泰县| 海门市| 永清县| 小金县| 旌德县| 资中县| 贡山| 来凤县| 泽库县| 武乡县| 仙游县| 孝感市| 米易县| 筠连县| 冕宁县| 健康| 清河县| 阳城县| 广平县| 错那县| 灵山县| 柘城县| 微山县| 平阴县| 民县| 龙泉市| 石家庄市| 军事| 东宁县| 安新县| 贵州省| 汤阴县| 诸暨市| 秀山| 江川县| 屏南县| 长白| 高唐县| 轮台县|