Record video using Golang and your webcam (without OpenCV/CGO/MingGW)

Vee2xx
3 min readMay 22, 2021

--

If you want to write a Go script that can record video, take pictures — basically interact with a webcam — you’ll come to a quick conclusion that you cannot do it out of the box without using a bunch of highly complex libraries that are OS specific, libraries which you generally cannot package in a way that’s user friendly. It’s almost as though the creators of Go did not want Go programmers to be able to easily interact with webcams (and be able to easily record video or take pictures) with Golang scripts.

For example, to use GoCV you need to compile and install OpenCV, something which anyone using the library will need to do as well before getting any of their code running (boo!). On Windows you need to install MinGW-W64 or CMake first (I kid you not!). Compiling C++ does not always go smoothly and sometimes you find yourself spending hours scouring the internet for the meaning of some obscure error caused by some specific quirk of your operating system (which header file am I missing now?). I don’t know about you but seeing ‘make install’ on the Get Started page of a library always strikes a little fear into my heart. The amount of pain and effort required to get OpenCV working seems out of proportion to the number of its features a project will actually use if all you want to do is write a simple Go script that’ll record video or take pictures from your webcam so you can save it to a file and or do some custom processing on it.

The GoLang Camtron library (I sincerely hope) solves this problem by using Electron’s built in MediaDevices API and permission handling (which are brilliant) to connect your Go script to the webcam (giving Go some real power). The Electron app will read the webcam stream and send it over websockets to your Go script where an embedded local Go server is running and ready to receive the video/image signal which it can then save to a file (a default file handler is included). This is a simple, out of the box solution that will allow you to create Golang scripts that can interact with webcams, record videos, take pictures and hopefully do even more without worrying about GoCV, OpenCV, CGO, MinGW-W64 (which in many years of use I’ve never managed once to get working) right.

How to Use

Install

There are two ways to install Camtron

Download the module

Check if you have Go 1.16 and up and if so turn modules off first.

go versionoutput:  go version go1.14.4 linux/amd64export GO111MODULE=off //Linux or macOSgo env -w GO111MODULE=off //Windows

Install using ‘go get’

go get github.com/vee2xx/camtron

Or use Go Modules

Initialize your project as a module

go mod init yourproject.com/yourmodule

Add Camtron to the resulting go.mod file

require (github.com/vee2xx/camtron v1.0.8)

The first time Camtron runs it will download and unzip the os appropriate camtron-ui package to your project’s root directory so that Camtron can find the Electron app binary and execute it (no user intervention required except on MacOs, in which case when the binary opens you need to give it permission to record from the camera).

Record a video and save it to a file with Golang

Create a file called main.go and you can get going with the following code (the video Go records will be found in the “videos” directory):

package mainimport ("github.com/vee2xx/camtron")func main() {camtron.StartStreamToFileConsumer()camtron.StartCam()}

It really is that simple!

NOTE! Please remember, if you did everything right the video file is saved to the videos directory in the project root.

├──main.go├──videos│ ├── vid-2021_05_20_21_36_55.webm│ └── vid-2021_05_21_13_02_05.webm

The source code for the project is available here:

Golang library

https://github.com/vee2xx/camtron

Electron app

https://github.com/vee2xx/camtron-ui

Demo project

https://github.com/vee2xx/camtron-demo

--

--

Vee2xx
Vee2xx

Written by Vee2xx

Self taught and often baffled developer just trying to keep up with what the kids are doing these days

No responses yet