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

Okay thank you very much.

I´ve tasted it and the pictures appear correctly.

However, the videos don´t appear and in the log it appears this:

Error running command: bash /config/shell_scripts/camara_piscina_shell_script.sh {{cam_token}}, return code: 1

My automation is this:


 - alias: esp32_cam
   trigger:
   - entity_id: media_player.samsung_ue43mu1234
     from: 'off'
     platform: state
     to: 'on'
   action:
   - data:
       entity_id: camera.camara_piscina
       filename: /config/www/cam_captures/pisicna_{{ now ().year }}_{{ now ().month }}_{{ now ().day }}_{{ now ().hour }}_{{ now ().minute }}.jpg
     service: camera.snapshot
   - delay: 00:00:01
   - data_template:
       data:
         force_image: true
         image: https://xxxxxx.duckdns.org/local/cam_captures/piscina_{{ now ().year }}_{{ now ().month }}_{{ now ().day }}_{{ now ().hour }}_{{ now ().minute }}.jpg
       message: Grabación iniciada
       title: Mensaje CAM
     service: notify.notify
- service: shell_command.camara_piscina_shell_script

In configuration.yaml:

shell_command:
     camara_piscina_shell_script: "bash /config/shell_scripts/camara_piscina_shell_script.sh {{cam_token}}"

In my config/shell_scripts/camara_piscina_shell_script.sh:

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

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

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

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

Do you know why it doesn´t work? Thank you!

Hello,

1-what do you found inside :

camara_piscina.log

post it please.

2-If you open your “camara_piscina” stream under hassio, under Firefox, try to click right on the page and “inspect property accesibility”, then “network”, what do you have as “Domain” for your camera stream?

Is it 192.168.x.x:8123 or xxx.duckdns.org:8123 ?

Hi!

  1. Inside camara_piscina.log there is nothing…

Actually it’s appears this:

  1. when I do the instruction that you said, it also appears the same as the picture before

I don’t know why :confused:

try on a PC or mac on local network, not with your smartphone.

You should have something in .log ! that mean something is incorrect in your code…an error of name file or something like that.
Even if you have no video, you should have “token is : xxpojx6q46zc46q4c6…” in .log

It doesn´t work when I try to access into the log.

it says: 502: Bad Gateway

with samba youcan acess any file under windows…inthe folder config/www/shell_scripts/camara_piscina.log

Without log can do anything

Hello Oliver,
can you help me please? I have followed all the steps and I get the following error in the log. If I copy and paste the route with the token in the browser I can see the camera live.

I have also noticed that in HA in notifications I get the following message when I run the automation:
Login attempt or request with invalid authentication from localhost (127.0.0.1) (Lavf / 58.45.100)

token is: 6c70ab7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/config/www/cam_record

ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3.0 (Alpine 9.3.0)
  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-libssh --disable-stripping --disable-static --disable-librtmp --enable-vaapi --enable-vdpau --enable-libopus --disable-debug
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
https://xxxxxxxxxxx.ui.nabu.casa/api/camera_proxy_stream/camera.camara_esp32?token=6c70ab7xxxxxxxxxxxxxxxxxxxxxxxxxx
: Server returned 401 Unauthorized (authorization failed)
ls: /config/www/cam_record
/20-09-22_19-27-23camara_esp32
.mp4: No such file or directory
ls: /config/www/cam_record
/camara_esp32.mp4
: No such file or directory
cp: can't stat '/config/www/cam_record
/20-09-22_19-27-23camara_esp32
.mp4': No such file or directory
ls: /config/www/cam_record
/20-09-22_19-27-23camara_esp32
.mp4: No such file or directory
ls: /config/www/cam_record
/camara_esp32.mp4
: No such file or directory
find: -exec requires an argument

Hi @Dani_Leal,

i think it can NOT work with nabu casa.

You just have to change xxxxxxxxxxx.ui.nabu.casa with your local IP Adress like (for me)

http://192.168.1.xx:8123/api/camera_proxy_stream/camera.camara_esp32?token=6c70ab7xxxxxxxxxxxxxxxxxxxxxxxxxx

and it should work :wink:

Because the RECORD function is local, not from outside, your cam is local, on HA.

NOTE that it is http, not https too

Thank you very much for your answer, but the exact same thing happens to me with the local ip. This could be due to the folder not having all the permissions? I can’t find another explanation for this…

and you use http? or https?

i am under hassio on a RPi4, and you?

try this :

-If you open your “camara_esp32” stream under hassio, under Firefox, try to click right on the page and “inspect property accesibility”, then “network”, what do you have as “Domain” for your camera stream?

Yes, I put my ip http: //192.168.1.88 … and nabu.casa https: //nabu.casa … In either of the two ways it gives the same error. It makes me think that raspian is blocking access to my camera’s ip or something like that. The error it gives me is Server returned 401 Unauthorized (authorization failed) as I do not have authorization to without entering my credentials (forgive my English, I’m using the google translate)
I have HA on Docker.

no, i dont think so…i think its just the token : each time you open a camera feed, the token is refresh.

what adress do you have if you try the tip mention above? :
open your “camara_esp32” stream under hassio, under Firefox, try to click right on the page and “inspect property accesibility”, then “network”, what do you have as “Domain” for your camera stream?

copy and paste the URL you have (hide what you want)

the adress you will found there will be the good one to enter in yur script file (without the token of course).

mine looks like :

http://192.168.1.xx:8123/api/camera_proxy/camera.chambre1?authSig=eyJ0eXAiOiJK..............blabla token.......gX0hXGuk2h3WpY

become :

http://192.168.1.xx:8123/api/camera_proxy_stream/camera.chambre1?token=$1

in the script “chambre1_cam.sh”, locate under : config/shell_scripts/

401 Unauthorized is a Authentication problem, sure its your adress not the good one or token problem.
/
/
/

No problem with your English, i am french lol

/
/
/
Have you the shell command properly write without errors? : under configuration.yaml :

shell_command:
   camara_esp32_shell: '/config/shell_scripts/camara_esp32_cam.sh {{ state_attr("camera.camara_esp32","access_token") }}' 

/
/
and you script must look like that :

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

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

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

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

Thanks for your help Olivier, but I’m still the same … I have done what you indicate and the IP that comes out is the same as the one I am putting, it is the IP of HA. If I copy and paste the url in the browser, I can see the camera live. http://192.168.8.169:8123/api/camera_proxy_stream/camera.camara_esp32?token=567cb35c2a387355.xxxxxxxxxxxxxxx

I see that your url appears something like authSig = xxxxxxxxx gives me that I need to give authorization to the camera in some way. I’m seeing that it may be something like this: https://www.home-assistant.io/docs/authentication/providers/

when I run the automation that runs the shell I get this notification in HA:

image

sorry to heard that…

seems you are right…permission is missing somewhere…sorry i am under hassio, not raspbian…

perhaps somebody could help?

Same problem.

Getting 401 errors and don’t know why…

Funny thing is that when I look in the error log and copy that exact address, the camera feed opens in Chrome.

EDIT: I found the problem. For some reason bash in Home Assistant appends a newline at the end of every variable. Writing the values directly makes it work, but it’s rather unintuitive. Currently looking for a fix on the internet.

1 Like

Hello everyone,

I’ve been working through the whole thread and after a couple of houres I managed to start the shell file and everything.

But still, I’m getting the 401 errors.
Sometimes I can take the the adress from the error log and I can open it in firefox or chrome browser, but i never manage to record anything.

Is there somehow a solution or workaround?

Help would be appreciated! Thanks!

Did you find the fix?
How do you write the shell_script.sh without variables?

I’m getting “Server returned 401 Unauthorized (authorization failed)” ERROR…

This is my configuration.yaml:

shell_command:
  camera_script: 'bash /config/www/shell_scripts/camera_shell_script.sh {{state_attr ("camera.camara", "access_token")}}'

This is my camera_shell_script.sh

#!/bin/bash
camera_shell_script="/config/www/shell_scripts/camera_shell_script.log"
rm "${camera_shell_script}" # esto borra cada vez el archivo si quieres que sea incremental puedes borrar
exec &>> "${camera_shell_script}"
echo "token is: $1"
cam_token=$1
folder="/config/www/cam"
echo "${folder}"
id=$(date +"%y-%m-%d_%H-%M-%S")camara
http_url=http://192.168.0.37:8123/api/camera_proxy_stream/camera.camara?token=$1
mkdir $folder
ffmpeg -i $http_url -t 1 -vcodec copy $folder/$id.mp4;

This is my log:

token is: 21e91b046e5f9a2c7214cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/config/www/cam

mkdir: can't create directory '/config/www/cam

': File exists
ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3.0 (Alpine 9.3.0)
  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-libssh --disable-stripping --disable-static --disable-librtmp --enable-vaapi --enable-vdpau --enable-libopus --enable-libaom --disable-debug
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
http://192.168.0.37:8123/api/camera_proxy_stream/camera.camara?token=21e91b046e5f9a2c7214cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
: Server returned 401 Unauthorized (authorization failed)

What am I doing wrong?

Wanted to share what worked for me on HASS version core-2021.3.1.

Notes:

  1. Create the directories scripts and recordings/yourcamera in /config after installing the SAMBA add-on.
  2. Replace “yourcamera” with your camera name and also “yoursensor”.
  3. Add “;” at the end of every line in the sh script or else HASS will automatically add a new line which breaks the command.

configuration.yaml

shell_command:
  yourcamera_script: 'bash /config/scripts/record_yourcamera.sh {{state_attr ("camera.yourcamera", "access_token")}}'

automations.yaml

- alias: Record yourcamera
  trigger:
  - platform: state
    entity_id: binary_sensor.yoursensor
    to: 'on'
  action:
    service: shell_command.yourcamera_script

yourcamera_script.sh

#!/bin/bash
rm /config/scripts/yourcamera.log;
exec &>>/config/scripts/yourcamera.log;
ffmpeg -i https://xxx.xxx.xxx.xxx:8123/api/camera_proxy_stream/camera.yourcamera?token=$1 -t 10 -vcodec copy -an /config/recordings/yourcamera/$(date +"%Y_%m_%d_%I_%M_%p")_yourcamera.mp4;
3 Likes

You have the new line problem, see my post below.

1 Like