OpenCV VideoWriter不写入Output.avi [英] OpenCV VideoWriter Not Writing to Output.avi

查看:947
本文介绍了OpenCV VideoWriter不写入Output.avi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm attempting to write a simple bit of code that takes a video, crops it, and writes to an output file.

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

基于此链接我应该可以引用此Fourcc参考列表,以确定要使用的适当Fourcc压缩代码.我尝试了很多变体,但无法获取要写入的输出文件.当我运行代码时,#verify输入形状变量会打印相应的640、360和正确的帧频.

Based on this link I should be able to refer to this fourcc reference list to determine the appropriate fourcc compression code to use. I have tried a bunch of variations, but cannot get the output file to be written. When I run the code, the #verify input shape variables print the corresponding 640, 360 and correct Frame Rate.

任何人都可以告诉我我的问题是什么...将不胜感激.

Can any one tell me what my issue is...would be much appreciated.

推荐答案

发生错误的原因是cropped_frame的尺寸(640,340)与writer中声明的尺寸(640,360)之间的差异.

The reason of error is the differences between the dimension of the cropped_frame (640,340) and the dimension declared in the writer (640,360).

所以作者应该是:

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

这篇关于OpenCV VideoWriter不写入Output.avi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆