我正在尝试编写一段简单的代码来拍摄视频,裁剪并写入输出文件。
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 = 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,我应该可以参考this fourcc reference list来确定要使用的适当Fourcc压缩代码。我尝试了很多变体,但无法获取要写入的输出文件。当我运行代码时,#verify输入形状变量会打印相应的640、360和正确的帧频。
任何人都可以告诉我我的问题是什么...
答案 0 :(得分:0)
错误的原因是cropped_frame
的尺寸(640,340)与writer
中声明的尺寸(640,360)之间的差异。
所以作者应该是:
out = cv2.VideoWriter('output.avi', fourcc, 20.0,(640,340))
答案 1 :(得分:0)
尝试以下示例代码:它对我有用:
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()
答案 2 :(得分:0)
我在Ubuntu ubuntu 18.04中使用OpenCV C ++遇到了同样的问题。我正在处理视频(转换为灰度,然后生成“绘画”效果)。
我成功完成了以下步骤,解决了该问题:
VideoWriter videoWriter("effect.avi", VideoWriter::fourcc('m','p','4','v'), 15.0, Size(ancho, alto), true);
Mat finalImage;
cvtColor(imgEffect,finalImage,COLOR_GRAY2BGR);
videoWriter.write(finalImage);
执行后,代码生成的视频没有问题。
您可以在以下链接中找到我的代码: Example Code in C++