Gstreamer webcam video capture and conversion

Andrewgoh
2 min readNov 3, 2022

Recently I started messing with Gstreamer in (Unbutu) Linux. The task is to capture video from the web cam and convert it to a common compressed video file format.

After many frustrating failures, it turns out that my usb webcam can only basically stream jpeg images (and raw images). This is likely to be true for (many) USB webcams in use. However, streaming raw images may considerably reduce the frame rates due to the USB bottleneck.

It turns out there are some undocumented features for image/jpeg usage with v4l2src gstreamer module or element.

After various attempts the following works:

The following plays the webcam in a window:

gst-launch-1.0 v4l2src device=/dev/video0 ! decodebin ! videoconvert! autovideosink

This is the same as if you use an app to play videos from the webcam.

To capture the jpeg frames into a file, it turns out no conversion is needed:
By running this, using avimux module/element, it would stream the jpeg frames into the file video.avi.

gst-launch-1.0 v4l2src device=/dev/video0 ! 'image/jpeg,framerate=30/1,width=640,height=480' ! avimux ! filesink location=video.avi

The benefit of streaming video this way is you can interrupt the capture by pressing control-C, much of the video up to the interrupt is saved in the file.

--

--