*Edit ( Aug/18/2014 ): I made a C/C++ program that stores the stream video from Foscam IP camera with libvlc. It may work for you better. Check out the article
here.
iSpy is a cool open source software that records surveillance video. Although it has several features, the features I was using was recording video/audio when motion is detected. It had been working very well and I was enjoying it a lot.
A little problem was that this software has to be running 24/7 and all of the video data must be streamed from IP camera to the computer that iSpy is running even when there is no motions are detected. Because the motion detection happens on the computer side not on the camera side, the computer must download all the video data from the IP camera via WiFi. I figure the video data isn't too big. It was about 7.3MByte per minute, which is 122KByte/sec.
Today I found that my IP Camera (
Foscam FI8910W Wireless IP Camera) has motion detection feature already in it. So it made me thinking that when IP camera detect any motions, it can let my computer know that the recording has to start. This way, the WiFi bandwidth is saved and CPU power that were supposed to be spent on image processing is saved. I don't think neither of them are big deal but I started implementing my idea for fun.
The idea was that my Foscam Camera can upload image files onto FTP server when motion is detected. But it doesn't have any ways to upload video streaming data. So I am recording video streaming data when the FTP connection is requested, while the actual image uploading from the camera will be ignored.
The working scenario is like this:
- Foscam Camera monitors the scene.
- Foscam Camera detect motions
- When any motions are detected Foscam Camera try to connect to FTP server and upload image files.
- On the FTP server side, it ignores whatever is sent from Foscam Camera.
- But at the time FTP connection is requested, VLC is launched and VLC start recording video streaming data directly from Foscam Camera.
Settings on Foscam Camera
"FTP Service Settings" looks like this.
- "FTP Server" is the IP number of the computer that is going store the video streaming data.
- "FTP port" is a fake number of this specific task. I will use a number "2121" for this article but you can change it for any numbers.
- "FTP User" should be "camera". It is case sensitive like most of FTP login ID.
- "FTP Password" doesn't matter for now. But you can make it count by changing the fake ftp server batch file later.
- "FTP Mode" should be "PASV" for now.
The way I am explaining here is not going to use a real FTP server. I made a fake FTP server that pretends to be a FTP server. So User/Password doesn't matter at all.
"Alarm Service Settings" looks like this:
- "Motion Detection Alarmed" should be checked.
- "Upload image on Alarm" should be checked.
- "Upload interval (seconds)" can be bigger than 60. This number is something you can tweak later. I don't recommend any number below 60.
These are all settings on Foscam Camera side.
Now when it detect any motions, the Camera will try to upload to the computer. If you have a real FTP server, it will be working. But you will get only images not audio/video.
Now I will explain computer side. This is little hacky and hard to understand but there is no way to harm the computer at all. You will need to install NetCat for windows and VLC for windows. My implementation is all based on Windows but if you are familiar with shell script, you can translate it by yourself because NetCat and VLC should be working on Linux/Mac too.
You need to install
Let me show you how VLC can record video stream directly. ( This example is taken from
here )
"C:\Program Files\VideoLAN\VLC\vlc.exe"
http://xxx.xxx.xxx.xxx:####/videostream.asf?user=UserName&pwd=Password
--qt-start-minimized --no-qt-notification --run-time=TimeInSeconds
:demux=dump :demuxdump-file=MyCamera1.asf vlc://quit
Other URL commands for Foscam IP Camera is
here.
Now let's have a batch file that know where to store the video stream data and where to get from.
Save this batch file as "
c:\batch\record_foscam.bat"
@ECHO OFF
SET IP=xxx.xxx.xxx.xxx
SET ID=userNameForCamera
SET PWD=passwordForCamera
SET run_time_in_sec=60
REM *** PATH should not have quotation marks ***
SET VLC=C:\Program Files\VideoLAN\VLC\VLC.EXE
SET ASF_BASEDIR=C:\surveillance\video\Foscam
SET YEAR=%DATE:~-4%
SET MONTH=%DATE:~4,2%
SET DAY=%DATE:~7,2%
SET HOUR=%TIME:~0,2%
SET MIN=%TIME:~3,2%
SET SEC=%time:~6,2%
SET ASF_FILENAME=%ASF_BASEDIR%\1_%YEAR%-%MONTH%-%DAY%_%HOUR%-%MIN%-%SEC%.asf
ECHO Recording %run_time_in_sec%seconds of Video stream on to %ASF_FILENAME%...
REM "%VLC%" http://%ID%@%IP%/videostream.asf^?user=%ID%^&pwd=%PWD%^&res=8^&rate=6 --qt-start-minimized --noqt-notification --run-time=%run_time_in_sec% :demux=dump :demuxdump-file="%ASF_FILENAME%" vlc://quit
"%VLC%" http://%ID%@%IP%/videostream.asf^?user=%ID%^&pwd=%PWD%^&res=8^&rate=6 --qt-start-minimized --noqt-notification --run-time=%run_time_in_sec% --sout=#transcode{vcodec=h264,acodec=mpga,channels=1}:standard{dst="%ASF_FILENAME%"} vlc://quit
You will need to change IP, ID, PWD and ASF_BASEDIR.
- IP is the IP number of your Foscam IP Camera.
- ID is the user name for the camera that you use when you access Camera Web Interface.
- PWD is the password for the user name on the Camera.
- ASF_BASEDIR is where you want to store the video streaming data.
At this point, you should run this batch file and see if it does store the video data.
If you don't get any files saved, you will need check the id, password and VLC path.
Now here is my Fake FTP Server.
Save it as a batch file like "
c:\batch\fake_ftp_server.bat"
@ECHO OFF
REM *** IP number of FTP server ***
SET ServerIP=xxx.xxx.xxx.xxx
REM *** Actual data transfer port number ***
SET ServerDataPort=2120
REM *** Case sensitive user name ***
SET ValidUserID=camera
REM *** Where is the batch file that triggers VLC to record video stream ***
REM *** Do no use quotation marks on the path
SET VLC_CAPTURE_BAT=C:\batch\record_foscam.bat
REM *** Do not change below ***
SET /a ServerPortHigh=%ServerDataPort% / 256
SET /a ServerPortLow=%ServerDataPort% - %ServerPortHigh% * 256
ECHO 220 Welcome message
SET /p user=
PAUSE > NUL
IF NOT "x%user%" == "xUSER %ValidUserID%" GOTO :ERROR_AUTH
ECHO 331 Please specify the password.
SET /p pass=
PAUSE > NUL
ECHO 230 Guest login ok, access restrictions apply.
SET /P typei=
PAUSE > NUL
ECHO 200 Type set to I
SET /P pasv=
ECHO 227 Entering passive mode (%ServerIP:.=,%,%ServerPortHigh%,%ServerPortLow%)
PAUSE > NUL
SET /P stor=
ECHO 150 FILE: no file will be stored but video recording will start...START /B CMD /C CALL "%VLC_CAPTURE_BAT%" 2> NUL > NULECHO 226 Transfer complete.
PAUSE > NUL
SET /P next=
PAUSE > NUL
GOTO :END
:ERROR_AUTH
ECHO 530 Invalid login information...
GOTO :END
:END
Once you saved this batch file somewhere, you will need to modify two variables: ServerIP, ServerPort.
- ServerIP is the IP number of the computer.
- ServerDataPort needs little explanation. When FTP client access to a FTP server, it makes two connections. One is for the command communication and the other is for actual data transporting. They are traditionally port number 21 and 20 respectively. In this article, I am using 2121 and 2120 respectively. If you scroll up, you can see "FTP port" is set to be 2121 on the Foscam Camera and this fake_ftp_server is going to listen to the port number. Then this fake FTP server has to tell what port number is for the data transfer. So the ServerDataPort should be any port number that is not in use. In this article I will use 2120 for the data transfer.
Remember that this FTP server is a fake FTP server. It can check user name and password but for now it cares user name but not password.
The last step of this article is to have a network port listener.
NetCat will keep listening any network access from other machines.
Here is the batch file and save it as "
c:\batch\fake_ftp_listener.bat"
@ECHO OFF
SET FTP_Port=2121
SET FTP_DataPort=2120
REM *** Do not use quotation marks on path
SET FTP_SERVER=C:\batch\fake_ftp_server.bat
SET NETCAT=C:\batch\nc111nt\nc.exe
ECHO [%DATE% %TIME%] Starting fake FTP server for IP camera...
:RESTART
START /B CMD /C "%NETCAT%" -l -p %FTP_DataPort% ^> NUL
"%NETCAT%" -l -p %FTP_Port% -e "%FTP_SERVER%"
"%NETCAT%" -z localhost %FTP_DataPort%
GOTO :RESTART
You will need to set the Path of batch file and NetCat; FTP_SERVER and NETCAT respectively.
Make sure the Port numbers are matching to other batch files; FTP_Port and FTP_DataPort.
Once you start this batch file, NetCat will start listening two network ports.
Now try to run the batch file.
To test whether it works or not, you can manually connect to the port while fake ftp listener is running.
telnet localhost 2121
If you see a welcome message, type in "USER camera".
If it asks password, then it is working.
As an optional step, you can start it as a hidden process when the windows start.
Save this line as a visual basic script file, "c:\batch\invisible.vbs"
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
Save this line as a batch file, "c:\batch\fake_ftp_listener_hidden_start.bat"
@"C:\Windows\System32\wscript.exe" "C:\batch\invisible.vbs" "C:\batch\fake_ftp_listener.bat"
Now you can make a shortcut of the "fake_ftp_listener_hidden_start.bat" in start up folder of your windows.
It is usually C:\Users\[YourUserName]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup