Capture RTSP stream from IP Camera and store

Solution 1:

IP cameras are of varying quality, some behaving erratically in my experience. Dealing with their RTSP streams requires a dose of fault-tolerance.

The Live555 project provides a relatively fault-tolerant RTSP client implementation, openRTSP, for pulling RTSP audio/video streams via CLI: http://www.live555.com/openRTSP/

For example, to save a camera's RTSP audio/video to files in QuickTime format (AVI and MP4 also available), one file every 15 minutes:

$ openRTSP -D 1 -c -B 10000000 -b 10000000 -q -Q -F cam_eight -d 28800 -P 900 -t -u admin 123456 rtsp://192.168.1.108:554/11

These options mean:

-D 1 # Quit if no packets for 1 second or more
-c   # Continuously record, after completion of -d timeframe
-B 10000000 # Input buffer of 10 MB
-b 10000000 # Output buffer 10MB (to file)
-q   # Produce files in QuickTime format
-Q   # Display QOS statistics 
-F cam_eight  # Prefix output filenames with this text
-d 28800      # Run openRTSP this many seconds
-P 900        # Start a new output file every -P seconds
-t            # Request camera end stream over TCP, not UDP
-u admin 123456  # Username and password expected by camera
rtsp://192.168.1.108:554/11  # Camera's RTSP URL

Removing the -t option causes openRTSP to default to UDP instead, which can reduce network traffic a bit. You will need to play with the options in order to find the combination which suits you.

Frankly, the cameras themselves are sometimes unreliable, or just implemented differently -like closing the socket unexpectedly is not all that unusual.

Sometimes the openRTSP client does not catch these glitches. So I've opted to code a controller in Python using the 'subprocesses' module to invoke and monitor the stdout of each openRTSP client instance, and also check that the files are continuing to grow in size.

This appears to be a byproduct of the low-end of the CCTV industry playing fast and loose with standards, RTSP and ONVIF being the two most frequently abused.

Fortunately, you can usually work around these problems. Unless your IP cameras and controller are all designed to play nicely together, only use ONVIF for once-only discovery and settings management.

I use openRTSP on a few Raspberry Pi B+ running Raspbian. Each 1280x1024 stream occupies around 8-10% of the CPU's time, and I've successfully run up to eight cameras per RPi, writing the files to NAS storage. Another RPi processes completed files with ffmpeg, searching for motion and produce index PNGs of those frames, to assist with spotting break-ins.

There is an open-source effort called ZoneMinder which does this latter part, but I was unable to get it working with my cameras. ONVIF support is new and nascent in ZM, and it doesn't seem to contend well with the spotty RTSP streams produced by my menagerie of under-$100 IP cameras.

Solution 2:

If I follow your question correctly, why don't you try the following command on a Linux system (RPi):

ffmpeg -i rtsp://192.168.0.21:554/mpeg4 -vcodec copy -acodec copy -map 0 -f segment -segment_time 300 -segment_format mp4 "ffmpeg_capture-%03d.mp4"

This should save the video in chunks of 300 seconds. (Note that the clip length will depend on your input and output frame rates)

Solution 3:

I just thought I'd add my two cents and complement BjornR's answer.

Instead of running a cron job to periodically kill the VLC process, one could tell VLC to run for a specified amount of time and close afterwards.

This is the command that I run on my box:

/usr/bin/vlc -vvv rtsp://192.168.1.128:1554/11 --sout=file/ts:/media/path/to/save/location/recording-$(date +"%Y%m%d%H%M%S").ts -I dummy --stop-time=480 vlc://quit

This runs VLC for the specified amount of time and later exits. The vlc://quit parameter is required since VLC would stop recording and stay open. This command needs to be placed inside a loop.

The only problem I've found so far is that it might miss a few seconds every time a new recording starts.

Solution 4:

VLC looks like an ideal candidate to process your stream. Basic methods to capture a stream are described on the Videolan website. I sucessfully recorded the output of my D-Link DCS-5222 network camera using the following command:

vlc rtsp://user:password@ip/play1.sdp --sout=file/ogg:mystream.ogv

In your case, this might work to save the output locally:

vlc rtsp://192.168.0.21:554/mpeg4 --sout=file/ts:mystream.mpg

I'd suggest to run a script that ends this vlc process and launch a new instance every 30 minutes as i'm not sure VLC is able to do this.

As for storing on a NAS, just mount it to your local file system.