Page 2 of 3

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

Posted: Thursday 10 September 2015 21:01
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.

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

Posted: Friday 11 September 2015 8:36
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.

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

Posted: Friday 11 September 2015 9:34
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

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

Posted: Friday 11 September 2015 9:38
by ThinkPad
Nice, please post your result here. You'll get it working, just spend some time on it :mrgreen:

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

Posted: Friday 11 September 2015 20:47
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.

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

Posted: Friday 11 September 2015 22:15
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.

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

Posted: Thursday 17 September 2015 20:55
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

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

Posted: Monday 05 October 2015 20:23
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.

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

Posted: Wednesday 30 December 2015 10:58
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

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

Posted: Wednesday 30 December 2015 11:23
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.

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

Posted: Wednesday 30 December 2015 11:41
by patoche
Thank for your quick and clear answer.
It is the bad news no compatibility to view it in domoticz.

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

Posted: Saturday 27 February 2016 11:52
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"
   

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

Posted: Saturday 16 April 2016 9:07
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?

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

Posted: Wednesday 11 May 2016 15:29
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.

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

Posted: Thursday 16 June 2016 15:21
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

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

Posted: Friday 17 June 2016 10:12
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

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

Posted: Tuesday 21 June 2016 13:48
by elpacito
Thanks for your answer

The snapshot is working, I create a bot and it is working fine!!! Thanks a lot!

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

Posted: Sunday 08 January 2017 12:06
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

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

Posted: Friday 17 February 2017 1:27
by gajotnt
How can i execute that code on the openipcam forum? :/
I have a Escam QD300 but your url doesnt work for mine.

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

Posted: Saturday 15 April 2017 2:50
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?