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

All kinds of 'OS' scripts

Moderator: leecollings

User avatar
Mediacj
Posts: 74
Joined: Wednesday 11 February 2015 16:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

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

Post by Mediacj »

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 »

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 / ODroid
Domoticz version:
Contact:

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

Post by Rextytus »

"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 / ODroid
Domoticz version:
Contact:

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

Post by Rextytus »

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 / ODroid
Domoticz version:
Contact:

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

Post by Rextytus »

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: 70
Joined: Monday 06 February 2017 12:48
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Azores
Contact:

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

Post by gajotnt »

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:[email protected]: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: 15
Joined: Sunday 24 July 2016 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by Failure404 »

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 »

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

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
tERBO
Posts: 1
Joined: Friday 31 January 2020 12:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

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

Post by tERBO »

Hi All!
Yesterday ffmpeg was updated on my Synology and after that snapshot script stopped working.
Here is the output:

Code: Select all

root@AtomDS:~# sudo /volume1/@appstore/MediaServer/bin/ffmpeg -rtsp_transport tcp -i 'rtsp://10.10.10.114:554/user=admin&password=pass&channel=1&stream=0.sdp?real_stream' -f singlejpeg -vframes 1 -pix_fmt yuvj420p 1.jpg
ffmpeg version 3.3.7 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 4.9.3 (crosstool-NG 1.20.0) 20150311 (prerelease)
  configuration: --prefix=/usr/pkg --incdir='${prefix}/include/ffmpeg' --arch=i686 --target-os=linux --cross-prefix=/usr/local/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu- --enable-cross-compile --enable-optimizations --enable-pic --enable-gpl --enable-version3 --enable-nonfree --enable-encoders --enable-pthreads --disable-muxer=image2 --disable-muxer=image2pipe --disable-swscale-alpha --disable-ffplay --disable-ffserver --disable-doc --disable-devices --disable-bzlib --disable-altivec --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libmp3lame --disable-decoder=amrnb --disable-encoder=zmbv --disable-encoder=dca --disable-decoder=dca --disable-encoder=ac3 --disable-encoder=ac3_fixed --disable-encoder=eac3 --disable-decoder=eac3 --disable-encoder=truehd --disable-decoder=truehd --extra-cflags=-I/usr/local/x86_64-pc-linux-gnu/x86_64-pc-linux-gnu/sys-root/usr/pkg/include --extra-ldflags=-L/usr/local/x86_64-pc-linux-gnu/x86_64-pc-linux-gnu/sys-root/usr/pkg/lib --cc=/usr/local/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-ccache-gcc --enable-shared --disable-static --enable-yasm --enable-libx264 --enable-vaapi --enable-encoder=h264_vaapi --enable-encoder=libx264
  libavutil      55. 58.100 / 55. 58.100
  libavcodec     57. 89.100 / 57. 89.100
  libavformat    57. 71.100 / 57. 71.100
  libavdevice    57.  6.100 / 57.  6.100
  libavfilter     6. 82.100 /  6. 82.100
  libswscale      4.  6.100 /  4.  6.100
  libswresample   2.  7.100 /  2.  7.100
  libpostproc    54.  5.100 / 54.  5.100
Input #0, rtsp, from 'rtsp://10.10.10.114:554/user=admin&password=pass&channel=1&stream=0.sdp?real_stream':
  Metadata:
    title           : RTSP Session
  Duration: N/A, start: 0.400000, bitrate: N/A
    Stream #0:0: Video: h264 (Main), yuv420p(progressive), 1920x1080, 15 fps, 15 tbr, 90k tbn, 30 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native))
Error while opening decoder for input stream #0:0 : Operation not permitted
It creates 0-length file so the write permissions are granted, actually this folder have full access to everyone.
Can someone help me to debug this issue?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest