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

@Olivier974
You helped once, maybe you can help me as well.
and @ anybody else, who knows how to tackle the problem.

I did some test and have very simple script on a hass.io / HassOS installation:

#!/bin/bash
http_url=http://homeassistant.local:8123/api/camera_proxy_stream/camera.my_camera?token=$1
date  >> /config/cam.txt
ffmpeg -i $http_url -y -t 5 -vcodec copy /config/cam.mp4

This script is located in the /config folder.
I can start it from the development / services menu in HA, cam.txt gets updated.
The recording cam.mp4 does not appear in the /config folder
but in logs there is an error message:

2021-05-07 00:30:08 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `bash /config/cam.sh {{ state_attr('camera.my_camera','access_token') }}`, return code: 1
NoneType: None

And by the way I’ve included in my configuration.yaml ffmpeg line:

ffmpeg:

I do believe it comes from missing ffmpeg executable.
I cannot start ffmpeg also via ssh
-bash: ffmpeg: command not found
Which is clear, since the ssh in another container as the hassio core.

Question: If I am right, how to run ffmpeg in a script on HassOS?

Many thanks in advance for any idea and suggestion.
KRR
Peter

hello,

the best way is to copy an example script above that work out of the box, first you have to record in /config/www/some_folder, second you have to create the “some_folder” in the script (see again example script that work).

third, no need ffmepg: in configuration file. Do you have a ESP32CAM flashed with ESPHOME?

follow post #41 and dont forget ; after each end of line script ( as it is explained post #51 for new ha versions)

Nice day

Hi @Olivier974
Many thanks for your quick response, I do appreciate it.
As I do not like copy&paste approach without understanding how the things work, you kicked my lazy a$$ to start thinking. It worked.

For those who will be browsing here for some troubleshooting, few remarks from my side:

The problem with my scripts was not the availability of ffmpeg, as I initially thought. The binary is available in the docker. Here I do not know if it is necessary to add the ffmepg: to config or not. According to manual it is: FFmpeg - Home Assistant

In my case HA running the script in docker failed to resolve hostname homeassistant.local: Name does not resolve. Changing the hostname to ip, solved the issue.

The updated and working test script is as follow:

#!/bin/bash
http_url=http://192.168.0.10:8123/api/camera_proxy_stream/camera.my_camera?token=$1
ffmpeg -i $http_url -y -t 5 -vcodec copy /config/cam.mp4

Don’t forget to change the ip to your one.
Now it works perfectly and generates a test recording.

Second remark for troubleshooting your own bash scripts: Add one line to the script as the second line:

#!/bin/bash
exec &>>/config/debug.log

In the config/debug.log you will find some useful information dumped from script execution.

And coming back to Olivier’s remark:

dont forget ; after each end of line script

I have no idea where the ; shall be added, it is apparently not needed. We do execute a regular bash scripts, so semicolon is just a separator of the commands, same a s new line. It is handy if you have more commands in one line (IMHO).

Many thanks for creating this thread and providing great ideas, very useful.
Best
Peter

1 Like

great!

i do not need the “;” after each line of the script…dont know why is needed for some others like @tdashmike

happy it work for you too, its a great use for ESP32-CAm or any otherscam integrate in HA…

@Olivier974,
… fully agree. Especially for those who can print their enclosure by themselves. The price for my last modules was 4,49€ including shipping. If you add power supply (HLK-5M05) for another 3€ - you have a fully working ip-cam under 10€. There are several type of lens one can chose - so as you wrote it is a great piece of hardware.

1 Like

Hello. I think I got it working. Can someone explain what do these 2 shell command do?

  security_cam_shell: 'bash /config/scripts/security_camera.sh {{ state_attr("camera.security", "access_token") }}'

  security_camera_shell_script: "bash /config/scripts/security_camera.sh {{ cam_token }}"

I can record a 10sec video using the first shell command. However, running the second shell cmd gives me this error.

Error running command: `bash /config/scripts/security_camera.sh {{ cam_token }}`, return code: 1

security_camera.sh:

 #!/bin/bash
 rm /config/www/data/logs/security_cam.log;
exec &>>/config/www/data/logs/security_cam.log;

date;
echo "token is: $1";
cam_token=$1;

folder=/config/www/img/security/cam_record;
id=$(date +"%y-%m-%d_%H-%M-%S")security_camera;
http_url=http://192.168.1.20:8123/api/camera_proxy_stream/camera.security?token=$1;

mkdir $folder;
ffmpeg -i $http_url -t 10 -vcodec copy $folder/$id.mp4;
ls -lh $folder/$id.mp4 $folder/security_camera.mp4;
cp $folder/$id.mp4 $folder/security_camera.mp4 ;

Also, how to fix these command not found messages that is showing in log?

/config/scripts/security_camera.sh: line 3: $'\r': command not found
/config/scripts/security_camera.sh: line 4: $'\r': command not found
Fri Jul 23 20:53:26 JST 2021
/config/scripts/security_camera.sh: line 5: $'\r': command not found
token is: aa41ce86b51a6xxxxxxxxxxxxxx36f74c3ed
/config/scripts/security_camera.sh: line 6: $'\r': command not found
/config/scripts/security_camera.sh: line 7: $'\r': command not found
/config/scripts/security_camera.sh: line 8: $'\r': command not found
/config/scripts/security_camera.sh: line 9: $'\r': command not found
/config/scripts/security_camera.sh: line 10: $'\r': command not found
/config/scripts/security_camera.sh: line 11: $'\r': command not found
/config/scripts/security_camera.sh: line 12: $'\r': command not found

hi!

for those will be interested, i have several 3Dprint case…from simple ones to pan tilt commanded from HA with a slider, or with motion detector…a simple doorbell with a ESP32-Cam :slight_smile: Yaml files included for easy config!

Thingiverse Electronlibre Design ESP32-Cam cases; video doorbell, and more!

Enjoy!

1 Like

Hello @

The shell_script (after 113.3 HA) are limited for 60s of exec, cut all my cameras videos after 6s and doesnt execute all the other script command at the end of recording…

HERE IS THE SOLUTION because no luck with “nohup sh”…spend hours to make it work with no luck, so a simple and fast solution is here : https://community.home-assistant.io/t/how-to-run-shell-commands/328284/6?u=olivier974

Thanks to @koying

Sorry for my English. Greetings to the community and special thanks to Olivier974.

esp32-cam with Ai-Thinker Camera.

This is my script:

#!/bin/bash
exec &>>/config/shell_scripts/camera_garage_shell_script.log

date
echo "token is: $1"
cam_token=$1


folder=/config/www/cam_record
id=$(date +"%d-%m-%y_%H-%M-%S")camera_garage
http_url=https://192.168.1.4:8123/api/camera_proxy/camera.webcam_pir_garage?token=$1

mkdir $folder
ffmpeg -i $http_url -t 1 -vcodec copy $folder/$id.mp4
ls -lh $folder/$id.mp4 $folder/webcam_pir_garage.mp4
cp $folder/$id.mp4 $folder/webcam_pir_garage.mp4;
#ls -lh $folder/$id.mp4 $folder/webcam_pir_garage.mp4
#find $folder -type f -name ‘*.mp4’ -mtime +30 -exec rm {} ;

and this is the log:

Fri Dec 24 21:56:34 CET 2021
token is: 3178384d0971b1bf96119ccc4e1599b157f0dfbc9db88a0d41de26ab3294fe3d
mkdir: can't create directory '/config/www/cam_record': File exists
ffmpeg version 4.4 Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 10.3.1 (Alpine 10.3.1_git20210424) 20210424
  configuration: --prefix=/usr --enable-avresample --enable-avfilter --enable-gnutls --enable-gpl --enable-libass --enable-libmp3lame --enable-libvorbis --enable-libvpx --enable-libxvid --enable-libx264 --enable-libx265 --enable-libtheora --enable-libv4l2 --enable-libdav1d --enable-postproc --enable-pic --enable-pthreads --enable-shared --enable-libxcb --enable-libsrt --enable-libssh --enable-libvidstab --disable-stripping --disable-static --disable-librtmp --enable-vaapi --enable-vdpau --enable-libopus --enable-vulkan --enable-libsoxr --enable-libwebp --enable-libaom --disable-debug
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, jpeg_pipe, from 'https://192.168.1.4:8123/api/camera_proxy/camera.webcam_pir_garage?token=3178384d0971b1bf96119ccc4e1599b157f0dfbc9db88a0d41de26ab3294fe3d':
  Duration: N/A, bitrate: N/A
  Stream #0:0: Video: mjpeg (Baseline), yuvj422p(pc, bt470bg/unknown/unknown), 800x600, 25 fps, 25 tbr, 25 tbn, 25 tbc
Output #0, mp4, to '/config/www/cam_record/24-12-21_21-56-34camera_garage.mp4':
  Metadata:
    encoder         : Lavf58.76.100
  Stream #0:0: Video: mjpeg (Baseline) (mp4v / 0x7634706D), yuvj422p(pc, bt470bg/unknown/unknown), 800x600, q=2-31, 25 fps, 25 tbr, 12800 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
frame=    1 fps=0.0 q=-1.0 size=       0kB time=00:00:00.00 bitrate=4512.8kbits/s speed=  39x    
frame=    1 fps=0.0 q=-1.0 Lsize=      25kB time=00:00:00.00 bitrate=2677743.6kbits/s speed= 0.1x    
video:25kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.197755%
-rw-r--r--    1 root     root       25.5K Dec 24 21:56 /config/www/cam_record/24-12-21_21-56-34camera_garage.mp4
-rw-r--r--    1 root     root       25.8K Dec 24 21:31 /config/www/cam_record/webcam_pir_garage.mp4

The problem:
A file of a few KB is saved in the “cam_record” folder. File does not increment (KB)
and it doesn’t open as a photo or video.

I need to change something on the line

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

?

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%.