我正在尝试保存视频,但它无法正常工作.我按照openCV文档中的说明进行操作.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_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)
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
怎么了?
Nur*_*hid 23
试试这个 .它对我有用.
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()
Run Code Online (Sandbox Code Playgroud)
ปรี*_*ากร 10
就我而言,我发现Writer的大小必须与相机或文件的帧大小相匹配.这样我首先读取帧大小并应用于编写器设置如下.
(grabbed, frame) = camera.read()
fshape = frame.shape
fheight = fshape[0]
fwidth = fshape[1]
print fwidth , fheight
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (fwidth,fheight))
Run Code Online (Sandbox Code Playgroud)
请确保设置正确的宽度和高度。你可以像下面这样设置
cv2.VideoWriter('output.avi', fourcc, 20.0, (int(cap.get(3)), int(cap.get(4))))
Run Code Online (Sandbox Code Playgroud)
您需要像这样获取捕获的确切大小:
import cv2
cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) + 0.5)
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) + 0.5)
size = (width, height)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('your_video.avi', fourcc, 20.0, size)
while(True):
_, frame = cap.read()
cv2.imshow('Recording...', frame)
out.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
小智 6
在jveitchmichaelis https://github.com/ContinuumIO/anaconda-issues/issues/223提供了一个完整的答案。在这里,我复制了他的答案:
OpenCV中的文档说(隐藏),您只能使用OpenCV3写入avi。我是否能确定这是否成立,但我无法写其他任何东西。
但是,OpenCV主要是计算机视觉库,而不是视频流,编解码器和编写库。因此,开发人员试图使这一部分尽可能简单。由于此视频容器OpenCV仅支持avi扩展程序(第一个版本)。
来源:http://docs.opencv.org/3.1.0/d7/d9e/tutorial_video_write.html
我的设置:我使用MSVC 2015(包括ffmpeg)从源代码构建了OpenCV 3。我还从Cisco下载并安装了XVID和openh264,并将其添加到PATH中。我正在运行Anaconda Python3。我还下载了最新版本的ffmpeg,并将bin文件夹添加到了我的路径中,尽管将其放入OpenCV并没有什么不同。
我正在Win 10 64位中运行。
该代码在我的计算机上似乎可以正常工作。它将生成一个包含随机静态数据的视频:
Run Code Online (Sandbox Code Playgroud)writer = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 30,(640,480)) for frame in range(1000): writer.write(np.random.randint(0, 255, (480,640,3)).astype('uint8')) writer.release()我通过反复试验中学到了一些东西:
- 仅使用“ .avi”,它只是一个容器,编解码器很重要。
指定框架尺寸时要小心。在构造函数中,您需要将帧大小传递为(列,行),例如640x480。但是,您传入的数组索引为(行,列)。在上面的示例中看到如何切换?
如果输入图像的大小与VideoWriter的大小不同,它将失败(通常是无声的)
- 仅传递8位图像,如果需要则手动转换数组(.astype('uint8'))
- 其实,没关系,只是经常投。即使使用cv2.imread加载图像,也需要强制转换为uint8 ...
- 如果您不传递3通道8位图像,MJPG将失败。我至少为此断言失败。
- XVID还需要一个3通道图像,但如果不这样做,则会静默失败。
- H264似乎可以使用单通道图像
- 如果您需要原始输出,例如来自机器视觉相机的输出,则可以使用“ DIB”。有时可以使用“ RAW”或空编解码器。奇怪的是,如果我使用DIB,则会收到ffmpeg错误,但视频保存良好。如果使用RAW,则没有错误,但是Windows Video Player无法打开它。在VLC中一切都很好。
最后,我认为关键是OpenCV并非旨在成为视频捕获库-它甚至不支持声音。VideoWriter很有用,但是在99%的时间里,最好将所有图像保存到文件夹中,并使用ffmpeg将其转换为有用的视频。
这个答案涵盖了变量的含义,重要的是,输出大小应与输入帧和视频大小相同。
import cv2
save_name = "output.mp4"
fps = 10
width = 600
height = 480
output_size = (width, height)
out = cv2.VideoWriter(save_name,cv2.VideoWriter_fourcc('M','J','P','G'), fps , output_size )
cap = cv2.VideoCapture(0) # 0 for webcam or you can put in videopath
while(True):
_, frame = cap.read()
cv2.imshow('Video Frame', frame)
out.write(cv2.resize(frame, output_size ))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
我也遇到了同样的问题,但是当我使用“MJPG”而不是'XVID'
我用了
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
Run Code Online (Sandbox Code Playgroud)
代替
fourcc = cv2.VideoWriter_fourcc(*'XVID')
Run Code Online (Sandbox Code Playgroud)
这个答案仅在 MacOS 中进行了测试,但它可能也适用于 Linux 和 Windows。
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Get the Default resolutions
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
# Define the codec and filename.
out = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
# write the 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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
67373 次 |
| 最近记录: |