VideoWriter 输出损坏的视频文件
VideoWriter outputs corrupted video file
这是我用来保存 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()
由于错误的帧速率和帧分辨率,输出文件已损坏。使用此代码:
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))
我们将 fps/frame 速率设置为每秒 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)))
下面是一些将帧保存到视频文件中的简单代码。我建议创建另一个线程来获取帧,因为 cv2.VideoCapture.read()
正在阻塞。这可能代价高昂并且会导致延迟,因为主线程必须等待直到获得帧。通过将此操作放入一个单独的线程中,该线程仅专注于抓取帧和 processing/saving 主线程中的帧,由于 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
我添加了 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
这是我用来保存 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()
由于错误的帧速率和帧分辨率,输出文件已损坏。使用此代码:
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))
我们将 fps/frame 速率设置为每秒 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)))
下面是一些将帧保存到视频文件中的简单代码。我建议创建另一个线程来获取帧,因为 cv2.VideoCapture.read()
正在阻塞。这可能代价高昂并且会导致延迟,因为主线程必须等待直到获得帧。通过将此操作放入一个单独的线程中,该线程仅专注于抓取帧和 processing/saving 主线程中的帧,由于 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
我添加了 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