无法在python open cv上使用此代码录制视频

无法在python open cv上使用此代码录制视频,python,opencv,cv2,Python,Opencv,Cv2,尝试在python中使用opencv录制视频时,我使用了videowrite()方法录制视频,但我显示了错误。此错误显示“需要一个整数(Get type noneType)”。需要此错误的解决方案 import numpy as np import os import cv2 filename = 'video.avi' frames_per_sec = 24.0 my_res = '720p' def change_res(cap,width,height): cap.set(3,

尝试在python中使用opencv录制视频时,我使用了videowrite()方法录制视频,但我显示了错误。此错误显示“需要一个整数(Get type noneType)”。需要此错误的解决方案

import numpy as np
import os
import cv2

filename = 'video.avi'
frames_per_sec = 24.0
my_res = '720p'

def change_res(cap,width,height):
    cap.set(3,width)
    cap.set(4,height)

STD_DIMENSIONS= {
    "480p": (640, 480),
    "720p": (1280, 720),
    "1080p": (1920, 1080),
    "4k": (3840, 2160),
}
def get_dims(cap, res = '1080p'):
    width, height = STD_DIMENSIONS['480p']
    if res in STD_DIMENSIONS:
        width, height = STD_DIMENSIONS[res]
        change_res(cap,width,height)
        return width,height
VIDEO_TYPE = {
    'avi' : cv2.VideoWriter_fourcc(*'XVID'),
    'mp4' : cv2.VideoWriter_fourcc(*'XVID'),
}

def get_video_type(filename):
    filename, ext  = os.path.splitext(filename)
    if ext in VIDEO_TYPE:
        return VIDEO_TYPE[ext]
        return VIDEO_TYPE['avi'] 

cap = cv2.VideoCapture(0)
dims = get_dims(cap,res=my_res)
video_type_cv2 = get_video_type(filename)

out = cv2.VideoWriter(filename,video_type_cv2,frames_per_sec,dims)

while(True):
    ret, frame = cap.read()
    out.write(frame)
    cv2.imshow('frame',frame)
    if cv2.waitkey(20) & 0xFF == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()
输出

Traceback (most recent call last):
  File "video.py", line 40, in <module>
    out = cv2.VideoWriter(filename,video_type_cv2,frames_per_sec,dims)
TypeError: an integer is required (got type NoneType)
[ WARN:0] terminating async callback
回溯(最近一次呼叫最后一次):
文件“video.py”,第40行,在
out=cv2.VideoWriter(文件名、视频类型、每秒帧数、dims)
TypeError:需要一个整数(获取类型NoneType)
[警告:0]正在终止异步回调

您正在使用
获取视频类型中的
os.path.splitext

# filename = 'video.avi'
filename, ext  = os.path.splitext(filename)
# Now filename = 'video' , and ext = '.avi'

您需要的是
ext='avi'
,它未在
get\u video\u type
中处理,并返回
None
。这是函数的to doc。

当扩展名不是
avi
mp4
之一时,
get\u video\u type
函数返回
None