首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCV MP4创建

OpenCV MP4创建
EN

Stack Overflow用户
提问于 2016-08-06 21:45:52
回答 1查看 7.3K关注 0票数 5

我一直在尝试用OpenCV在python中写MP4视频文件。

当我同时使用linux和windows时,AVI创建工作得很好:

代码语言:javascript
复制
out = cv2.VideoWriter('x.avi', 0, 30, (640, 480))

代码语言:javascript
复制
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter('x.avi', fourcc, 30, (640, 480))

甚至

代码语言:javascript
复制
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter('x', fourcc, 30, (640, 480))

但是,当我尝试保存一个MP4时,任何东西都不会保存--使用:

代码语言:javascript
复制
fourcc = cv2.VideoWriter_fourcc(*"H264")
out = cv2.VideoWriter('x.mp4', fourcc, 30, (640, 480))

代码语言:javascript
复制
fourcc = cv2.VideoWriter_fourcc(*"AVC1")
out = cv2.VideoWriter('x.mp4', fourcc, 30, (640, 480))

没有发生错误,只是没有保存任何内容。

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

EN

回答 1

Stack Overflow用户

发布于 2017-03-08 04:11:16

给出框架的高度和宽度的右值:

代码语言:javascript
复制
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()

这段代码适用于我。

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38804931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档