Camera in HD@30fps without reencoding (0%CPU) WORKING!

I have been working with ffmpeg camera streaming from a xiami rtsp origin, and I am seen that component is transcoding h264 in a mjpeg feed to show in a browser…
Thats suposse a lot of CPU on a RPI, and low framerate.
Can be possible to sacrify a little realtime (5-10 seconds delay) to without reencode, create a HLS h264 feed?
I have good skill in ffmpeg command-line (create m3u and mp4 parts from RTSP feed without CPU charge), but I dont know if a HLS player has been included in lovelace?. I couldnt see any similar.

Thanks…

**PD. **

I have included (here in 4th post), a working scenario with a RPI camera capturing at 1280x720@15fps in H264 (tipically 30~40 kbps in no-moving scenario), and less than 3% of CPU!!!, all avaliable in Home Assistant web server (but not in lovelace cards).

1 Like

Hello again.
I have now a raspberry pi camera, and I want to have a lovelace picture entity with full rate h264 video. But I can`t figure how to get it. I dont understand what ‘stream’, ‘generic camera’, ‘ffmpeg camera’ componets means, but what I want is not to reencode anything (minimum CPU) and good framerate (>15 fps).

I can easily make a local HLS stream (m3u), and I have read that new lovelace cards support hls.
But I dont know how to make it work.

My ffmpeg code is:

sudo usermod -a -G video homeassistant

ffmpeg -y -f video4linux2 -input_format h264 -video_size 1280x720 -framerate 15 -i /dev/video0 -vcodec copy -an -f hls -hls_flags delete_segments stream.m3u8

This create a great quality h264 stream ( with less than 50MB buffer) prepared to HLS stream in any modern web browser (with proper js player), and all with less than 3% CPU on a RPI3.

But my question is: How I ‘config’ to put this stream in a lovelace image card?

Seems that all stuff in home assistant cards are for camera components with still images…

1 Like

I have tried:
With ffmpeg generating mp3u and h264 ts parts with commandline:
stream:
ffmpeg:

camera:
  - platform: ffmpeg
    input: "file:///home/homeassistant/.homeassistant/stream.m3u8"
    name: ffmpeg camera

  - platform: generic
    name: rpi camera
    still_image_url: "file:///home/homeassistant/.homeassistant/still.jpg"
    stream_source: "file:///home/homeassistant/.homeassistant/stream.m3u8"

But no result.

I have tried too:

camera:
  - platform: ffmpeg
    input: "file:///home/homeassistant/.homeassistant/stream.m3u8"
    name: ffmpeg camera2

to see if the ffmpeg capture camera has to be initted by HA, but no results at all.
The only thing I could obtain was a little thumbnail associated to the first camera ffmpeg attempt.

Please, any help or orientation could be appreciated.

1 Like

For those that has a raspberry pi camera, I will show how to use HA web server to serve full rate great quality stream with HLS, with absolute no encoding (CPU<3%).
This is for ‘env’ install under homeassistant user:

  1. Let homeassistant user open video camera:
sudo usermod -a -G video homeassistant
  1. Create a directory to static access for HA (www)
cd /home/homeassistant/.homeassistant
mkdir www
  1. Restart HA (needed to read new www dir)

  2. Install a HLS player plugin on your favorite browser I.e for firefox: https://addons.mozilla.org/es/firefox/addon/native_hls_playback/ or for chrome: https://chrome.google.com/webstore/detail/native-hls-playback/emnphkkblegpebimobpbekeedfgemhof

  3. run the camera at /home/homeassistant/.homeassistant/www

ffmpeg -y -f video4linux2 -input_format h264 -video_size 1280x720 -framerate 15 -i /dev/video0 -vcodec copy -an -f hls -hls_flags delete_segments stream.m3u8

This will create (and update) a m3u8 file and some .ts parts of h264 captured video. (Control-c or ‘q’ to stop it)

With ffmpeg runnig, open your browser and navigate to
http://your.homeasistant.baseurl/local/stream.m38u
(changing your.homeasistant.baseurl)

…et voilà !

You can see the pi camera 720p@15fps on your HA browser, with almost zero CPU!!! on your RPI (and a minimum delay of 5 secs for how HLS works)

Please, take into accout that this is only a proof-of-concept, with no security considerations,etc.

Now, if you like it, follow this post, to see if anybody can help in how to integrate this in lovelace HA (so you can start-stop camera stream automatically when open the camera (to not waste your sdcard), not depend in HLS plugin, etc.)

HLS is a standard for video-stream prior to webm stuff, supported by all browsers with javascript, even Safari (builtin).

4 Likes

Hi @segalion and thanks for this tutorial !

I’ve just tested your solution with an IP cam :

ffmpeg -i rtsp://me:[email protected]:554/stream1 -acodec copy -vcodec copy -an -f hls -hls_flags delete_segments stream.m3u8

It’s working great on VLC but the Chrome extension can’t catch the file, while its working with other URLs (like this one). Chrome simply downloads my m3u8 file.

I guess I missed something ? Thanks

It may be possible to do this by using the ffmpeg camera integratyion