OpenCV:完成后如何重新启动视频? [英] OpenCV: how to restart a video when it finishes?

查看:126
本文介绍了OpenCV:完成后如何重新启动视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在播放视频文件,但播放完后如何再次播放?

I'm playing a video file, but how to play it again when it finishes?

哈维尔

推荐答案

如果您想一遍又一遍地重新启动视频(也就是循环播放),您可以使用 if 语句来确定帧数何时达到 cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) 然后将帧数和 cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, num) 重置为相同的值.我正在使用 OpenCV 2.4.9 和 Python 2.7.9,下面的示例不断为我循环播放视频.

If you want to restart the video over and over again (aka looping it), you can do it by using an if statement for when the frame count reaches cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) and then resetting the frame count and cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, num) to the same value. I'm using OpenCV 2.4.9 with Python 2.7.9 and the below example keeps looping the video for me.

import cv2

cap = cv2.VideoCapture('path/to/video') 
frame_counter = 0
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    frame_counter += 1
    #If the last frame is reached, reset the capture and the frame_counter
    if frame_counter == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
        frame_counter = 0 #Or whatever as long as it is the same as next line
        cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 0)
    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

它也可以重新捕获视频而不是重置帧数:

It also works to recapture the video instead of resetting the frame count:

if frame_counter == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
    frame_counter = 0
    cap = cv2.VideoCapture(video_name)

这篇关于OpenCV:完成后如何重新启动视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆