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

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.

主站蜘蛛池模板: 平顶山市| 峨边| 从江县| 霍邱县| 松阳县| 延川县| 云阳县| 禹州市| 福建省| 屏东县| 古浪县| 如皋市| 淮滨县| 金平| 涟源市| 阿巴嘎旗| 景洪市| 北票市| 阆中市| 凉山| 姜堰市| 南川市| 大港区| 尚志市| 达州市| 肥乡县| 杭锦后旗| 沽源县| 仙游县| 安徽省| 五河县| 吐鲁番市| 环江| 北碚区| 万源市| 缙云县| 仙游县| 合肥市| SHOW| 佛学| 台湾省|