gpt4 book ai didi

python - OpenCV:完成后如何重新启动视频?

转载 作者:太空宇宙 更新时间:2023-11-03 22:06:47 26 4
gpt4 key购买 nike

我正在播放一个视频文件,但是播放结束后如何再次播放?

哈维尔

最佳答案

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

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()

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

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

关于python - OpenCV:完成后如何重新启动视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10057234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com