2

I'm working on a project where I need to perform some manipulations on a video file using opencv. A big problem I noticed is that the video files I create using cv2.VideoWriter are extremely large.

For instance, when I perform video stabilization, my input video is 10MB and the output video I create can reach even 80MB, despite having the same fps, same number of frames. Both input and output are in color and both input and output are in AVI format and so on...

Is this a known feature of cv2.VideoWriter? Or is there a way that I can make the output videos smaller?

fmw42
  • 46,825
  • 10
  • 62
  • 80
Ariel
  • 33
  • 1
  • 5
  • What manipulations are you doing? Some processing can add more colors to your images, which will make the file size larger for each frame. Also the input frame might be 8-bit color, but your processing changes them to 24-bit or 32-bit color. – fmw42 Jun 19 '20 at 22:47
  • I'm doing stabilization, and object tracking (separately). after I change the frames, I always convert them to uint8 (since videowriter only accept uint8 and uint64). – Ariel Jun 19 '20 at 23:01
  • But what kind of processing does stabilization and tracking do to the images? And if the input was 8-bit color and the output was 24-bit color, then saving as uint8 will not help that. Does the dimensions of the video frames change? Check the number of unique colors in your input and in your output. – fmw42 Jun 19 '20 at 23:38
  • You should provide a [minimal reproducable code](https://stackoverflow.com/help/minimal-reproducible-example) for us to detect the problem better. I think the issue is related to the codec you use. See https://stackoverflow.com/questions/41966372/opencv-c-write-produces-big-file-compared-to-input – Burak Jun 20 '20 at 16:52
  • `cv2.VideoWriter` is not designed to be optimised for size. It's well known that the output size is bloated. What people usually do is convert the output video through say FFMPEG or HandBrake to reduce the size. What would be even better is to pipe the frame data directly to FFMPEG bypassing the `cv2.VideoWriter` step completely: https://stackoverflow.com/questions/34167691/pipe-opencv-images-to-ffmpeg-using-python. You can also use `VidGear` found in the same post. I haven't tried it yet but it does look promising – rayryeng Jun 22 '20 at 06:38

1 Answers1

1

Using ffmpeg can handle the problem.

ffmpeg -i output.mp4 output_light.mp4 

This simple method roughly reduces the size of the video by about 1/3.

yeachan park
  • 154
  • 2
  • 4