这是我保存web_cam流的代码。它可以正常工作,但是输出视频文件有问题。
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
#fourcc = cv2.cv.CV_FOURCC(*'DIVX')
#out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
out = cv2.VideoWriter('output.avi', -1, 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()
答案 0 :(得分:4)
这里有一些简单的代码可以将帧保存到视频文件中。由于cv2.VideoCapture.read()
被阻止,我建议创建另一个线程来获取帧。这可能会很昂贵,并且会导致等待时间,因为主线程必须等待直到获得帧为止。通过将此操作放到一个单独的线程中,该线程仅专注于抓取帧并在主线程中处理/保存帧,由于I / O延迟减少,因此可显着提高性能。您也可以尝试使用其他编解码器,但是使用MJPG是安全的,因为它已内置在OpenCV中。
from threading import Thread
import cv2
class WebcamVideoWriter(object):
def __init__(self, src=0):
# Create a VideoCapture object
self.capture = cv2.VideoCapture(src)
# Default resolutions of the frame are obtained (system dependent)
self.frame_width = int(self.capture.get(3))
self.frame_height = int(self.capture.get(4))
# Set up codec and output video settings
self.codec = cv2.VideoWriter_fourcc('M','J','P','G')
self.output_video = cv2.VideoWriter('output.avi', self.codec, 30, (self.frame_width, self.frame_height))
# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
def show_frame(self):
# Display frames in main program
if self.status:
cv2.imshow('frame', self.frame)
# Press Q on keyboard to stop recording
key = cv2.waitKey(1)
if key == ord('q'):
self.capture.release()
self.output_video.release()
cv2.destroyAllWindows()
exit(1)
def save_frame(self):
# Save obtained frame into video output file
self.output_video.write(self.frame)
if __name__ == '__main__':
webcam_videowriter = WebcamVideoWriter()
while True:
try:
webcam_videowriter.show_frame()
webcam_videowriter.save_frame()
except AttributeError:
pass
答案 1 :(得分:3)
由于错误的帧速率和帧分辨率,输出文件已损坏。使用此代码:
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))
我们将每秒fps /帧速率设置为20。这是不正确的。此外,框架的宽度和高度是错误的。我通过从捕获的web_cam配置文件中获取fps,宽度,高度来解决。
cap = cv2.VideoCapture(0) #web-cam capture
fps = cap.get(cv2.CAP_PROP_FPS)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # float
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) # float
out = cv2.VideoWriter('output.avi', -1,fps, (int(width), int(height)))
答案 2 :(得分:2)
我在功能codec
中添加了parameter
cv2.videowriter
。
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
#fourcc = cv2.cv.CV_FOURCC(*'DIVX')
#out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
fps = cap.get(cv2.CAP_PROP_FPS)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # float
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
codec = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
out = cv2.VideoWriter('output.avi',codec,fps, (int(width),\
int (height)))
#out = cv2.VideoWriter('output.avi', -1, 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()
希望您可能会发现我的代码和您的代码有什么不同。
我的现在工作。使用MJPG
编解码器进行.avi
扩展
缩进有点混乱,请原谅,因为我是第一次使用。
video
文件不再损坏。
我从以下地方获得了信息:Link