17

The Raspberry Pi will do hardware accelerated h264 encoding when recording video from the camera board. How can I encode h264 (using hardware acceleration) when the source images are not coming directly from the camera?

Are there any command line tools that support this?

If no, what APIs should I be looking at to make this possible?

Szabolcs
  • 662
  • 2
  • 6
  • 18

3 Answers3

11

GStreamer is included in Raspbian and with its OpenMAX plugin it will use the hardware encoding capabilities of the Raspberry Pi.

See this link for a tutorial on doing what you're looking for: https://www.raspberrypi.org/forums/viewtopic.php?t=72435

If you're interested in transcoding, I've just posted an answer to another question that might interest you: What speed can I expect from the hardware-H264-encoding?

M. Rubio-Roy
  • 568
  • 6
  • 10
3

Looks like by compiling ffmpeg by yourself, on RPi3B+ you can get

ENCODING HIGH-COMPLEXITY 30 FPS VIDEO FROM A 1920 X 1080 JPEG IMAGE SEQUENCE – SIGNIFICANT MOTION AND INTRAFRAME DETAIL – HIGH QUALITY SETTINGS:

Software-Based H.264 Encoding (CPU): 2.6 FPS (11.5 times slower than real time)

Hardware-Based H.264 Encoding (GPU): 6.3 FPS (4.8 times slower than real time)

ENCODING HIGH-COMPLEXITY 30 FPS VIDEO FROM A 640 X 480 JPEG IMAGE SEQUENCE – SIGNIFICANT MOTION AND INTRAFRAME DETAIL – HIGH QUALITY SETTINGS:

Software-Based H.264 Encoding (CPU): 18 FPS (1.7 times slower than real time)

Hardware-Based H.264 Encoding (GPU): 38 FPS (1.3 times FASTER than real time)

The script to help achieve all this is in a forum thread.

akostadinov
  • 131
  • 3
2

The ffmpeg apt package now includes support for hardware encode and decode.

sudo apt install ffmpeg

To see which hardware codecs (using one of the following APIs: v4l2m2m, VAAPI, OMX, MMAL) are available run:

ffmpeg -codecs | grep 'omx\|m2m\|vaa\|mmal'

This will list them all and the first column indicates whether they're available for encode(E)/decode(D).

So for example to use the v4l2m2m H.264 codec to encode 30secs of colour bars:

ffmpeg -f lavfi -i smptebars -t 30 -vcodec h264_v4l2m2m out.mp4 

Or use the OMX based H.264 hardware encoder to encode input_file:

ffmpeg -i input_file -vcodec h264_omx out.mp4 

Also if you have changed the gpu_mem setting in /boot/config.txt it needs to be greater than 16, otherwise you will get an error with any hardware codecs.

Pierz
  • 852
  • 11
  • 9