Script to get JPEG snapshot from RTSP-stream (ffmpeg)

All kinds of 'OS' scripts

Moderator: leecollings

marigo
Posts: 30
Joined: Sunday 31 May 2015 11:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands, Veenendaal
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffm

Post by marigo »

I use the script on my Raspberry and it's working. The only thing is when I execute the script from the CLI the *.jpg will we stored under the "Pi" account.
If I do it from Domoticz, with a virtual switch, it will be stored under the user root.

How can I change this. (Sorry, new to linux)

Another question: how can I email this *.jpg to my email address within Domoticz

I had a IP cam which I could add to a switch and in Domoticz the jpg will be send automagicly. But now I use this script and I love to have the picture in my email box.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffm

Post by ThinkPad »

Like i said you can change the path where the image will be stored. Store at some place like /home/pi/doorbellcam.

And to get it delivered to your mail: you have to find out yourself. I drop the image on Dropbox and get a link to it, through a Pushover message. See the scripts i posted earlier in this topic.
But it can also be dumped onto a FTP-server, like 'mvveelen' has posted.
I am not active on this forum anymore.
marigo
Posts: 30
Joined: Sunday 31 May 2015 11:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands, Veenendaal
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffm

Post by marigo »

I have edited the script to send the snapshot. It seems to work, but only my e-mail server links the attachment as spam.

Code: Select all

#!/bin/sh
IP="[IP address]"                          # IP address Camera

#Ping IP-address of camera to see if it's online, otherwise we don't have to grab a snapshot
if ping -c 1 $IP > /dev/null ; then

#Grab snapshot from RTSP-stream
/usr/bin/ffmpeg -rtsp_transport tcp -i 'rtsp://'$IP'/user=admin&password=&channel=1&stream=0.sdp' -f image2 -vframes 1 -pix_fmt yuvj420p /home/pi/doorbell/snapshot.jpeg
fi

sudo curl --url "smtp://[IP address of SMTP server]:25" --mail-from "[email protected]" --mail-rcpt "[email protected]" --upload-file "/home/pi/doorbell/snapshot.jpeg"
When I find the root cause I'll post it in this topic
Last edited by marigo on Friday 11 September 2015 9:58, edited 2 times in total.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffm

Post by ThinkPad »

Nice, please post your result here. You'll get it working, just spend some time on it :mrgreen:
I am not active on this forum anymore.
marigo
Posts: 30
Joined: Sunday 31 May 2015 11:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands, Veenendaal
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffm

Post by marigo »

The "curl" wasn't able to send the *.jpeg which have something to do with "Mime" settings.
I have installed "ssmtp" on the Pi to get the job done. I've used this setup:

https://rpi.tnet.com/project/faqs/smtp
I used my gmail credentials just like the setup in the url.

In the script I added the following line:

Code: Select all

mpack -s "Doorbell" /home/pi/doorbell/snapshot.jpeg [email protected] [email protected]
So the whole script is:

Code: Select all

#!/bin/sh
IP="[ip address]" # IP address Camera

#Ping IP-address of camera to see if it's online, otherwise we don't have to grab a snapshot
if ping -c 1 $IP > /dev/null ; then

#Grab snapshot from RTSP-stream
/usr/bin/ffmpeg -rtsp_transport tcp -i 'rtsp://'$IP'/user=admin&password=&channel=1&stream=0.sdp' -f image2 -vframes 1 -pix_fmt yuvj420p /home/pi/doorbell/snapshot.jpeg
fi

mpack -s "Doorbell" /home/pi/doorbell/snapshot.jpeg [email protected] [email protected]
Thanks goes to Thinkpad for pushing me in the right direction.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffm

Post by ThinkPad »

Always nice to see the variations that people have on different scripts. Good to hear you got it working.

Maybe this can also be useful: http://domoticz.com/forum/viewtopic.php ... 451#p50451 so you won't get spammed with a lot of mails when someone is pressing your doorbell very often.
I am not active on this forum anymore.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffm

Post by ThinkPad »

I updated my NAS to DSM 5.2 yesterday (still had 5.1 running) and today i noticed i didn't get a snapshot.
When i ran the script by hand, i got the dreaded 'rtsp://192.168.4.20/user=admin&password=&channel=1&stream=1.sdp: Input/output error'. The 'MediaServer' package i was using (/volume1/@appstore/MediaServer/bin/ffmpeg) was also upgraded it seemed. It was back on v1.0 of ffmpeg it said.

I uninstalled it and installed the 'Audio Station' package, and now it works back again :D It says it is version 2.02 of ffmpeg.


volume1/@appstore/AudioStation/bin/ffmpeg
I am not active on this forum anymore.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffm

Post by ThinkPad »

Migrated Domoticz to a Ubuntu VM on ESXi, not running on my NAS anymore (which results in the HDD's of the NAS spinning down 8-) )

I changed the doorbell notification script, it now sends me a pushnotification (Pushover) with only text. The image is send through mail to my Gmail address (sent by myself).

Code: Select all

#!/bin/sh
today=`/bin/date '+%d-%m-%Y__%H-%M-%S'`;  #Used to generate filename
datesubject=`/bin/date '+%d-%m-%Y om %T'`;  #Used to generate timestamp in mailbody
IP="192.168.4.20"                         # IP address Camera


#Ping IP-address of camera to see if it's online, otherwise we don't have to grab a snapshot
if ping -c 1 $IP > /dev/null ; then  

#Grab snapshot from RTSP-stream
/usr/bin/avconv -rtsp_transport tcp -i 'rtsp://'$IP'/user=admin&password=&channel=1&stream=0.sdp' -f image2 -vframes 1 -pix_fmt yuvj420p /home/domoticz/deurbelsnapshots/$today.jpeg

#Send pushnotification with URL to snapshot
sh /home/domoticz/domoticz/scripts/bash/deurbel/pushover.sh -u USERTOKEN -a APPLICATIONTOKEN -q "Deurbel galerij" -m "Er is aangebeld op de galerij. Zie bijlage in Gmail voor foto."
echo "Er is op" $datesubject "aangebeld, zie de bijlage voor de foto." | mail -s "Deurbelfoto" -r "YOUR NAME<[email protected]>" -aFrom:[email protected] -A /home/domoticz/deurbelsnapshots/$today.jpeg [email protected]

#Delete previous taken snapshots older than 7 days
find /home/domoticz/deurbelsnapshots/ -name '*.jpeg' -mtime +7 -delete
else
   sh /home/domoticz/domoticz/scripts/bash/deurbel/pushover.sh -u USERTOKEN -a APPLICATIONTOKEN -q "Deurbel galerij" -m "Net aangebeld, plaatje niet beschikbaar omdat cam offline is"
fi
I also changed from 'ffmpeg' to 'avconv' as it seems ffmpeg has become deprecated as of Ubuntu 14.04

To be able to send mail from the system through Gmail, i used this tutorial: http://www.renedohmen.nl/blog/2014/12/s ... on-ubuntu/
Backup of page if website ever goes down:
Spoiler: show
First, install all necessary packages:
sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules

If you do not have postfix installed before, postfix configuration wizard will ask you some questions. Just select your server as Internet Site and for FQDN use something you like or if you have a FQDN; use that.
Add the following lines to /etc/postfix/main.cf
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

Create a new file /etc/postfix/sasl_passwd to store the Gmail credentials:
[smtp.gmail.com]:587 [email protected]:YOURGMAILPASSWORD

Note: If you want to use your Google App’s domain, just replace @gmail.com with your @yourgoogleappsdomain.com

Fix permission and update postfix config to use sasl_passwd file:
sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

Next, validate certificates to avoid running into error. Just run following command:
cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

Restart your postfix server:
sudo /etc/init.d/postfix restart

Note: you can ignore errors like: warning: /etc/postfix/main.cf, line 43: overriding earlier entry: relayhost=

Test your setup with:
echo "Hello there, this is a test mail from postfix via Gmail." | mail -s "Test" [email protected]

Troubleshooting
If you run into troubles you probably are sending mail from a new IP of from a Gmail address that wasn’t used before.
In maillog you will find something like:
Error: “SASL authentication failed; server smtp.gmail.com”
You need to unlock the captcha by visiting this page https://www.google.com/accounts/DisplayUnlockCaptcha
You can run test again after unlocking captcha.
I am not active on this forum anymore.
patoche
Posts: 10
Joined: Monday 09 November 2015 19:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffm

Post by patoche »

Hi,
I have also a camera with only rtsp stream ( srcicam sp008) and i am newbie :( .
I use a Rpi 2 with domoticz
So i have some questions :
In which directory i have to save this script ?
How run it after each reboot ?
With this script is it possible to add camera in domoticz as a camera full compatible domoticz ? if yes what will be the parameter, Ip, port, url image,... to configure it?

Thanks in advance for your help
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffm

Post by ThinkPad »

Just put the script in a folder like /home/pi/ and call it doorbell.sh or so. You link the script to a switch that is activated when someone presses your doorbell.
The script will work outside Domoticz, you cannot get your cam working in Domoticz. If you want that: buy a different camera that is supported.
I am not active on this forum anymore.
patoche
Posts: 10
Joined: Monday 09 November 2015 19:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffm

Post by patoche »

Thank for your quick and clear answer.
It is the bad news no compatibility to view it in domoticz.
Mozart
Posts: 39
Joined: Monday 19 January 2015 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffmpeg)

Post by Mozart »

I bought an Escam QD300 Wifi and want to use it to send a snapshot when someone presses the doorbel. I noticed that on some pages in the webgui for this cam a current snapshot appeared. So I used Fiddler to see what happens when I go to that page.
I discovered that the webgui uses a special command to retrieve the snapshot:
http://1.2.3.4/web/cgi-bin/hi3510/param.cgi?cmd=snap
The output of this command is a string in this format:
var path="/tmpfs/snap_tmpfs/20160227/IMG001/IMG_chn0_TIMER_MNG_20160227114416_008.jpg"
If you create the following URL you can get the picture in maximum resolution:
http://1.2.3.4/tmpfs/snap_tmpfs/2016022 ... 16_008.jpg

I'm interested in the fact if the snapshot option is also available in other cams. Like I said, I used fiddler to discover this.
Also, this URL does not need any username and password :( So it's a security issue as well...

I created a quick and dirty python script that I can execute when the doorbell is pressed. Here is the script for others to use. Quick and very dirty.

Code: Select all

#!/usr/bin/python

import smtplib
import sys, json, urllib, urllib2
from cookielib import CookieJar
import base64

sender = '[email protected]'
receivers = ['[email protected]']


cj = CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

response = opener.open("http://1.2.3.4/web/cgi-bin/hi3510/param.cgi?cmd=snap") 
content = response.read()

imgpath=content.split("=")[1]
imgPath = imgpath[1:-3]

imgUrl="http://1.2.3.4" +  imgPath
#print imgUrl

imgData = urllib2.urlopen(imgUrl).read()
#output = open("/home/pi/test.jpg",'wb')
#output.write(imgData)
#output.close()
	
encodedcontent = base64.b64encode(imgData)

#Compose mail
marker = "AUNIQUEMARKER"

body ="""
Er is aangebeld.
"""
# Define the main headers.
part1 = """From: Cam <[email protected]>
To: User Name <[email protected]>
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)

# Define the message action
part2 = """Content-Type: text/plain
Content-Transfer-Encoding:8bit

%s
--%s
""" % (body,marker)

# Define the attachment section
part3 = """Content-Type: multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=%s

%s
--%s--
""" %("snapshot.jpg", "snapshot.jpg", encodedcontent, marker)
message = part1 + part2 + part3

try:
   smtpObj = smtplib.SMTP('smtp.yourprovider.something')
   smtpObj.sendmail(sender, reciever, message)
   print "Successfully sent email"
except Exception:
   print "Error: unable to send email"
   
Raspberry 3
Aeon Labs Z-Wave Stick Series 2 - RFLink USB Gateway
Z-Wave switches
433 MHz Temperature + Humidity sensors
Philips Hue Lights
Mi-Light WiFi Bridge + RGBW Controllers
Heisenberg
Posts: 135
Joined: Monday 27 January 2014 14:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP (EScam QD50

Post by Heisenberg »

matthijs wrote:It is working. I have almost the same code and copied your previous taken snapshots part. I also make a copy of the last snapshot and named it snapshot.jpg so I can use it to send a notification to Telegram (It is workaround I think, but I couldn't get it working otherwise). The only problem I have is my front door. We have a gallery with other houses so I can't put my camera there (for now).

Slightly offtopic but the Xiaomi had another problem. Newer firmware versions of the Xiaomi ants camera are lacking a rtsp stream, because it is protected with a username and password which Xiaomi doesn't reveal. so I had to downgrade the camera to get the stream working. I indeed used that website for the correct stream and it is working.
Hi matthijs,

I have bought a Xiaomi Ants camera. Could you share your script how you got it setup up with this camera and Telegram?
Raspberry Pi Type B
Raspberry Pi Type B2
RFXCOMtrx433e
matthijs
Posts: 17
Joined: Friday 29 May 2015 12:25
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP (EScam QD50

Post by matthijs »

Heisenberg wrote:
Hi matthijs,

I have bought a Xiaomi Ants camera. Could you share your script how you got it setup up with this camera and Telegram?

Code: Select all

#!/bin/sh
export DATETIME=`date +%Y%m%d%H%M%S`
ffmpeg -i rtsp://CAMERAIP/ch0_0.h264 -vframes 1 -r 1 -s 1920x1080 /home/pi/domoticz/www/snapshots/$DATETIME.jpg
cp /home/pi/domoticz/www/snapshots/$DATETIME.jpg /home/pi/domoticz/www/snapshots/snapshot.jpg

curl -s -X POST "https://api.telegram.org/BOTNAME/sendPhoto" -F chat_id=ID -F photo="@/home/pi/domoticz/www/snapshots/snapshot.jpg"
Where CAMERAIP is your Xiaomi ants camera ipadress. BOTNAME your botname from telegram and ID your chat id. I have build ffmpeg from the source so it still works. I could not get avconv to work. I make a copy of the snapshot which I send to telegram. I wanted to see the previous photo's in case I needed them. You need of course use a custom firmware where the rtsp stream is enabled, because Xiaomi blocked this in the newer firmware versions.
elpacito
Posts: 2
Joined: Thursday 16 June 2016 15:16
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Germany
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffmpeg)

Post by elpacito »

Hi matthijs,

I use also a Xiaomi Ants camera with the hack software (https://github.com/fritz-smh/yi-hack)

I am able to generate a snapshot of the rtsp stream with ffmpeg on my raspberry but how did you configure the camera in domoticz? I am not able to see the snapshot...

Thanks in advance
elpacito
Heisenberg
Posts: 135
Joined: Monday 27 January 2014 14:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffmpeg)

Post by Heisenberg »

elpacito wrote:Hi matthijs,

I use also a Xiaomi Ants camera with the hack software (https://github.com/fritz-smh/yi-hack)

I am able to generate a snapshot of the rtsp stream with ffmpeg on my raspberry but how did you configure the camera in domoticz? I am not able to see the snapshot...

Thanks in advance
elpacito

You could try this script below with xiaomi hack software:

1. Make sure you have avconv installed and a Telegram Bot
2. save the script in your script folder as doorbell.sh
3. assign doorbell.sh to your doorbell button in Domoticz (e.g. script:///home/pi/domoticz/scripts/doorbell.sh)
4. push the doorbell button to receive snapshot

Code: Select all

#!/bin/sh
export DATETIME=`date +%Y%m%d%H%M%S`
avconv -i rtsp://root:[email protected]:554/ch0_0.h264 -vframes 1 -r 1 -s 1920x1080 /home/pi/domoticz/www/snapshots/$DATETIME.jpg
cp /home/pi/domoticz/www/snapshots/$DATETIME.jpg /home/pi/domoticz/www/snapshots/snapshot.jpg

curl -s -X POST "https://api.telegram.org/bot123456789:ABCDEFGHIJKLMNOPetc/sendPhoto" -F chat_id=12345678 -F photo="@/home/pi/domoticz/www/snapshots/snapshot.jpg"

#Delete previous taken snapshots older than 7 days
find /home/pi/domoticz/www/snapshots/ -name '*.jpg' -mtime +7 -delete
Let me know if it works for you
Last edited by Heisenberg on Wednesday 22 June 2016 11:04, edited 1 time in total.
Raspberry Pi Type B
Raspberry Pi Type B2
RFXCOMtrx433e
elpacito
Posts: 2
Joined: Thursday 16 June 2016 15:16
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Germany
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffmpeg)

Post by elpacito »

Thanks for your answer

The snapshot is working, I create a bot and it is working fine!!! Thanks a lot!
jprod
Posts: 2
Joined: Sunday 08 January 2017 11:57
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: France
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffmpeg)

Post by jprod »

Hello,
i try that on a escam qd 500, it's work. the ONVIF port is 8899.

http://www.openipcam.com/forum/index.ph ... 37#msg5125

i find this snapshot uri:

http://xxx.xxx.xxx.xxx/webcapture.jpg?c ... &channel=1

hope it's help
gajotnt
Posts: 70
Joined: Monday 06 February 2017 12:48
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Azores
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffmpeg)

Post by gajotnt »

How can i execute that code on the openipcam forum? :/
I have a Escam QD300 but your url doesnt work for mine.
GSVNoFixedAbode
Posts: 1
Joined: Saturday 15 April 2017 2:42
Target OS: -
Domoticz version:
Contact:

Re: [Bash] Script to get JPEG snapshot from RTSP-stream (ffmpeg)

Post by GSVNoFixedAbode »

Please excuse the bumping of an old thread but I'm hoping someone may be able to assist.

I've got a Sricam sp009a camera that I can get a good feed displaying on VLC (on a PC) using rtsp://[IPaddress]:554/onvif1 and also surprisingly works on rtsp://[IPaddress]/onvif1 (without the explicit 554 port). However, when I try to save an image on the Raspberry Pi using ffmpeg or avconv using the examples in the scripts here I'm constantly getting "invalid data found when processing input".
Any suggestions on troubleshooting?
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests