类型错误:VideoWriter() 缺少必需参数“frameSize”(位置 5),opencv-python ==4.4.0.42

lia*_*ong 5 python opencv video-capture

我想使用以下代码创建 VideoWriter:

  fourcc = cv2.VideoWriter_fourcc(*'mp4v')

  video_writer = cv2.VideoWriter('out.mp4',fourcc,fps,(frame_width,frame_height))
Run Code Online (Sandbox Code Playgroud)

但我收到错误:

   TypeError: VideoWriter() missing required argument 'frameSize' (pos 5)
Run Code Online (Sandbox Code Playgroud)

当我将代码更改为:

   fourcc = cv2.VideoWriter_fourcc(*'mp4v')

   video_writer = cv2.VideoWriter(filename='out.mp4',fourcc=fourcc,fps=fps,frameSize=(frame_width,frame_height))
Run Code Online (Sandbox Code Playgroud)

我收到另一个错误:

   TypeError: VideoWriter() missing required argument 'apiPreference' (pos 2)
Run Code Online (Sandbox Code Playgroud)

所以我将代码更改为:

   fourcc = cv2.VideoWriter_fourcc(*'mp4v')

   video_writer = cv2.VideoWriter(filename='out.mp4',apiPreference=0,fourcc=fourcc,fps=fps,frameSize=(frame_width,frame_height))
Run Code Online (Sandbox Code Playgroud)

我收到错误:

   TypeError: VideoWriter() missing required argument 'params' (pos 6)
Run Code Online (Sandbox Code Playgroud)

我该如何解决它?谁能告诉我如何使用 api:VideoWriter() 吗?非常感谢

lia*_*ong 5

好的,以下代码对我有用:

frame_num = int(Cap.get(cv2.CAP_PROP_FRAME_COUNT))
frame_width = int(Cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(Cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter(result,fourcc,fps,(frame_width,frame_height))
Run Code Online (Sandbox Code Playgroud)

Cap.get(cv2.*) 的类型是 float,所以我将其更改为整数

  • 感谢您的回答,这有效。其他正在阅读此答案的人请确保在使用之前将 num、width、height 转换为 int。 (2认同)