Joel Masters

Photo by Waldemar Brandt on Unsplash

OpenCV has several nuances and silent fails that makes recording videos from cameras difficult and frustrating. Here’s how to record from a camera:

import cv2cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (w,h))

while(cap.isOpened()):
ret, frame = cap.read()
if ret:
out.write(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()

Adapted from: https://stackoverflow.com/questions/29317262/opencv-video-saving-in-python/45868817

If your videos are being saved as ~6kb files, there is likely a size mismatch between the resolution of the camera and the VideoWriter.

Hope this helps.

--

--

Photo by Lili Popper on Unsplash

Getting the path of a local file in Electron.js may be easier than you think. Here are the steps:

Step 1 of 2: Add an <input> element to index.html

In your index.html, add an <input> element:

// index.html...
<input type="file" id="file-input">
...

Step 2 of 2: Add a change handler for the <input> element in index.js

In your index.js file, add an event handler:

// index.js...
document.getElementById('file-input').addEventListener('change', function() {
console.log(this.files[0].path);
});
...

And voila! That’s it.

--

--