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

Hello @tdashmike,

i think you find the trick…formating problem of the sh script…the “;” do the job!

I have work on this for a few days last year but never had problem without “;”, perhaps something has change between when i work on and the new version of HA core, anyway, you find the problem for others…and its great because its very usefull to add recording to cam ffmeg like esp32cam!

the point is i have used a formatting text editor for bash script ( notepad++ for windows if i remember well).

have a good day,

hope others will give you some love for that!

I’m not sure what happened but the ESP32 Cam is online but there is no video, it used to work fine. I was going to flash it back to Tasmota to see if that worked so I enabled the web server on ESPHome and now the video loads every time. This happened on both of the cameras I had running.

i use home assistant core on docker but this doesn’t work for me

i managed to get the recording from esphome cam to work. but the created video plays at a fast speed. I saw objects dart by @tdashmike

Yes, I have that problem also. I spent a lot of time trying to fix that but couldn’t. Let me know if you have a solution. I have mine set to record for 10 seconds but it it actually recording for ~2 minutes and playing it back at 25fps.

Hello @Giuseppe-P and @tdashmike,

this is normal, its because of the slow FPS rate of the ESP32cam, its like you take a snapshot every half second for ten second lenght video…that why the playback is like a speed motion video…i think its more a limitation with the ESP32-CAM than with HA…

the “10” value is the lenght of the video stream, normaly you can modify this as you want.

Sorry for my bad english (i`m russian speak).
I’m trying to add a camera on esp32cam to MotionEye, there is a problem with the constantly changing short token, and the long-live token cannot be used because you must pass it in the header, but this is impossible in MotionEye. I think you can use the script idea to form the camera address and transfer it as a result of the script’s work to the MotionEye.
Can someone help to form such a script or if someone has already done this, please share your experience?

P.S.
Can you have any ideas how to transfer the address of a picture or video stream to MotionE using a script?

I paste into MotionEye the address copied from the lovelace map such as http:/[not-link-put]/192.168.1.123:8123/api/camera_proxy_stream/camera.cam1?token=x6ad229398aeb5ebcca7a504d6d2da26e11b0e4dca69e378abw253d6ad It is possible to somehow pass a parameter with a long token. For example, I can record a video stream into a video file using a long live token and curl:
GET http://[not-link-put]192.168.1.123:8123/api/camera_proxy_stream/camera.cam1 -H ‘Authorization: Bearer eyJxxxxxxxxxxxxxxxxxxNiJ9.eyJpc3MiOiIyZmFmZDJiMjcyZmQ0MWNkYTdlOGZhxxxxxxxxxxxMTYxNTg5NzgyMSwiZXhwIjoxOTMxMjU3ODIxfQ.e-wgIde-XNDTwKMkYQNgabZ-kJf4xIbiZdPrt5qFAgU’ --output cam.mp4
Maybe there is some way to generate a link like http:/[not-link-put]/192.168.1.123:8123/cam1.php and insert it into MotionEye?

Thanks! It “;” - works! The 401 error is gone. I also have Home Assistant 2021.3.0 with supervisor installed on Ubuntu.

I would put a different firmware on the ESP32 cam if you want to use motioneye. There are a few on the internet with rtsp built in.

Example:

Thanks for the hint, I still hope that ESPHome will be improved and RTSP will be added to it (after all, there is an opportunity to easily add, for example, a relay or a motion sensor).

I still have a few questions (maybe someone has already solved them or knows how to do it:

  1. RTSP does not have a login and password authorization setting, can I add it?
  2. The resolution 800 * 600 is set and I still could not figure out how to change it, for example 1600 * 1200 and 2-3fps suit me, but I still did not understand how to do it.
  3. As I understand it, the air firmware is provided in this project, but I still do not understand how to make it work.

I would be grateful if someone can tell you how to solve at least one of these questions!

Hi @cyberfilin

RTSP was not the main discussion of my original POST. Perhaps open a seperate one or search on HA forum here. Tasmota firmware works on ESP32-Cam and works with Motion-Eyes AddOn in HA.

For your questions : https://esphome.io/components/esp32_camera.html

All is explained here, first install “EspHome Add-On” if you use HassOS. Then create your first node where you can set everything you want : resolution, and much more…
Then flash your device with esphome Flasher and it will be discovered in HA in the EspHome integration.

The script/coding in this post is to make a vidéo when motion is detect with a PIR or when a trigger is on.

Enjoy

@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

?