使用 OpenCV 编写视频 +蟒蛇 +苹果

发布于 2024-10-26 06:11:18 字数 1005 浏览 1 评论 0原文

当我尝试将帧写入视频时,我不断收到断言错误。我收到的错误是这样的:

Traceback (most recent call last):
  File "VideoMixer.py", line 23, in <module>
    cv.WriteFrame(writer, cv.LoadImage(fileName))
cv.error: dst.data == dst0.data

这是我的脚本:

import cv
import sys

files = sys.argv[1:]

for f in files:
    capture = cv.CaptureFromFile(f)
    height = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH)
    width = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT)
    fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)
    fourcc = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FOURCC)
    print fourcc
    writer = cv.CreateVideoWriter('ok.mov', int(fourcc),fps,(int(width),int(height)),1)
    print writer
    for i in range(30):
        frame = cv.QueryFrame(capture)
        print frame
        if frame:
            cv.WriteFrame(writer, frame)

将帧保存为图像效果很好,所以我知道捕获没有任何问题。我创造作家错了吗? 'print fourcc' 输出 0.0 但我尝试过许多 FOUR_CC 值。

谢谢!

I keep getting an assertion error when I'm trying to write frames to video. The error I'm getting is this:

Traceback (most recent call last):
  File "VideoMixer.py", line 23, in <module>
    cv.WriteFrame(writer, cv.LoadImage(fileName))
cv.error: dst.data == dst0.data

Here's my script:

import cv
import sys

files = sys.argv[1:]

for f in files:
    capture = cv.CaptureFromFile(f)
    height = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH)
    width = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT)
    fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)
    fourcc = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FOURCC)
    print fourcc
    writer = cv.CreateVideoWriter('ok.mov', int(fourcc),fps,(int(width),int(height)),1)
    print writer
    for i in range(30):
        frame = cv.QueryFrame(capture)
        print frame
        if frame:
            cv.WriteFrame(writer, frame)

Saving the frames as images works fine so I know there's nothing wrong with the capture. Am I creating the writer wrong? The 'print fourcc' outputs 0.0 but I've tried with many FOUR_CC values.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

场罚期间 2024-11-02 06:11:19

This file in the OpenCV source implies on line #2598 - if this assert fails: "dst.data == dst0.data"

it means that the destination size or type was incorrect

谁把谁当真 2024-11-02 06:11:18

您的某些框架是否具有不同的色彩空间或深度?一些观察:

  • 您在代码中交换了高度和宽度,这是故意的吗?
  • 您的 fourcc 应该是一个整数 > 0. 请参阅下面我的示例。

我个人没有使用 OpenCV 生成 Quicktime 视频,但这对我生成未压缩的 AVI 文件很有用。我使用 cv.CV_FOURCC 函数选择 I420 fourcc:

import cv
import sys

# standard RGB png file
path = 'stack.png'
cap = cv.CaptureFromFile(path)
fps = 24
width = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_WIDTH))
height = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_HEIGHT))
# uncompressed YUV 4:2:0 chroma subsampled
fourcc = cv.CV_FOURCC('I','4','2','0')
writer = cv.CreateVideoWriter('out.avi', fourcc, fps, (width, height), 1)
for i in range(90):
    cv.GrabFrame(cap)
    frame = cv.RetrieveFrame(cap)
    cv.WriteFrame(writer, frame)

更新:VLC 播放 out.avi 的屏幕截图:

在此处输入图像描述

在 Quicktime 中:

Do some of your frames have different colorspaces or depths? A few observations:

  • You have swapped the height and width in your code, is that on purpose?
  • Your fourcc should be an integer > 0. See my example below.

I haven't personally generated Quicktime video using OpenCV, but this worked for me generating an uncompressed AVI file. I choose the I420 fourcc using the cv.CV_FOURCC function:

import cv
import sys

# standard RGB png file
path = 'stack.png'
cap = cv.CaptureFromFile(path)
fps = 24
width = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_WIDTH))
height = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_HEIGHT))
# uncompressed YUV 4:2:0 chroma subsampled
fourcc = cv.CV_FOURCC('I','4','2','0')
writer = cv.CreateVideoWriter('out.avi', fourcc, fps, (width, height), 1)
for i in range(90):
    cv.GrabFrame(cap)
    frame = cv.RetrieveFrame(cap)
    cv.WriteFrame(writer, frame)

Update: Screencapture of VLC playing out.avi:

enter image description here

In Quicktime:

enter image description here

想你只要分分秒秒 2024-11-02 06:11:18

我尝试了各种编解码器,包括“MJPG”和“I420”,但它们都不适用于我的 Mac OpenCV 版本。他们毫无抱怨地生成了微小的无法查看的输出文件。

然后我发现此页面其中列出了一些适合我的编解码器。例如,“mp4v”在我的 Mac 上运行良好,QuickTime 也能够播放它。

I tried various codecs including 'MJPG' and 'I420' and none of them worked on my Mac OpenCV build. They produced tiny unviewable output files without complaining.

Then I found this page which lists some codecs that worked for me. E.g. 'mp4v' works fine on my Mac and QuickTime is able to play it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文