IP Camera recording with OpenHAB

My goal was to record every suspected situation with my three Foscam camera’s and also integrate it with OpenHAB. After a lot of very, very, very frustrating hours experimenting with video recording software like ZoneMinder, Shinobi, BlueIris, etc. I experienced that software like these are too heavy, too much overhead for such a simple task. Everything went slow, heavy, complicated and so on. I tried software motion detection but this takes hour to configure and so on. The final solution is very fast and solved with OpenHAB without a camara binding.

My slogan is: Keep is simple…!

  • Software motion detection? Too complicated and generates so much cpu load. Just some relaible PIR sensors in the neighborhood of the camera’s.
  • Camera database with fancy tools? Not nesessary, just drop every cameyra recording as a mp4 file in a folder on my server with the name format: YYYYMMDD_HHMMSS_CameraName. If nescessary, you can query the standard OpenHAB persist binding like MySQL

My configuration:

  • OpenHAB on linux Ubuntu server 18.x.x. (on a small dedicated Intel NUC computer)
  • 3 Foscam camera’s IP9900

How does it work? Quite simple! The only small tool you need to have is ’ ffmpeg’. This tool is able to capture a live stream from any camera and save it to a file.

  1. The PIR motion detector detects … wel motion;
  2. a rule is triggerd after the motion detection;
  3. this rule fires a small linux script which calls ffmpeg
  4. ffmpeg stores the live camera stream to disk for a specified time.

That’s all. The only hassle was file permissions on linux. A hassle because I’m a linux newbee… After all, everything is working fine now.

rule "Camera recording Oprit"
	when 
	   Item CameraOprit changed from OFF to ON
	then		
  {
   if(TimerCameraOprit === null)
      {
      val result = executeCommandLine("/etc/openhab2/scripts/record_oprit.sh 60",500)
      logInfo("record_oprit.sh",result)
      TimerCameraOprit = createTimer(now.plusMinutes(1)) 
        [|        
         CameraOprit.sendCommand(OFF)
       	 TimerCameraOprit = null
        	]
       }
    }         
	end

The ExecuteCommandLine calls the linux script with ffmpeg.

#!/bin/bash

CameraName=Oprit
InputStream=rtsp://admin:password@ipaddress:88/videoMain 
OutputDirectory=/mnt/data/Camera/

OutputFile=$(date "+%Y%m%d")_$(date+"%H%M%S")_$CameraName.mp4

ffmpeg -i $InputStream -t $1 -b 900k -vcodec copy -r 25  $OutputDirectory$OutputFile >$OutputDirectory$CameraName.log 2>&1 </dev/null

# -i : input file url
# -t : duration time
# > Redirect stdout to  logfile
# 2>&1 Redirect stderr to stdout (so it ends up in the logfile and not in the terminal)
# < /dev/null Redirect stdin (which ffmpeg thinks it needs) to /dev/null (which gives an empty stream).

That’s all.

8 Likes

Interesting way to do it, thanks for sharing as it is always great to have options to do it in a way that works for different cases… Any reason why you did not use the camera to store the file when motion is detected, surely a Foscam has that ability?

How are you displaying the mp4 files in Openhabs apps, habpanel and sitemaps? I can get mjpeg streams working but not mp4 streams as they just start downloading the file instead of displaying the video stream. Perhaps it is different for finished stored files compared to live streams.

There is a “FTP upload” binding which is from what I hear able to watch a folder for recordings and use that info to trigger motion as being detected. Camera stores the file and the binding knows the new file means movement. Good little trick to know.

Storing the images/movie on the camera is an option, but you need the terrible Foscam web-interface to access these files. I am also concerned about my privacy when someone pulls my camera’s from the wall and steal them :frowning:

For the occasion I need to look back the recorded streams, I do not use OpenHab. For live the streams I use TinyCam Pro. But I’m sure the will be a workaround to veiw them in OH.

Thanks for the trick.

I meant getting the camera to send the file to a NAS or a FTP site. Both my cameras can do this directly as well as storing on the cameras SD card. Not all cameras offer all these options but ones that do are <$120 If your camera does not have the option then I can understand setting it up in the way you have done. Getting a camera to do it has the advantage that you can tell the camera to store x seconds before an alarm occurs so you do not miss the event.

Check this post out as ffmpeg is very flexible as you already know.

1 Like