【问题标题】:OpenCV VideoWriter Not Writing to Output.aviOpenCV VideoWriter 不写入 Output.avi
【发布时间】:2019-07-11 08:00:18
【问题描述】:

我正在尝试编写一段简单的代码,用于拍摄视频、裁剪视频并写入输出文件。

系统设置:

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 和 out 变量的变化试图让编解码器工作:

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 和正确的帧速率。

谁能告诉我我的问题是什么...将不胜感激。

【问题讨论】:

    标签: opencv ffmpeg python-3.7


    【解决方案1】:

    错误的原因是cropped_frame (640,340) 的维度与writer (640,360) 中声明的维度之间的差异。

    所以作者应该是:

    out = cv2.VideoWriter('output.avi', fourcc, 20.0,(640,340))
    

    【讨论】:

    • 我有一个关于细节的问题。所以我的输出变量必须始终具有相应的尺寸,使得 Y=y1-y0 和 X-x1-x0。它是否正确?最后,为什么时间会相差几秒钟。我尝试匹配 fps (5.056859927954619)(我上面的 20 是一个错误),但时间仍然比原来的时间差了 7 秒
    • 是的,作者out必须和你写的框架有对应的尺寸。
    • 请假是什么意思?你设置的fps和实际的fps不一样?
    • 我猜cv2.waitKey(1) 每帧延迟 1 毫秒,所以你的视频比原始视频长一点。另外,我从来没有尝试在这样的十进制数中设置 fps。
    • 我想知道它是否与我使用的编解码器(FMP4)有关,现在我将尝试其他一些,例如(*'DIVX),因为您告诉我同步净值的问题尺寸。 cv2.waitKey(1) 可能没有效果,因为我在没有那段代码的情况下进行了测试。无论如何,我真的很感谢所有的帮助。非常感谢您的时间、耐心和努力。
    【解决方案2】:

    我在 Ubuntu ubuntu 18.04 中使用 OpenCV C++ 时遇到了同样的问题。我正在处理视频(转换为灰度然后生成“绘画”效果)。

    我通过以下步骤成功解决了问题:

    1. 将 VideoWriter 与以下行一起使用:
        VideoWriter videoWriter("effect.avi", VideoWriter::fourcc('m','p','4','v'), 15.0, Size(ancho, alto), true);
    
    1. 将图像(在我的情况下为灰度)转换为 BGR(如果您的图像在 RGB 颜色空间中,请执行相同操作):
        Mat finalImage;
        cvtColor(imgEffect,finalImage,COLOR_GRAY2BGR);
        videoWriter.write(finalImage);
    

    执行后,代码生成视频没有问题。

    您可以在以下链接中找到我的代码: Example Code in C++

    【讨论】:

      【解决方案3】:

      试试这个示例代码:它对我有用:

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

      【讨论】:

        猜你喜欢
        • 2015-06-01
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 2020-06-04
        • 2017-12-16
        • 2018-04-07
        • 2014-08-01
        相关资源
        最近更新 更多