Sunday, October 20, 2024

Computer Vision - Transformations of Images - Capturing and Saving Video

So we capture a video and process it frame-by-frame, and we want to save that video. For images, it is very simple: just use cv.imwrite(). Here, a little more work is required.


This time we create a VideoWriter object. We should specify the output file name (eg: output.avi). Then we should specify the FourCC code (details in next paragraph). Then number of frames per second (fps) and frame size should be passed. And the last one is the isColor flag. If it is True, the encoder expect color frame, otherwise it works with grayscale frame.


FourCC is a 4-byte code used to specify the video codec. The list of available codes can be found in fourcc.org. It is platform dependent. The following codecs work fine for me.


  • In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)
  • In Windows: DIVX (More to be tested and added)
  • In OSX: MJPG (.mp4), DIVX (.avi), X264 (.mkv).



FourCC code is passed as ‘cv.VideoWriter_fourcc('M’,'J','P','G')or cv.VideoWriter_fourcc(*'MJPG')` for MJPG.



The below code captures from a camera, flips every frame in the vertical direction, and saves the video.


Code






The Saved Video:



Natural Language Processing service + Generative AI Chatbot + Machine Learning + Mobile App + Web App? Yes, I do provide!


Call me: +84854147015

WhatsApp: +601151992689

https://amatasiam.web.app

Email: ThomasTrungVo@Gmail.Com


No comments:

Post a Comment