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

All kinds of 'OS' scripts

Moderator: leecollings

ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

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

Post by ThinkPad »

Edit: Jump directly to working script here.
------------------------------------------------------------------------------------------------------------------------------------------------------------------
I recently bought a EScam QD500 ipcamera.

Unfortunately it only offers a RTSP-stream, and no snapshot URL :roll:

I want to use it to make a snapshot when someone rings my doorbell, so i build a little script after Googling for a while on how to do this.
You need ffmpeg (sudo apt-get install ffmpeg) and create the 'snapshots' directory in the 'www' folder (or somewhere else, but the files in 'www' can be accessed from the web).

Copy the contents of the script below, change the camera IP-address to your situation and save the script as 'snapshot.sh'. Then make it executable (chmod +x snapshot.sh)

Code: Select all

#!/bin/sh
export DATETIME=`date +%Y%m%d%H%M%S`
ffmpeg -loglevel fatal -i rtsp://192.168.4.20//user=admin_password=_channel=1_stream=1.sdp -vframes 1 -r 1 /home/pi/domoticz/www/snapshots/$DATETIME.jpg
Only downside is that (for now) i can only get the low-quality stream (stream=1.sdp) to work, the HQ (720p) stream (stream=0.sdp) is too large for the buffer(s) of ffmpeg or so, i receive a lot of errors OR only a completely grey output image is generated.

Another downside (for me personally) is that this only works on Raspberry Pi, when i try to run this on my Synology it almost stops immediately: "RTSP: protocol not found"

Run the script by excecuting

Code: Select all

bash snapshot.sh
After a few seconds, the snapshot will be saved in the folder you configured in the script.
FFmpeg parameters maybe need adjustment if you use it with a different camera than the QD500.

With the '-s' parameter you can define the size of the image, like '-s 640x480' if you want to stretch it a bit bigger. Put it between the '-r 1' and the imagepath part
Last edited by ThinkPad on Monday 07 September 2015 15:11, edited 4 times in total.
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 (EScam QD50

Post by ThinkPad »

Did anyone ever use this?
I use this solution now: http://domoticz.com/forum/viewtopic.php?f=23&t=6794 (grabbing snapshot through Synology Surveillance package), but is a bit slow and it seems to serve me old snapshots sometimes (bell is rang, but i don't see anyone on the snapshot???) . Also seems a bit of overkill for me, i don't need the whole 'Surveillance Station' stuff with recordings, motion detection and such when i only need a snapshot when the doorbell is being pressed.

I think the ffmpeg solution is much more lean, but like i said it only works on Raspberry, the ffmpeg package on my Synology doesn't support RTSP. Think i will setup a Pi for this kind of stuff, still have one lying around...
Another downside is that i wasn't able to get a snapshot from the HQ stream, would be nice if i could get this working, anyone has an idea?
I am not active on this forum anymore.
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 »

I'm testing this script at the moment. I have the Xiaomi ants camera and I couldn't get it working with surveillance station from Synology. My rtsp stream, which was a big hurdle to get that working on the Xiaomi, is already 1280X720. When I use '-s 640x480' 'I get an image with 640x480 so the parameter works when I downsized the image. I use it on a raspberry pi 2 so maybe it can handle the better quality streams better.
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 (EScam QD50

Post by ThinkPad »

Nice :)

I am also working on it, i now have this, which seems to work fine and quite fast (after pressing enter it takes ~5 seconds before it is back on the prompt):

Code: Select all

#!/bin/sh
today=`/bin/date '+%d-%m-%Y__%H-%M-%S'`;

#Grab snapshot from RTSP-stream
ffmpeg -i 'rtsp://192.168.4.20/user=admin&password=&channel=1&stream=0.sdp?real_stream--rtp-caching=100' -f image2 -vframes 1 -pix_fmt yuvj420p /home/pi/deurbel/$today.jpeg 

#Delete previous taken snapshots older than 7 days
find /home/pi/deurbel/ -name '*.jpeg' -mtime +7 -delete
But the '-pix_fmt yuvj420p' part is specific to the stream of my cam, so you could also try without that parameter if you encounter problems.
And you can also try with 'avconv' as ffmpeg is becoming deprecated.

Oh and did you already found this page: http://www.ispyconnect.com/man.aspx?n=xiaomi ? Maybe it can help you find the good stream...
I am not active on this forum anymore.
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 »

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.
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 (EScam QD50

Post by ThinkPad »

I now edited my script to upload the snapshots to Dropbox. Therefore i don't have to forward any ports in my router to my NAS, which is more secure i think ;)

snapshot.sh:

Code: Select all

#!/bin/sh
today=`/bin/date '+%d-%m-%Y__%H-%M-%S'`;
IP="192.168.4.20"                    # IP address Camera

if ping -c 1 $IP > /dev/null ; then  # if IPCAM is online then:
#Grab snapshot from RTSP-stream
avconv -rtsp_transport tcp -i 'rtsp://192.168.4.20/user=admin&password=&channel=1&stream=0.sdp?real_stream--rtp-caching=100' -f image2 -vframes 1 -pix_fmt yuvj420p /home/pi/deurbel/snapshots/$today.jpeg 

#Send pushnotification with URL to snapshot
bash /home/pi/deurbel/pushover.sh -u YOUR_USERKEY_HERE -a YOUR_API_KEY_HERE -q "Deurbel galerij" -m "https://dl.dropboxusercontent.com/u/ID_OF_YOUR_DROPBOX_PUBLIC_FOLDER/deurbel_snapshots/$today.jpeg"

#Copy image to Dropbox 'Public' folder
bash /home/pi/deurbel/dropbox_uploader.sh upload /home/pi/deurbel/snapshots/$today.jpeg /Public/deurbel_snapshots/$today.jpeg

#Delete previous taken snapshots older than 7 days
find /home/pi/deurbel/snapshots -name '*.jpeg' -mtime +7 -delete
else
	bash /home/pi/deurbel/pushover.sh -u YOUR_USERKEY_HERE -a YOUR_API_KEY_HERE -q "Deurbel galerij" -m "Net aangebeld, plaatje niet beschikbaar omdat cam offline is"
fi
pushover.sh: https://github.com/fschaefer/pushover.sh

The 'dropbox_uploader.sh' is this project: https://github.com/andreafabrizi/Dropbox-Uploader
When running the configuration wizard you need to give the script permissions to your whole Dropbox, otherwise it can't access the 'Public' folder.

Only thing is that i can't delete files older than 7 days on Dropbox, because that project works via curl and not like a real Dropbox client (where you can browse folders etc). Maybe as a workaround i can create a cron that deletes the folder every 7 days and creates it again directly.
I am not active on this forum anymore.
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 (ffm

Post by Heisenberg »

Is the script working for any (chinese) rtsp ip camera?
And could it be that ffmpeg is not working anymore?

I use this script but get some errors:

Code: Select all

pi@domoticzpi ~/www $ bash snapshot_rtsp.sh
ffmpeg version 0.8.17-6:0.8.17-1+rpi1, Copyright (c) 2000-2014 the Libav developers
  built on Mar 25 2015 00:39:58 with gcc 4.6.3
The ffmpeg program is only provided for script compatibility and will be removed
in a future release. It has been deprecated in the Libav project to allow for
incompatible command line syntax improvements in its replacement called avconv
(see Changelog for details). Please use avconv instead.
[rtsp @ 0xbc1660] method DESCRIBE failed: 401 Unauthorized
rtsp://192.168.178.99/user=admin&password=&channel=1&stream=0.sdp?real_stream--rtp-caching=100: Invalid data found when processing input
Raspberry Pi Type B
Raspberry Pi Type B2
RFXCOMtrx433e
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 »

"401 Unauthorized"

You need to provide the login credentials (see the url, it is possible to fill them). I only needed user 'admin' and no password. And ofcourse you need to change the RTSP-url to that of your camera (which camera do you use?)
The warning about ffmpeg becoming deprecated isn't that severe, but if you want you can replace it with avconv, see my post (12 jul) above yours for a version where i use avconv.

Let me know if you got it working. It works great here.
I am not active on this forum anymore.
pj-r
Posts: 140
Joined: Wednesday 17 December 2014 17:30
Target OS: Linux
Domoticz version: V3.8650
Location: Jyväskylä, Finland
Contact:

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

Post by pj-r »

I'm having the same "problem" with camera here also: only rtsp. escam ant..

As general solution would it be a good idea to make it possible to support camera snapshot also by script? I mean an option to use script in camera settings. Similar to UVC and Rapsberry cameras but you would specify a script that makes/takes the snaptshot.

Could be called: camerascript.cgi&script=scriptName&image=imageName
I think there have to be some secure aspect with script and image that the possible attacker could not use this for loading files or executing other scripts.

This would make possible to grap images from any weird places :lol:
LXC(x64 Ubuntu Xenial), RFXtrx433E, MySensors
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 »

For displaying the image in Domoticz this script approach is too slow. It takes about 5 seconds or so to grab the snapshot from the RTSP-stream.
I have never used the integrated Domoticz camera support, but i think it refreshes the image every xx seconds?

By using the script and refreshing the image every xx seconds, you will put a biiiiig load on your CPU, system will get very slow i guess.

When i ever will buy a new cam i will get a model which also has a JPEG-snapshot URL, much easier. But for now this works fine, i only need to retrieve a image when the doorbell is pressed.
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 »

Ok, i had this running for a while, but was still a bit annoyed by the fact that i had to run a Raspberry Pi for a 'silly' task like this. I mean: the Synology is a linux box, it is ridiculous that i need a separate linux box (RPI) for this.

So i did some research on how to get a different ffmpeg version on the NAS, a version that supports RTSP, because the default one (/usr/syno/bin/ffmpeg) doesn't like RTSP ("Protocol not found").

I discovered this post: http://www.synology-forum.de/showthread ... post474947
probiere bitte die anderen Versionen von ffmpeg aus. Unterschiedliche Versionen unterstützen unterschiedliche Protokolle:
/volume1/@appstore/MediaServer/bin/ffmpeg
/volume1/@appstore/AudioStation/bin/ffmpeg
/volume1/@appstore/VideoStation/bin/ffmpeg
So i installed the 'MediaServer' package, and changed my script a bit.... and BOOM! it worked! I got a .jpg file, made by the Synology!!

The line to make the snapshot looks like this:

Code: Select all

/volume1/@appstore/MediaServer/bin/ffmpeg -i 'rtsp://192.168.4.20/user=admin&password=&channel=1&stream=0.sdp' -f image2 -vframes 1 -pix_fmt yuvj420p /volume1/@appstore/domoticz/var/scripts/plaatje.jpg
I will post my changed script later, have to leave in a few minutes.

Best part of it, is that the package doesn't even needs to be running, i only need the ffmpeg that came attached with it. Only thing is, that the package will autostart after a reboot of the NAS. Maybe i can fix that with a script (kill the proces through crontab). Ideas are welcome. But for now i am very happy! :mrgreen:
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 »

Edit: Script not needed anymore for the TOP-201 mini camera. With a firmware update it now has a url that spits out a JPEG image.
See my blog for more information: https://thinkpad.tweakblogs.net/blog/12 ... -stream%21
For other cameras that still only support (RTSP) stream, the script can be used.

Updated script:
snapshot.sh (i have it in /volume1/@appstore/domoticz/var/scripts/bash/doorbell/snapshot.sh)

Code: Select all

#!/bin/sh
today=`/bin/date '+%d-%m-%Y__%H-%M-%S'`; 	#Used to generate filename
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

/volume1/@appstore/MediaServer/bin/ffmpeg -rtsp_transport tcp -i 'rtsp://'$IP'/user=admin&password=&channel=1&stream=0.sdp' -f image2 -vframes 1 -pix_fmt yuvj420p /volume1/homes/admin/doorbellcam/Public/doorbell_snapshots/$today.jpeg

#Send pushnotification with URL to snapshot
sh /volume1/@appstore/domoticz/var/scripts/bash/doorbell/pushover.sh -u PUSHOVER_USERKEY -a PUSHOVER_APPKEY -q "Doorbell" -m "https://dl.dropboxusercontent.com/u/YOUR_DROPBOX_PUBLICFOLDER_ID/doorbell_snapshots/$today.jpeg"

#Delete previous taken snapshots older than 7 days
find /volume1/homes/admin/doorbellcam/Public/doorbell_snapshots/ -name '*.jpeg' -mtime +7 -delete
else
	sh /volume1/@appstore/domoticz/var/scripts/bash/doorbell/pushover.sh -u PUSHOVER_USERKEY -a PUSHOVER_APPKEY -q "Doorbell" -m "Doorbell has just been pressed, but image is unavailable because camera is ofline"
fi
You can grab the pushover.sh here: https://github.com/fschaefer/pushover.sh

According to 'kraades' on Tweakers, the package won't autostart at boot if you stop it manually. So if you don't have a suitable 'ffmpeg' on your Synology NAS, you can install the 'Media Server' package without even loading your CPU, because the package doesn't have to be running :D

The snapshot.sh is triggered by a Lua script:
script_device_doorbell_singlepulse.lua (i have it in /volume1/@appstore/domoticz/var/scripts/lua/script_device_doorbell_singlepulse.lua)

Code: Select all

-- script_device_deurbel_singlepuls.lua
-- Script needs a Domoticz uservariable called 'DoorbellPreviousPress' of type 'String'
-- See http://domoticz.com/forum/viewtopic.php?f=23&t=7512 for more information

commandArray = {}

TimeBetweenPresses = 30 --time that needs to be between presses (in seconds), before os.execute line is ran again

function datetimedifferencenow(s)
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end

if (devicechanged['Doorbell'] == 'On') then
   if (datetimedifferencenow(uservariables_lastupdate['DoorbellPreviousPress']) > TimeBetweenPresses ) then
      print('<b style="color:Blue">Doorbell has been pressed: previous press was <b style="color:Green">MORE</b> than '..TimeBetweenPresses..' sec ago, send pushnotification</b>')
      os.execute('/volume1/@appstore/domoticz/var/scripts/bash/doorbell/snapshot.sh')
      commandArray['Variable:DoorbellPreviousPress']=tostring(os.time())
   else
      print('<b style="color:Blue">Doorbell: previous press was <b style="color:Red">LESS</b> than '..TimeBetweenPresses..' sec ago, ignore signal</b>')
   end
end
return commandArray
The image is dropped into a folder that is being watched by the 'Cloud Sync' package from Synology. I have it linked to my Dropbox, and thus a image is put in my Dropbox, which i can access using the URL for the public folder. This wil NOT work for other folders, only for the public folder the URL stays always the same.
It is also important that you choose the 'Bidirectional' sync direction, otherwise the 'Public' folder will not appear on your NAS. You also need the 'Media Server' package, because it contains the updated 'ffmpeg' utility with support for RTSP. But you can stop the package, it is not needed that it is running.

It works really great now. Doorbell is pressed, the Lua script looks if the previous press was more than XX seconds ago (otherwise multipe images and pushnotifications would be sent, which is annoying if the package delivery guy presses the button 3x), if so the Bash script is started. In the Bash script the camera is first pinged (otherwise we don't have to go through all the trouble of grabbing a snapshot), then a screenshot is grabbed, pushnotification is send, and at the end the script looks in the directory if there are images older than 7 days (i don't see any value in keeping them longer).
I am not active on this forum anymore.
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

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

Post by capman »

:D :D :D :D keep up this good work you do !! (APPLAUSE)

Now a must figer it out :oops:
I have mediaserver installed but get this error with putty.

ffmpeg version 1.0 Copyright (c) 2000-2012 the FFmpeg developers
built on Apr 24 2015 20:44:24 with gcc 4.6.4 (Linaro GCC branch-4.6.4. Marvell GCC Dev 201310-2126.3d181f66 64K MAXPAGESIZE ALIGN)
configuration: --prefix=/usr/pkg --incdir='${prefix}/include/ffmpeg' --arch=armada --target-os=linux --cross-prefix=/usr/local/arm-marvell-linux-gnueabi/bin/arm-marvell-linux-gnueabi- --enable-cross-compile --enable-optimizations --enable-pic --enable-shared --disable-static --enable-version3 --enable-encoders --enable-pthreads --enable-libmp3lame --disable-swscale-alpha --disable-postproc --disable-ffserver --disable-ffplay --disable-bzlib --disable-altivec --disable-decoder=amrnb --disable-indev=alsa --disable-outdev=alsa --disable-encoder=zmbv --disable-encoder=dca --disable-encoder=ac3 --disable-encoder=ac3_fixed --disable-encoder=eac3 --disable-decoder=dca --disable-decoder=eac3 --disable-decoder=truehd --disable-doc
libavutil 51. 73.101 / 51. 73.101
libavcodec 54. 59.100 / 54. 59.100
libavformat 54. 29.104 / 54. 29.104
libavdevice 54. 2.101 / 54. 2.101
libavfilter 3. 17.100 / 3. 17.100
libswscale 2. 1.101 / 2. 1.101
libswresample 0. 15.100 / 0. 15.100
rtsp://192.168.178.10/user=admin&password=&channel=1&stream=0.sdp: Input/output error

and this is my line:

/volume1/@appstore/MediaServer/bin/ffmpeg -rtsp_transport tcp -i 'rtsp://192.168.178.10/user=admin&password=&channel=1&stream=0.sdp' -f image2 -vframes 1 -pix_fmt yuvj420p /volume1/homes/admin/doorbellcam/Public/doorbell_snapshots/$today.jpeg

:roll: :roll: :roll:
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 »

Weird, seems to be something with the stream being wrong. Can you open rtsp://192.168.178.10/user=admin&password=&channel=1&stream=0.sdp with VLC (on your PC) for example? Did you change the username on the cam? (i didn't). Maybe it has something to do with permissions and that you need a different user/password combination.
I am not active on this forum anymore.
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

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

Post by capman »

I had a working picture with VLC. The login is now still standard. :(
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 »

Have you tried the VGA (lower quality) stream?

rtsp://192.168.178.10/user=admin&password=&channel=1&stream=1.sdp
I am not active on this forum anymore.
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

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

Post by capman »

On VLC a get a lower quality stream thats no problem. A should try it at my script.
User avatar
capman
Posts: 153
Joined: Friday 12 July 2013 20:48
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Belgium
Contact:

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

Post by capman »

At last :D It was not working because my mediaserver was running on my nas. Now I have my snapshot on low quality.
On high quality my image is always a half image or less. So low quality is good also :) Now a have to figure it out how
to become it visible on my phone. That's the next step :mrgreen:
Thanks 4 your support Thinkpad.
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 »

Good to hear! I heard from someone else that he also got the "input/output" error, but that was because the ffmpeg version of his MediaServer was still v1.0
On my NAS it is 2.02

However, he switched to /volume1/@appstore/AudioStation/bin/ffmpeg which was v2.02 and it worked :mrgreen:

I use Dropbox to store the image, but you could also upload it to an FTP-server, see this post for how to do it: http://forum.synology.com/enu/viewtopic ... 64#p320730
You probably have to change the location of 'lftp', use 'which lftp' to find the location on your NAS.
I am not active on this forum anymore.
User avatar
mvveelen
Posts: 687
Joined: Friday 31 October 2014 10:22
Target OS: NAS (Synology & others)
Domoticz version: Beta
Location: Hoorn, The Netherlands
Contact:

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

Post by mvveelen »

This script works like a charm! Thanks for the tips & tricks. The part with the lftp also works, and the images are instantly copied to my domain.

The code (the code contains pieces in which I mute my Onkyo) :

Code: Select all

#!/bin/sh
today=`/bin/date '+%d-%m-%Y__%H-%M-%S'`;    #Used to generate filename
IP="192.168.0.12"                       # IP address Camera

#Let the old doorbell ring: this is the old KaKu doorbell with the 2 receivers
/usr/bin/curl "http://192.168.0.124:8084/json.htm?type=command&param=switchlight&idx=133&switchcmd=Chime"

#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

#Use for HD-stream
#/volume1/@appstore/AudioStation/bin/ffmpeg -rtsp_transport tcp -i 'rtsp://'$IP'/user=admin&password=&channel=1&stream=0.sdp' -f image2 -vframes 1 -pix_fmt yuvj420p /volume1/Downloads/Snapshots/deurbel-$today.jpeg

#Use for low-quality streamA(640x480)
/volume1/@appstore/AudioStation/bin/ffmpeg -rtsp_transport tcp -i 'rtsp://'$IP'/user=admin&password=&channel=1&stream=1.sdp' -f image2 -vframes 1 -pix_fmt yuvj420p /volume1/Downloads/Snapshots/deurbel-$today.jpeg

#Send pushnotification with URL to snapshot
#sh /volume1/@appstore/domoticz/var/scripts/bash/doorbell/pushover.sh -u PUSHOVER_USERKEY -a PUSHOVER_APPKEY -q "Doorbell" -m "https://dl.dropboxusercontent.com/u/YOUR_DROPBOX_PUBLICFOLDER_ID/doorbell_snapshots/$today.jpeg"
/bin/sh /volume1/@appstore/domoticz/var/scripts/prowl.sh "Domoticz" 0 "Deurbel" "Er is aangebeld, bekijk snapshot: http://www.mywebspace.nl/snapshots/deurbel-$today.jpeg"

#Mute the volume of the Onkyo receiver
/usr/bin/curl "http://192.168.0.124/onkyo/onkyo.php?command=AMT&params=01"


HOST="mywebspace.nl/public_html/pics/snapshots/"
USER="username"
PASSWD="password"

#Location where the snapshots are stored in Domoticz
LFILE="/volume1/Downloads/Snapshots/deurbel-$today.jpeg"


/usr/bin/lftp <<END

#Create the FTP session, using the open command; input credentials and host.
open -u $USER,$PASSWD -p 21 $HOST

#Upload file using variable LFILE
put $LFILE

#Quit after file has been uploaded.
quit
END


#Delete previous taken snapshots older than 7 days
find /volume1/Downloads/Snapshots/ -name '*.jpeg' -mtime +7 -delete
else
   /bin/sh /volume1/@appstore/domoticz/var/scripts/prowl.sh "Domoticz" 0 "Deurbel" "Er is aangebeld, maar camera is offline dus geen plaatje :-("
   	#Mute the volume of the Onkyo receiver
	/usr/bin/curl "http://192.168.0.124/onkyo/onkyo.php?command=AMT&params=01"
fi
RPi3b+/RFXCOM rfxtrx433E/Shelly/Xiaomi Gateway/Philips HUE Lights/Atag Zone One/2 SunnyBoy inverters/AirconWithMe/P1 smartmeter/Domoticz latest Beta
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest