python opencv打开视频卡顿_OpenCV Python视频播放 – 如何为cv2.waitKey()设置正确的延迟...

我使用以下代码捕获视频文件,翻转并保存.

#To save a Video File

import numpy as np

import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object

fourcc = cv2.cv.CV_FOURCC(*'XVID')

out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):

ret, frame = cap.read()

if ret==True:

frame = cv2.flip(frame,0)

# write the flipped frame

out.write(frame)

cv2.imshow('frame',frame)

if cv2.waitKey(1) & 0xFF == ord('q'):

break

else:

break

# Release everything if job is finished

cap.release()

out.release()

cv2.destroyAllWindows()

该程序将输出保存为output.avi

现在,为了播放视频文件,我使用了以下程序

#Playing Video from File

import numpy as np

import cv2

cap = cv2.VideoCapture('output.avi')

print cap.get(5) #to display frame rate of video

#print cap.get(cv2.cv.CV_CAP_PROP_FPS)

while(cap.isOpened()):

ret, frame = cap.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #convert to grayscale

cv2.imshow('frame',gray)

if cv2.waitKey(1) & 0xFF == ord('q'):

break

cap.release()

cv2.destroyAllWindows()

该程序播放从第一个程序保存的视频文件output.avi.问题是,这个视频出现了快进.所以,我尝试更改cv2.waitKey()的延迟值.当我放100时,视频看起来很好.我怎么知道放在哪个值?它应该与帧速率有关吗?我检查了output.avi的帧速率(见第二个程序中的cap.get(5)行)并得到20.但如果我使用20作为cv2.waitKey()的延迟,则视频仍然太快.

任何帮助,将不胜感激.

你可能感兴趣的:(python,opencv打开视频卡顿)