【发布时间】:2014-09-30 16:20:26
【问题描述】:
我使用以下代码捕获视频文件,翻转并保存。
#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 video delay frame-rate