我一直在尝试用OpenCV在python中写MP4视频文件。
当我同时使用linux和windows时,AVI创建工作得很好:
out = cv2.VideoWriter('x.avi', 0, 30, (640, 480))和
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter('x.avi', fourcc, 30, (640, 480))甚至
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter('x', fourcc, 30, (640, 480))。
但是,当我尝试保存一个MP4时,任何东西都不会保存--使用:
fourcc = cv2.VideoWriter_fourcc(*"H264")
out = cv2.VideoWriter('x.mp4', fourcc, 30, (640, 480))和
fourcc = cv2.VideoWriter_fourcc(*"AVC1")
out = cv2.VideoWriter('x.mp4', fourcc, 30, (640, 480))没有发生错误,只是没有保存任何内容。
在过去的几天里,我尝试了所有的方法,尽一切努力避免创建AVI,然后使用ffmpeg将其转换为MP4,因为我发现这是一种可怕的做法。
发布于 2017-03-08 04:11:16
给出框架的高度和宽度的右值:
import cv2
print ('Press [ESC] to quit demo')
# Read from the input video file
# input_file = 'Your path to input video file'
# camera = cv2.VideoCapture(input_file)
# Or read from your computer camera
camera = cv2.VideoCapture(0)
# Output video file may be with another extension, but I didn't try
# output_file = 'Your path to output video file' + '.avi'
output_file = "out.avi"
# 4-byte code of the video codec may be another, but I did not try
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
is_begin = True
while camera.isOpened():
_, frame = camera.read()
if frame is None:
break
# Your code
processed = frame
if is_begin:
# Right values of high and width
h, w, _ = processed.shape
out = cv2.VideoWriter(output_file, fourcc, 30, (w, h), True)
print(out.isOpened()) # To check that you opened VideoWriter
is_begin = False
out.write(processed)
cv2.imshow('', processed)
choice = cv2.waitKey(1)
if choice == 27:
break
camera.release()
out.release()
cv2.destroyAllWindows()这段代码适用于我。
https://stackoverflow.com/questions/38804931
复制相似问题