【问题标题】:how loading a high quality video in python-opencv and play as normally?如何在 python-opencv 中加载高质量视频并正常播放?
【发布时间】:2018-02-06 10:33:39
【问题描述】:
import numpy as np
import cv2

cap = cv2.VideoCapture('1440pHD.mp4')
while(cap.isOpened()):

    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

【问题讨论】:

  • “通常”是什么意思?
  • 加载视频时,它会逐帧播放或快进
  • 考虑将标题改为:如何在python-opencv中加载高质量视频并以正常速度播放?

标签: python-3.x numpy opencv


【解决方案1】:

如果要以正常速度播放视频,则需要找出帧速率或每秒帧数 (fps):

import cv2
if __name__ == '__main__' :

    video = cv2.VideoCapture("video.mp4");

    # Find OpenCV version
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')

    if int(major_ver)  < 3 :
        fps = video.get(cv2.cv.CV_CAP_PROP_FPS)
        print "Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}".format(fps)
    else :
        fps = video.get(cv2.CAP_PROP_FPS)
        print "Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps)

   video.release(); 

更多详情:https://www.learnopencv.com/how-to-find-frame-rate-or-frames-per-second-fps-in-opencv-python-cpp/

然后,您应该考虑 fps,而不是在 waitKey() 中等待 1 毫秒。

例如,如果您的视频是 30fps,您需要每 1000/30=33.333 毫秒播放一帧。在这种情况下,你应该做大约 waitKey(33)。

如果您需要更高的精度,您应该跟踪经过的时间(使用 time.time() 函数)和播放的帧数,并相应地调整 waitKey 参数。

如果您的视频播放速度比应有的慢,您必须跳帧

【讨论】:

    【解决方案2】:

    如果您想以正常播放速率播放视频。以下功能可以帮助您正常播放视频。

    def readVideo():
        # Read the video 
        capture = cv2.VideoCapture("video1.mp4")
        # get the versions of cv2 and convert them to a python list [major_version, minor_version, subminor_version]
        versions = str(cv2.__version__).split('.')
    
       # check if the major version integer value is less or greater than 3
       if int(versions[0]) < 3:
           # if less fps will be equal to
           fps = capture.get(cv2.cv.CV_CAP_PROP_FPS)
       else:
           # if greater fps will be
           fps = capture.get(cv2.CAP_PROP_FPS)
       while True:
           success, img = capture.read()
           cv2.imshow("Video", img)
           # On the waitKey() pass the integer value of 1000/fps as shown bellow
           if cv2.waitKey(int(1000/fps)) & 0xFF == ord('q'):
              # quit the loop when q is pressed
              break
       return capture.release() and cv2.destroyAllWindows()
    # call the function
    readVideo()
    

    祝你好运

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      相关资源
      最近更新 更多