10

I'm using OpenCV 4.0 and Python 3.7 to create a timelapse video.

When constructing a VideoWriter object, the documentation says the Size argument should be a tuple.

When I give it a tuple it rejects it. When I try to replace it with something else, it won't accept it because it says the argument isn't a tuple.

When Size not a tuple:

out = cv2.VideoWriter('project.avi', 1482049860, 30, height, width)
SystemError: new style getargs format but argument is not a tuple

When I changed Size to a tuple:

out = cv2.VideoWriter('project.avi', 1482049860, 30, (height, width))
TypeError: must be real number, not tuple

I just want to create a simple cv2.VideoWriter object.

Travis Williams
  • 171
  • 1
  • 6
  • What are the exact values of `height` and `width`? The only way I can reproduce this is when they are floating point values. They should be integers. Provide a [mcve]. – Dan Mašek May 30 '19 at 02:46
  • 1
    @DanMašek the code was this: width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) but without 'int' before the cap.get. I added the 'int' and now it works. thank you. – Travis Williams May 30 '19 at 20:20

3 Answers3

7

I was trying to take the height and width from an uploaded video using:

width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

I changed it to:

width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

Now I don't get that error. It doesn't work yet as a whole, but that particular error isn't there anymore.

Travis Williams
  • 171
  • 1
  • 6
4

I encountered the same situation, here is my solution:

It seems like cv2.VideoWriter() tend to "drop" the argument that has the wrong type. In my case I used pathlib.Path for the first output file name argument,like:

p=Path('project.avi')

out = cv2.VideoWriter(p, 1482049860, 30, frameSize=(height, width))

But cv2.VideoWriter() only accepts str, so it "dropped" the p, and it received:

out = cv2.VideoWriter(1482049860, 30, frameSize=(height, width))

Then (height, width) is actually at the fps position, it requires real but gets tuple

So I suggest you check every argument to make sure they have the right type.

xelmirage
  • 61
  • 2
1

Try updating to opencv v4.1.0. Not seeing that issue there.

Else try:

out = cv2.VideoWriter('project.avi', 1482049860, 30, frameSize=(height, width))
ohyesyoucan
  • 168
  • 2
  • 10
  • No point, this makes no difference, you just didn't reproduce the actual scenario on OP's end since they didn't provide a [mcve]. – Dan Mašek May 30 '19 at 02:48