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?

4
  • What are you encoding, and how so currently? - are you converting video files, rendering them from images and sound, etc...
    – Wilf
    Commented Jun 5, 2014 at 23:00
  • @Wilf I'm interested in this in general, however I do have a present application: encoding a time lapse directly to video. I can't use raspivid because I need low framerates between 1/5 and 2 fps. Also, raspivid auto-exposure goes into oscillations under my lighting conditions (low light). So I want to use raspistill, but encode directly to video so the result doesn't take up too much space (as JPEGs would). As a second application I want to post process each frame in real time using Mathematica (will take a lot of CPU time), and then send them to be encoded into h264.
    – Szabolcs
    Commented Jun 6, 2014 at 3:17
  • 1
    @Wilf I do have a partial solution which I think should be possible to make work: the idea is something like raspiyuv -o - | rpi-encode-yuv (roughly) where the rpi-encode-yuv tool is from here. The frame size is hard-coded in this tool, so you need to change it at the beginning of the C file and recompile. Unfortunately I haven't managed to get a properly aligned output yet, as raspiyuv doesn't seem to encode the frame size into its output. I get a gradually shifting picture that eventually turns to garbage. Still working on it.
    – Szabolcs
    Commented Jun 6, 2014 at 3:20
  • Depends on the version. Pi 5 doesn't support hardware encoding at all.
    – artem
    Commented Mar 9 at 13:14

3 Answers 3

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?

1
  • Thanks! I cannot test this right now but will accept in advance based on your tutorial.
    – Szabolcs
    Commented Apr 12, 2015 at 21:34
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.

1
  • Unfortunately that is still only for RPi3 running Stretch. Modern Buster raspbian isn't supported. And RPi_Mike has an attitude and lengthy prose problem so attempting the read the forums will be soul sucking. You can probably get the information you need if you're willing to read through everything within, ignoring the lengthy prose and diatribes, and do it yourself. But that isn't much of an "answer" so I can't upvote this one. Bummer hw based encoding isn't a raspbian default offering.
    – gps
    Commented May 10, 2020 at 20:47
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.