How to Convert H.265 to H.264 with go2rtc (for Apple Home and Home Assistant)
A lot of newer cameras send H.265, also called HEVC. It saves bandwidth, but it does not play everywhere. HomeKit cameras stream H.264, and the Home Assistant HomeKit Bridge encodes H.264 with libx264 by default. Browser support for H.265 is still patchy. The fix is to leave the camera alone and let go2rtc hand out an H.264 copy.
This is the transcode at the center of the Ring guide, pulled out on its own so it works for any camera go2rtc can open.
The page is dressed like a 1998 personal site. The config is current go2rtc. Copy it from the Notepad boxes.
1 Find out what the camera sends
Add the camera to go2rtc.yaml as it is, with no transcode. Restart go2rtc, open http://SERVER_IP:1984, start the stream, and open its info page. While it plays, the producer lists the codecs. If video says H265, keep going.
streams:
driveway_source: rtsp://USER:PASSWORD@CAMERA_IP:554/STREAM_PATH
The RTSP path is different for every brand. Check your camera’s manual. Keep the real password out of git and screenshots.
If you would rather check from a terminal, ffprobe comes with FFmpeg.
ffprobe -v error -rtsp_transport tcp -show_entries stream=codec_type,codec_name,width,height -of compact rtsp://127.0.0.1:8554/driveway_source
2 Add an FFmpeg transcode stream
An ffmpeg: source tells go2rtc to start FFmpeg when someone watches. The # parts are output options. #video=h264 encodes H.264. #audio=aac encodes AAC. Leave out an #audio option and the audio is dropped.
The input can be another go2rtc stream by name. go2rtc opens the camera once and FFmpeg reads from go2rtc.
streams:
driveway_source: rtsp://USER:PASSWORD@CAMERA_IP:554/STREAM_PATH
driveway: ffmpeg:driveway_source#video=h264#audio=aac
The Ring guide does the same thing with the source spelled out as a local RTSP URL, plus a ? codec filter that asks for the H.265 and Opus tracks Ring sends. Both forms work. The RTSP one is handy when you want to pick tracks.
streams:
driveway: "ffmpeg:rtsp://127.0.0.1:8554/driveway_source?video=h265#video=h264#audio=aac"
Video only, and the camera audio is already AAC? Copy it instead of encoding it again: #video=h264#audio=copy.
3 Size and CPU
Encoding video is the expensive part. Two ways to keep it sane.
Make the frame smaller. #width and #height scale the output. Set one and the other follows the aspect ratio. A 4K camera does not need to reach a phone tile at 4K.
streams:
driveway: ffmpeg:driveway_source#video=h264#width=1280#audio=aac
Use the GPU. Add #hardware and go2rtc picks a hardware encoder, or name one: vaapi, cuda, v4l2m2m, dxva2, or videotoolbox. Hardware acceleration is off unless you ask for it, because it can be unstable. Intel and AMD on Linux go through VAAPI, and the host needs a /dev/dri folder. Docker needs access to it.
streams:
driveway: ffmpeg:driveway_source#video=h264#hardware#audio=aac
# or pick the engine
driveway_vaapi: ffmpeg:driveway_source#video=h264#hardware=vaapi#audio=aac
The software H.264 template is libx264 with the superfast preset and zerolatency tune. You can override it under an ffmpeg: block in the config if you know what you want. Most people do not need to.
4 Point clients at the H.264 stream
Every go2rtc stream is also an RTSP stream on port 8554. Home Assistant, a Generic Camera, or VLC can open rtsp://127.0.0.1:8554/driveway on the server, or rtsp://SERVER_IP:8554/driveway from the LAN. Use TCP for RTSP.
For a still image, go2rtc serves a JPEG of any stream.
still_image_url: "http://127.0.0.1:1984/api/frame.jpeg?src=driveway"
stream_source: "rtsp://127.0.0.1:8554/driveway"
rtsp_transport: tcp
If Home Assistant feeds this stream to a HomeKit Bridge, the bridge can pass the H.264 straight through with video_codec: copy instead of encoding a second time. The Ring guide keeps libx264, which also works and costs more CPU.
5 Verify the output
sudo systemctl restart go2rtc
journalctl -u go2rtc -f
Open the web UI, start driveway, and check its info page. The consumer side should now say H264 and AAC. Or ask ffprobe again.
ffprobe -v error -rtsp_transport tcp -show_entries stream=codec_type,codec_name,width,height -of compact rtsp://127.0.0.1:8554/driveway
6 Troubleshooting
Black video
The client is still getting H.265. Make sure it opens the transcoded stream name, not the source.
CPU pegged
Lower the size with #width, try #hardware, and avoid stacking a second transcode in the HomeKit Bridge.
No audio
There is no #audio option, so FFmpeg dropped it. Add #audio=aac. Also check the HomeKit side, where camera audio is off unless support_audio is on.
The stream never starts
Raise the go2rtc log level, restart, and watch the log for the FFmpeg command and its error. An RTSP camera that is slow to answer can need a longer #timeout=10.
log:
level: debug
Comments
Loading comments…