Save streaming video from TP-Link IP camera with openRTSPPosted: July 8, 2013
So I have got few of these TP-LINK made IP cameras TL-SC3171, which so far I am very happy with. After some extensive tests I have come to a conclusion that the best way to save streaming media served by the cameras is to use openRTSP (included in livemedia-utils package in Ubuntu based distributions).
Huge advantage of openRTSP is that it creates virtually no load on the system, except for some network traffic (but that is to be expected). I did run several of openRTSP instances simultaneously on my desktop computer, recording live streams from cameras, and it did not even show in GNOME system monitor graphs for CPU and memory usage! The top system monitor reports for a single openRTSP process 0% memory usage and alteration between 0% and 1% of used processor power! Fantastic!
I also tested openRTSP with one of those ingenious Raspberry Pi super-mini computers, and it was taking only between 2% and 3% of the processor power, just about the same amount that was consumed by the top utility itself, which I used to list the processes!
The command to record the media streams is quite straightforward (the video URL may be specific to the particular manufacturer of the camera):
If you have to login into your IP camera, you have to add user name followed by the password as extra parameters in the command line like this (credentials get encrypted when passed to the camera):
openRTSP -u myuser mypassword "rtsp://192.168.0.101/video.mp4"
Do not use the traditional notion:
Aside from passing on your credentials in plain text, it will not work and will end up with an error message:
Failed to get a SDP description for the URL "rtsp://myuser:firstname.lastname@example.org/video.mp4": 503 Service Unavailable
And, of course, if you just forget to provide your credentials, you will end up with:
Failed to get a SDP description for the URL "rtsp://192.168.0.101/video.mp4": 401 Unauthorized
Now, what will happen when the recording starts?
The openRTSP utility will dump the audio and video streams into two separate files: audio-PCMU-2 for audio and video-MP4V-ES-1 for video.
The audio is in raw headerless waveform data file format. I recommend that you add .ul extension to the raw audio file (actually that stands for µ-law), which will ease processing later on. You may also want to add .m4v extension to the video file for the same reason. Note: Don’t add .mp4 extension to the video file! If you do, you will have problems later, as it is incorrect and will confuse the software.
You can play the audio directly with play command (alias for the wonderful sox sound processing program), like this:
You can play it louder, if you want (normally you do, as the built-in mike is not too sensitive). The numeric value means volume, where 1 is 100% (the saved level):
play -v 10 audio-PCMU-2.ul
You even can seek to a particular moment in time and play it from there:
play audio-PCMU-2.ul trim 00:10
Or you can just convert the raw audio file to a regular WAV file like this:
sox audio-PCMU-2.ul audio-PCMU-2.wav
You may also choose to process the raw data in Audacity sound editor program. For that you need to open it:
File -> Import -> Raw Data
… and set the import parameters:
Byte order: Little-endian
Channels: 1 Channel (Mono)
Sample rate: 8000 Hz
The video stream is in MPEG-4 format, so one would expect no problems with playback. Still, strangely enough, the famous VLC player refuses to play the video unless the .m4v extension is added to the file, and so does Totem media player, while GNOME MPlayer plays the video easily. And then there is the SMPlayer.
Still, regardless of the extension, you may use the ffplay utility to play the initial MPEG-4 file:
ffplay -i video-MP4V-ES-1.m4v
You can seek in to a given point in timeline of the video file expressed in seconds:
ffplay -ss 30 -i video-MP4V-ES-1.m4v
By the way, you can record the audio and video simultaneously into a single AVI file (but to me it was useless as I had audio/video synchronization error). Just remember to redirect the output to a file:
openRTSP -i rtsp://192.168.0.101/video.mp4>myfile.avi
Please note that if recording is terminated for any reason before the AVI file is decently closed, it will become unusable and you will lose your record. Which is the main reason why separate recording described above is recommended instead.
Later you can run ffmpeg to extract separate frames from the video file (“%03d” here is a placeholder for frame numbering and “-r 1/10” is the frame rate):
ffmpeg -i video-MP4V-ES-1.m4v -r 1/10 frame%03d.jpg
Note: Tested on Linux Mint 13 Maya with MATE 1.2.0.