OpenCV VideoWriter 未写入 Output.avi
OpenCV VideoWriter Not Writing to Output.avi
我正在尝试编写一段简单的代码来拍摄视频、裁剪视频并将其写入输出文件。
系统设置:
OS: Windows 10
Conda Environment Python Version: 3.7
OpenCV Version: 3.4.2
ffmpeg Version: 2.7.0
文件输入规范:
Codec: H264 - MPEG-4 AVC (part 10)(avc1)
Type: Video
Video resolution: 640x360
Frame rate: 5.056860
无法生成输出的代码(创建文件但不写入文件):
import numpy as np
import cv2
cap = cv2.VideoCapture('croptest1.mp4')
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc('F', 'M', 'P', '4')
out = cv2.VideoWriter('output.avi', fourcc, 20.0,
(int(cap.get(3)), int(cap.get(4))))
# Verify input shape
width = cap.get(3)
height = cap.get(4)
fps = cap.get(5)
print(width, height, fps)
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
# from top left =frame[y0:y1, x0:x1]
cropped_frame = frame[20:360, 0:640]
# write the clipped frames
out.write(cropped_frame)
# show the clipped video
cv2.imshow('cropped_frame', cropped_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
fourcc 和 out 变量的变体试图让编解码器工作:
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('ditch_effort.avi', -1, 20.0, (640, 360))
Based on this link I should be able to refer to this fourcc reference list 确定要使用的适当 fourcc 压缩代码。我尝试了很多变体,但无法写入输出文件。当我 运行 代码时,#verify 输入形状变量打印相应的 640、360 和正确的帧率。
任何人都可以告诉我我的问题是什么...将不胜感激。
错误的原因是 cropped_frame
的维度 (640,340) 与 writer
中声明的维度 (640,360) 之间存在差异。
所以作者应该是:
out = cv2.VideoWriter('output.avi', fourcc, 20.0,(640,340))
试试这个示例代码:它对我有用:
from __future__ import print_function
import numpy as np
import imutils
import time
import cv2
output = 'example.avi'
video = 'output.mp4'
fps = 33
codec ='MJPG'
vs = cv2.VideoCapture(video)
time.sleep(2.0)
# initialize the FourCC, video writer, dimensions of the frame, and
# zeros array
fourcc = cv2.VideoWriter_fourcc(*codec)
writer = None
(h, w) = (None, None)
zeros = None
while True:
ret, frame = vs.read()
if ret==True:
frame = imutils.resize(frame, width=300)
# check if the writer is None
else:
break
if writer is None:
(h, w) = frame.shape[:2]
writer = cv2.VideoWriter(output, fourcc, fps,
(w * 2, h * 2), True)
zeros = np.zeros((h, w), dtype="uint8")
(B, G, R) = cv2.split(frame)
R = cv2.merge([zeros, zeros, R])
G = cv2.merge([zeros, G, zeros])
B = cv2.merge([B, zeros, zeros])
output = np.zeros((h * 2, w * 2, 3), dtype="uint8")
output[0:h, 0:w] = frame
output[0:h, w:w * 2] = R
output[h:h * 2, w:w * 2] = G
output[h:h * 2, 0:w] = B
writer.write(output)
(B, G, R) = cv2.split(frame)
R = cv2.merge([zeros, zeros, R])
G = cv2.merge([zeros, G, zeros])
B = cv2.merge([B, zeros, zeros])
output = np.zeros((h * 2, w * 2, 3), dtype="uint8")
output[0:h, 0:w] = frame
output[0:h, w:w * 2] = R
output[h:h * 2, w:w * 2] = G
output[h:h * 2, 0:w] = B
writer.write(output)
cv2.imshow("Frame", frame)
cv2.imshow("Output", output)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
print("[INFO] cleaning up...")
cv2.destroyAllWindows()
vs.release()
writer.release()
我在 Ubuntu ubuntu 18.04 中遇到了与 OpenCV C++ 相同的问题。我正在处理视频(转换为灰度,然后生成 "paint" 效果)。
我通过以下步骤成功解决了问题:
- 使用带有以下行的 VideoWriter:
VideoWriter videoWriter("effect.avi", VideoWriter::fourcc('m','p','4','v'), 15.0, Size(ancho, alto), true);
- 将图像(在我的情况下是灰度)转换为 BGR(如果您的图像是 RGB 颜色,请执行相同的操作 space):
Mat finalImage;
cvtColor(imgEffect,finalImage,COLOR_GRAY2BGR);
videoWriter.write(finalImage);
执行后,代码生成视频没有问题。
你可以在下面link找到我的代码:
Example Code in C++
我正在尝试编写一段简单的代码来拍摄视频、裁剪视频并将其写入输出文件。
系统设置:
OS: Windows 10
Conda Environment Python Version: 3.7
OpenCV Version: 3.4.2
ffmpeg Version: 2.7.0
文件输入规范:
Codec: H264 - MPEG-4 AVC (part 10)(avc1)
Type: Video
Video resolution: 640x360
Frame rate: 5.056860
无法生成输出的代码(创建文件但不写入文件):
import numpy as np
import cv2
cap = cv2.VideoCapture('croptest1.mp4')
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc('F', 'M', 'P', '4')
out = cv2.VideoWriter('output.avi', fourcc, 20.0,
(int(cap.get(3)), int(cap.get(4))))
# Verify input shape
width = cap.get(3)
height = cap.get(4)
fps = cap.get(5)
print(width, height, fps)
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
# from top left =frame[y0:y1, x0:x1]
cropped_frame = frame[20:360, 0:640]
# write the clipped frames
out.write(cropped_frame)
# show the clipped video
cv2.imshow('cropped_frame', cropped_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
fourcc 和 out 变量的变体试图让编解码器工作:
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('ditch_effort.avi', -1, 20.0, (640, 360))
Based on this link I should be able to refer to this fourcc reference list 确定要使用的适当 fourcc 压缩代码。我尝试了很多变体,但无法写入输出文件。当我 运行 代码时,#verify 输入形状变量打印相应的 640、360 和正确的帧率。
任何人都可以告诉我我的问题是什么...将不胜感激。
错误的原因是 cropped_frame
的维度 (640,340) 与 writer
中声明的维度 (640,360) 之间存在差异。
所以作者应该是:
out = cv2.VideoWriter('output.avi', fourcc, 20.0,(640,340))
试试这个示例代码:它对我有用:
from __future__ import print_function
import numpy as np
import imutils
import time
import cv2
output = 'example.avi'
video = 'output.mp4'
fps = 33
codec ='MJPG'
vs = cv2.VideoCapture(video)
time.sleep(2.0)
# initialize the FourCC, video writer, dimensions of the frame, and
# zeros array
fourcc = cv2.VideoWriter_fourcc(*codec)
writer = None
(h, w) = (None, None)
zeros = None
while True:
ret, frame = vs.read()
if ret==True:
frame = imutils.resize(frame, width=300)
# check if the writer is None
else:
break
if writer is None:
(h, w) = frame.shape[:2]
writer = cv2.VideoWriter(output, fourcc, fps,
(w * 2, h * 2), True)
zeros = np.zeros((h, w), dtype="uint8")
(B, G, R) = cv2.split(frame)
R = cv2.merge([zeros, zeros, R])
G = cv2.merge([zeros, G, zeros])
B = cv2.merge([B, zeros, zeros])
output = np.zeros((h * 2, w * 2, 3), dtype="uint8")
output[0:h, 0:w] = frame
output[0:h, w:w * 2] = R
output[h:h * 2, w:w * 2] = G
output[h:h * 2, 0:w] = B
writer.write(output)
(B, G, R) = cv2.split(frame)
R = cv2.merge([zeros, zeros, R])
G = cv2.merge([zeros, G, zeros])
B = cv2.merge([B, zeros, zeros])
output = np.zeros((h * 2, w * 2, 3), dtype="uint8")
output[0:h, 0:w] = frame
output[0:h, w:w * 2] = R
output[h:h * 2, w:w * 2] = G
output[h:h * 2, 0:w] = B
writer.write(output)
cv2.imshow("Frame", frame)
cv2.imshow("Output", output)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
print("[INFO] cleaning up...")
cv2.destroyAllWindows()
vs.release()
writer.release()
我在 Ubuntu ubuntu 18.04 中遇到了与 OpenCV C++ 相同的问题。我正在处理视频(转换为灰度,然后生成 "paint" 效果)。
我通过以下步骤成功解决了问题:
- 使用带有以下行的 VideoWriter:
VideoWriter videoWriter("effect.avi", VideoWriter::fourcc('m','p','4','v'), 15.0, Size(ancho, alto), true);
- 将图像(在我的情况下是灰度)转换为 BGR(如果您的图像是 RGB 颜色,请执行相同的操作 space):
Mat finalImage;
cvtColor(imgEffect,finalImage,COLOR_GRAY2BGR);
videoWriter.write(finalImage);
执行后,代码生成视频没有问题。
你可以在下面link找到我的代码: Example Code in C++