Skip to content

Using the RPi3's Video Codecs and Camera Module

sakaki edited this page Mar 7, 2019 · 2 revisions

Easily leverage your RPi3's built-in video codecs, and optional camera module, from the command line!

Introduction

As of v1.4.0 of the image, you can use your RPi3's built-in hardware video codecs, and also capture video streams and stills from the Raspberry Pi camera module, if you have one attached.

While the image includes two demo applications illustrating camera and codec access (ApplicationsMultimediaRPi3 Camera Live View and ApplicationsMultimediaRPi3 Video Player (HW Codecs)), these features can easily be exploited from the command line too. This page just collects together a few 'recipes' for common operations. Feel free to add your own!

NB, as of the time of writing, a patched version of ffmpeg is required to access V4L2 endpoints; the version supplied on the image (media-video/ffmpeg-4.1.1-r2) has had the necessary patches applied.

Prerequisites

To use the hardware codecs via v4l2 m2m , you must have:

gpu_mem=96

or greater in /boot/config.txt (you can use the bundled ApplicationsSettingsRPi3 Config tool to set this, if desired); requires a reboot to take effect, if changed.

If you want to use the camera module, you must have:

gpu_mem=128

or greater, and:

start_x=1

set in /boot/config.txt.

Capturing Output from the Camera

To save an h264 stream from the camera to file, issue (e.g.):

demouser@pi64 ~ $ ffmpeg -f video4linux2 -input_format h264 \
  -video_size 1280x720 -framerate 30 -i /dev/video0 \
  -vcodec copy -an -f matroska test.mkv

Ctrlc to stop the grab when done.

As with all recipes here, feel free to vary the parameters as required.

Or, to grab (e.g.) 10 numbered jpeg files in sequence from the camera at 5fps, issue:

demouser@pi64 ~ $ ffmpeg -f video4linux2 -input_format mjpeg \
  -video_size 1280x720 -framerate 5 -i /dev/video0 \
  -vcodec copy -an -vframes 10 'test%03d.jpg'

The files will be named test001.jpg, test002.jpg etc, and the grab will stop automatically once 10 frames are captured.

Using the RPi3's Hardware Video Codecs

To play back the video file captured above in a window, using the Pi's hardware h264 codec, issue:

demouser@pi64 ~ $ ffplay -vcodec h264_v4l2m2m -i test.mkv

You can also use mpeg4_v4l2m2m and (if you have purchased the license from the RPF) mpeg2_v4l2m2m codecs, if your video stream is of an appropriate format.

gstreamer can be used for hardware-codec-enhanced playback too; for example:

demouser@pi64 ~ $ gst-launch-1.0 -e -vvv filesrc location=test.mkv ! matroskademux ! \
  queue ! h264parse ! v4l2h264dec capture-io-mode=4 ! \
  glimagesink

Miscellaneous Hints

You can append -nostats -loglevel 0 to any of above ffmpeg / ffplay commands, to suppress the chatty terminal output (particularly useful with the v4l2 m2m codecs).

You can’t perform that action at this time.