Python OpenCV MP4创建

Python OpenCV MP4创建,python,opencv,video,ffmpeg,Python,Opencv,Video,Ffmpeg,我一直在尝试用python编写使用OpenCV的MP4视频文件 当我同时使用以下两种工具时,AVI创建在linux和windows上都能正常工作: out = cv2.VideoWriter('x.avi', 0, 30, (640, 480)) 及 甚至 fourcc = cv2.VideoWriter_fourcc(*"XVID") out = cv2.VideoWriter('x', fourcc, 30, (640, 480)) 当我尝试保存MP4时,但没有任何东西可以保存-使用:

我一直在尝试用python编写使用OpenCV的MP4视频文件

当我同时使用以下两种工具时,AVI创建在linux和windows上都能正常工作:

out = cv2.VideoWriter('x.avi', 0, 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))

没有错误发生,只是没有保存


在过去几天里,我尝试了所有方法,尽一切努力避免创建AVI,然后使用ffmpeg将其转换为MP4,因为我发现这是一种可怕的做法。

请给出正确的帧高和帧宽值:

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()

这段代码适合我。

我想opencv在videowriter中使用ffmpeg对视频进行编码(转码)和打包。也许你的系统上没有?作为一种解决方法,您可以使用ffmpeg将avi转换为mp4。如果我安装了ffmpeg,您认为它可以工作吗?是python模块还是实际的windows程序或其他什么
cv::VideoWriter::VideoWriter
上的文档说明:构造函数/函数初始化视频编写器。在Linux上,FFMPEG用于编写视频;在Windows上使用FFMPEG或VFW;在MacOSX上使用QTKit。我不认为openCV单独实现了H.264编码器和ISOBMFF,我也有同样的问题。有时我得到一个视频,有时我得到一个空文件。你解决问题了吗?这是.avi视频的基本示例,而不是问题要求的mp4。提供正确的宽度和高度值为我解决了类似的问题。我有1920x1080个帧,错误地指定了640x480(希望在保存时这些帧会被简单地截断)。相反,mp4文件只包含一个小的头(~200字节)。一旦我正确指定了尺寸,视频就被正确保存了。
fourcc = cv2.VideoWriter_fourcc(*"AVC1")
out = cv2.VideoWriter('x.mp4', fourcc, 30, (640, 480))
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()