2017-04-01

How to record video with GStreamer and Logitech's c920 webcam on a Mac in 1080p

This took me unnecessarily long, so I'm going to write it down here for anybody who tries to do a similar thing. It uses h264 hardware encoding, so it works at full 1080p 30fps. Unfortunately I couldn't manage to make the in-camera h264 encoding work. It doesn't seem to be supported by the avfoundation plugin. Still, it's the best way I've found to record full framerate 1080p video on a Mac from a Logitech c920 without the resulting video file taking a massive amount of space, as it's the case when recording with Quicktime. The only problem that I haven't been able to solve is recording from 2 cameras at the same time, since the second camera will invariably drop frames.

Okay, let's start.

First we have to install gstreamer:

brew install gstreamer
brew install gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly

Not all these plugins may be necessary, just in case.

Also, the only way I know how to list capture devices is using ffmpeg, so:

brew install ffmpeg

You can run this command to list available devices:

ffmpeg -f avfoundation -list_devices true -i ""

Then, to record just the video you can do this (supposing you want to record device number 0):

gst-launch-1.0 avfvideosrc -e device-index=0 ! video/x-raw,width=1920,height=1080 ! videoconvert ! vtenc_h264_hw realtime=true max-keyframe-interval=5 ! queue ! mp4mux ! filesink location='output.mp4'

To record and display what's being recorded at the same time, you can do this:

gst-launch-1.0 avfvideosrc -e device-index=0 ! video/x-raw,width=1920,height=1080 ! tee name=t t. ! queue ! osxvideosink sync=false t. ! videoconvert ! vtenc_h264_hw realtime=true max-keyframe-interval=5 ! queue ! mp4mux ! filesink location='output3.mp4'

Finally, to record, display, and record audio too, you can do this:

gst-launch-1.0 avfvideosrc -e device-index=0 ! video/x-raw,width=1920,height=1080 ! tee name=t t. ! queue ! osxvideosink sync=false t. ! videoconvert ! vtenc_h264_hw realtime=true max-keyframe-interval=5 ! queue ! mux. osxaudiosrc device=0 ! audioconvert ! avenc_aac ! queue ! mux. mp4mux name=mux ! filesink location='output.mp4'

This assumes that your audio recording device is number 0. This number doesn't match the device numbers you get from the ffmpeg command, and I don't know how to find what number represents each device. You'll have to do trial-and-error.

If I learn any more details about this I'll update this post. Please comment if you have any more information, since there's not much info online about how to use GStreamer on a Mac.