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

All kinds of 'OS' scripts
User avatar
Mediacj
Posts: 102
Joined: Wednesday 11 February 2015 17:09
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands
Contact:

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

Post by Mediacj » Thursday 20 April 2017 21:50

GSVNoFixedAbode wrote: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?
I have a sp009a to and these commands work for me:

10 sec video:
ffmpeg -rtsp_transport -udp_multicast -i rtsp://user:password@ipcam:554/onvif1 -vcodec copy -t 10 -y /home/user/sricam.avi

image:
ffmpeg -rtsp_transport -udp_multicast -i rtsp://user:password@ipcam:554/onvif1 -f image2 -vframes 1 -pix_fmt yuvj420p /home/user/test.jpeg

Sundown89
Posts: 3
Joined: Saturday 10 June 2017 13:48
Target OS: Linux
Domoticz version: 3.7708
Location: Netherlands
Contact:

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

Post by Sundown89 » Monday 12 June 2017 16:56

Mediacj wrote: I have a sp009a to and these commands work for me:

10 sec video:
ffmpeg -rtsp_transport -udp_multicast -i rtsp://user:password@ipcam:554/onvif1 -vcodec copy -t 10 -y /home/user/sricam.avi

image:
ffmpeg -rtsp_transport -udp_multicast -i rtsp://user:password@ipcam:554/onvif1 -f image2 -vframes 1 -pix_fmt yuvj420p /home/user/test.jpeg
Thank you!
I can confirm following commands for Xiaomi Yi Ants (Night) camera with the earlier mentioned "Yi Hack":

10s video:
avconv -rtsp_transport -udp_multicast -i rtsp://ip:554/ch0_0.h264 -vcodec copy -t 10 -y /home/user/domoticz/www/snapshots/yitest.avi

Image:
avconv -i rtsp://ip:554/ch0_0.h264 -vframes 1 -r 1 -s 1920x1080 /home/user/domoticz/www/snapshots/yitest.jpg
Domoticz on Ubuntu 14.04
Dashticz V2
Pi-Hole
RFXCom

Rextytus
Posts: 3
Joined: Thursday 13 October 2016 12:17
Target OS: Raspberry Pi
Domoticz version:
Contact:

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

Post by Rextytus » Sunday 06 August 2017 16:38

"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."

Hello I have a question to Mozart what parameters are given in the jan script I can run.

Rextytus
Posts: 3
Joined: Thursday 13 October 2016 12:17
Target OS: Raspberry Pi
Domoticz version:
Contact:

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

Post by Rextytus » Sunday 06 August 2017 20:34

I apologize for the form of my previous post and for not being greeted but in the heat of battle with phyton files I had no time to read the forum rules. And I would like to ask Mozart or someone who knows? Mozart's formulas work with my camera when I type http. I get the name of the .jpg file and then I can open it or it works. I wrote the Mozart script as executable.py followed by the http address of the camera email address and after the file starts after some time I get a message "Error: unable to send email" .Because I do not know the python I can not say what else I need to complete. And whether I do it correctly. Regards

Rextytus
Posts: 3
Joined: Thursday 13 October 2016 12:17
Target OS: Raspberry Pi
Domoticz version:
Contact:

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

Post by Rextytus » Sunday 06 August 2017 20:47

Or maybe some of you know what I should type out here 192.168 ...

Code: Select all

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

imgUrl="http://1.2.3.4" +  imgPath

gajotnt
Posts: 68
Joined: Monday 06 February 2017 13:48
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: Azores
Contact:

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

Post by gajotnt » Friday 05 January 2018 1:12

Hello tried this but im getting lots of errors during the script and then eventualy it makes the snapshot
Code im using:

Code: Select all

avconv -rtsp_transport tcp -i $rtspUrl -f image2 -vframes 1 -pix_fmt yuvj420p $SnapFile

Results in (it works, just gives alot of errors):

Code: Select all

avconv version 11.11-6:11.11-1~deb8u1+rpi1, Copyright (c) 2000-2017 the Libav developers
  built on Nov  2 2017 13:07:45 with gcc 4.9.2 (Raspbian 4.9.2-10)
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[rtsp @ 0x9d01e0] Non-increasing DTS in stream 2: packet 2 with DTS 0, packet 3 with DTS 0
[rtsp @ 0x9d01e0] Non-increasing DTS in stream 2: packet 3 with DTS 0, packet 4 with DTS 0
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[h264 @ 0x9d29e0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 0x9d29e0] decode_slice_header error
[h264 @ 0x9d29e0] no frame!
[rtsp @ 0x9d01e0] max_analyze_duration 5000000 reached
[rtsp @ 0x9d01e0] Estimating duration from bitrate, this may be inaccurate
Guessed Channel Layout for  Input Stream #0.1 : mono
Input #0, rtsp, from 'rtsp://user:password@192.168.1.188:554/ch01.264':
  Metadata:
    title           : WUHAN Ants RtspServer
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0.0: Video: h264 (Main), yuv420p, 1280x720, 10.08 fps, 90k tbn
    Stream #0.1: Audio: [0][0][0][0] / 0x0000, 8000 Hz, 1 channels
    Stream #0.2: Data: [0][0][0][0] / 0x0000
Output #0, image2, to '/home/pi/domoticz/var/frontdoorcam.jpg':
  Metadata:
    title           : WUHAN Ants RtspServer
    encoder         : Lavf56.1.0
    Stream #0.0: Video: mjpeg, yuvj420p, 1280x720, q=2-31, 200 kb/s, 90k tbn, 90k tbc
    Metadata:
      encoder         : Lavc56.1.0 mjpeg
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native))
Press ctrl-c to stop encoding
frame=    1 fps=  0 q=4.7 Lsize=       0kB time=2.14 bitrate=   0.0kbits/s
video:37kB audio:0kB other streams:0kB global headers:0kB muxing overhead: unknown

Failure404
Posts: 19
Joined: Sunday 24 July 2016 15:28
Target OS: Raspberry Pi
Domoticz version:
Contact:

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

Post by Failure404 » Wednesday 17 January 2018 17:50

Try raising -vframes 1 to a higher value. With 1 it only captures the very first frame and the webcam might not be ready at that time - afaik.

band8266
Posts: 1
Joined: Friday 06 April 2018 21:33
Target OS: Linux
Domoticz version:
Contact:

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

Post by band8266 » Friday 06 April 2018 21:41

I went a different way with this. Created a php page that takes a snapshot using ffmpeg then displays it as an image.. Doing this way I can just use domoticz to point at the PHP page and use it as a normal camera.

Code: Select all

<?php
$video = 'rtsp://192.168.1.185/user=user_password=*********_channel=3_stream=0.sdp';
system("ffmpeg -y -i $video -vframes  1 /var/www/html/img/snapshot.jpg");
$thumbnail = '/var/www/html/img/snapshot.jpg';
$fp = fopen($thumbnail, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($thumbnail));
// dump the picture and stop the script
fpassthru($fp);
exit;
?>
It is somewhat slow but work well
I am using Domo on ClearOS so you will likely need to adjust some to get it to work on RasPI

Freekers
Posts: 7
Joined: Friday 20 October 2017 17:36
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

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

Post by Freekers » Saturday 28 April 2018 20:05

Thank you very much for this informative topic! Without the information in this topic, I probably would not have been able to get my Chinese IP cam to work.

I made some small changes to the script. The ffmpeg version on my Synology did not support 'image2' as output format, therefore I needed to change this to 'singlejpeg'. I also edited the script so that it sends me the snapshot by Telegram.

Code: Select all

#!/bin/sh

#Setup Environment
today=`/bin/date '+%d-%m-%Y__%H-%M-%S'`; 		 #Used to generate filename
IP="192.168.X.X"             		    		#IP address Camera
API="botXXXXXXXXXXX:XXXXXXXXXXXXXX"   #Telegram bot API key
chat="XXXXXXXXXXXXXX" 					 #Telegram (group) chat ID
belletje=$'\360\237\224\224'			# Telegram bell emoji

#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'/stream1' -f singlejpeg -vframes 1 -pix_fmt yuvj420p /volume1/homes/USER/deurbel/$today.jpg

#Send pushnotification with URL to snapshot
curl -s -X POST 'https://api.telegram.org/'$API'/sendPhoto' -F chat_id=$chat -F photo='@/volume1/homes/USER/deurbel/'$today'.jpg' -F caption=''$belletje' *DEURBEL* '$belletje' Er staat iemand aan de deur...' -F parse_mode=markdown

#Delete previous taken snapshots older than 14 days
find /volume1/homes/USER/deurbel/ -name '*.jpg' -mtime +14 -delete

#If camera is offline:
else
	curl -s -X POST 'https://api.telegram.org/'$API'/sendMessage' -F chat_id=$chat -F text=''$belletje' *DEURBEL* '$belletje' Er staat iemand aan de deur... (Foto is niet beschikbaar omdat de camera offline is).' -F parse_mode=markdown
fi

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest