Record Vidéo from ESP32_Cam (config with EspHome) in hassio works great!

Hello,

happy christmas too,

your record duration in your code is “1” one second is not enough i think…so, try to increase to 5 or 10 :

ffmpeg -i $http_url -y -t 5 -vcodec copy /config/cam.mp4

another thing is that you put spaces between $ and http and $ and folder , and between $ and id, thats not good :

try

ffmpeg -i $http_url -t 10 -vcodec copy $folder/$id.mp4

What is the computer you run HA from? a Raspberry Pi? a model 3, a 4? BECAUSE you need some minimum compute power to encode video.

good day

Merry Christmas to you Olivier and thanks for the reply.

Home assistant on Raspberry Pi 4 model B 4gb ram.

I edited as you suggested

ffmpeg -i $http_url -t 10 -vcodec copy $folder/$id.mp4

but nothing different.

In reality, the spaces between $ and http etc, (see script code) were not present. It was a copy and paste problem.

Hello!

thanks

i just realise you put http_url with a https adress : it cannot work.

2nd : the directory seems to be bad : its camera_proxy_stream
and not
camera_proxy

http_url: http://192.168.1.4:8123/api/camera_proxy_stream/camera.webcam_pir_garage?token=$1;

it should work better :slight_smile:

Yeah !!! :ok_hand:

Many many thanks Olivier.

  • The real problem was camera_proxy in place of camera_proxy_stream.

  • In my case, the address must be https otherwise it gives me a 401 error.

  • In my case, the file extension must be .avi. With .mp4 it saves a video that cannot be viewed with VLC or with Windows Media Player.

  • The saved video is much faster than normal.
    For example, with these parameters
    ffmpeg -i $http_url -s 1600x1200 -t 3 -vcodec copy $folder/$id$extension_file
    a 3 seconds video is saved but, actually, it contains frames for about 30/35 seconds.

  • This is the complete script

#!/bin/bash

rm /config/shell_scripts/camera_garage_shell_script.log

exec &>>/config/shell_scripts/camera_garage_shell_script.log

date

echo "token is: $1"

cam_token=$1

extension_file=.avi

folder=/media/video_web_cam_pir_garage

id=$(date +"%d-%m-%y_%H-%M-%S")camera_garage

http_url=https://192.168.1.4:8123/api/camera_proxy_stream/camera.webcam_pir_garage?token=$1

mkdir $folder

ffmpeg -i $http_url -s 1600x1200 -t 3 -vcodec copy $folder/$id$extension_file

ls -lh $folder/$id$extension_file $folder/webcam_pir_garage$extension_file

cp $folder/$id$extension_file $folder/webcam_pir_garage$extension_file

#rm /config/shell_scripts/camera_garage_shell_script.log

#ls -lh $folder/$id$extension_file $folder/webcam_pir_garage$extension_file

#find $folder -type f -name ‘*$extension_file’ -mtime +30 rm $folder/webcam_pir_garage$extension_file;

Anyway, there is a video, and this is the basic thing.

I will try to study better ffmpeg …

Thanks again Olivier, you are very kind and helpful.

1 Like

Hi @Olivier974,

I am trying to follow your steps but I can’t even get the script to run.

Could you tell me what would be the steps one by one?

Theoretically I have everything right, but it is clear that the script does not run, because it gives an access denied error and the file with the logs is not generated.

hi, I manage to make the all things working, but I still have the same problem as yours…
My output video is really fast, I saw on the log that my input stream is detected as 25fps even if I put 5fps in esphome as parameter. The thing is the FFmpeg wait for 250 frame for 25fpt*10sec and put it in a 10sec container. I have at the end a 10sec movie with 250 frame, but those frame were taken during a much longer time… any idea to solve that?

hi @alexdi @nathanBob

i have found a solution that make better video…its a bit fast but nothing compare to the previous video…its almost perfect :

just replace the ffmpeg command in the script of each of your cams like this:

existing command:

ffmpeg -i $http_url -an -t 20 -c:v libx264 -c:a copy -crf 17 $folder/$id.mp4

with this one :

ffmpeg -f mjpeg -analyzeduration 50000 -probesize 20000 -i $http_url -vcodec copy -t 20 $folder/$id.mp4

its much better, let me know if you think ist better or not for you :wink:

if you want to save space you can use libx264 but be carefull because it consume CPU. I have an intel NUC8 with a Core I5 and it consume 4% more of CPU with compression. Without my CPU is at 8%, with compression my CPU is at 12%. And when IDLE he is between 5 and 7%.

Imagine if you have a RPI4 and 2 or 3 stream are encoded…your CPU will be at 100%, sure! and probably crash with 3 or more stream to be recorded and compressed at the same time, so, be carefull and check your CPU usage when he record a stream.

code with compression:

ffmpeg -f mjpeg -analyzeduration 50000 -probesize 20000 -i $http_url -vcodec libx264 -t 20 $folder/$id.mp4

Enjoy :wink:

1 Like

Hello Olivier,
Thank you for all the effort and work you put into this post, it has helped me a lot.
I can already record a video, but it is still too fast. I have implemented your recommended change but have not seen any improvement. Have I done something wrong? Here is my shell file:

medien/!/bin/bash
camera_shell_script="/config/shell_scripts/camera_shell_script.log"
rm "${camera_shell_script}"
exec &>> "${camera_shell_script}"
echo "token is: $1"
cam_token=$1
folder="/media/"
echo "${folder}"
id=$(date +"%y-%m-%d_%H-%M-%S")nistkasten_camera
http_url="http://192.168.***.**:8123/api/camera_proxy_stream/camera.nistkasten?token=$1 "

ffmpeg -f mjpeg -analyzeduration 50000 -probesize 20000 -i $http_url -vcodec copy -t 20 $folder/$id.mp4 
ls -lh $folder/$id.mp4 $folder/nistkasten_camera.mp4
cp $folder/$id.mp4 $folder/nistkasten_camera.mp4
ls -lh $folder/$id.mp4 $folder/nistkasten_camera.mp4
find $folder -type f -name "*.mp4" -mtime +1 -exec rm -f {} \;

I would be very grateful if you could help me.
Kind regards

Simon

hello @SCOORS

what kind of hardware do you use?

I have a NUC with a I5 3.6Ghz and the encode and record is really power consuming…so if you a have a “simple” RPI4…that should be the point…just and idea…

Hi, you’re right I’m using an RPI4, but the processor load doesn’t go above 6% when I run the script. I still get a video just not slowed down.
Do you have any other ideas? I was thinking I could adjust the frame rate. Currently it always assumes 25 FPS (25 FPS * 20 seconds recording time = 500 frames). I’ve already thought about overwriting this default value of 25 FPS with the actual FPS. Do you know how to do this?

Hello,

strange that you consume only 6%…because i was on RPI4 for a couple of years and each time a video is recorded it take almost 60% of my RPI4 CPU, which was a 8Go version and if i encode with recording, it take 80%.