Turn a Raspberry Pi into NVR or DVR with Motion


Raspberry Pi is a low-cost micro-computer that is able to run Linux and has endless extension possibilities then can be also used for network video recorder (NVR) to records video in a digital format to a disk driveUSB flash drive, SD memory card or other mass storage device.
This post will guide you through the process of setting up your Raspberry Pi as a Network video recorder (NVR).

Requirements

You are going to need the following:
  • A Raspberry Pi
  • An Internet connection
  • An SD Card flashed with Raspbian OS (> 8 Gb)
  • IP or Analog camera with mjpeg streaming support or USB Camera (If you want record from Raspberry's camera module, Please visit "Raspberry Pi as low-cost HD surveillance camera")

Update the Raspberry Pi

Before continue make sure all packages, firmware, and kernel is up to date -  Run the following commands:
  • Update Packages
sudo apt-get update
sudo apt-get upgrade
  •  Update Firmware
sudo rpi-update
sudo ldconfig
sudo reboot
Make sure there are no error while updating the firmware or kernel

Install Motion

In order for the Raspberry Pi to act as a network video recording you need to install packages on the Raspberry. You are going to need the following packages
  • motion Motion is a program that monitors the video signal from one or more cameras and is able to detect if a significant part of the picture has changed; in other words, it can detect motion.
To install the motion packages run following command:
sudo apt-get update
sudo apt-get install motion
If there are no error at installation you can continue to the next step.

Configure the Motion

To configure the motion, make sure backup the file first then you can open file called /etc/motion/motion.conf edit this file or open it using this command:
sudo nano /etc/motion/motion.conf

The configuration

The configuration is summarized, some configuration is not showed Motion Configuration version 3.2.12 ######################### Run as daemon in the background
daemon off
This is are the pid (Process Identifier) // Change it to /home/pi/process/motion.pid to avoid permissions problems
process_id_file /var/run/motion/motion.pid 
This is are location of the videodevice // add comment for IP Camera
videodevice /dev/video0
Change to your videodevice mode
v4l2_palette 8
This used for USB Camera and other plugged to Pi camera
input 8
This used only for video capture or TV tuner cards (not used for IP Camera)
norm 0
This used only for TV tuner cards
frequency 0
Change it if you want use custom rotation degrees
rotate 0
Change it to your camera pixel width
width 320
Change it to your camera pixel height
height 240
Frame rate is the Frame per second of the recorded video from camera (high frame rate mean high CPU usage)
framerate 2
This will limit frame per seconds of the capturing images
minimum_frame_time 0
This is are the URL of the IP camera e.g : http://192.168.1.150/video.mjpg
; netcam_url value
This is are the username and password to login to your IP Camera e.g : admin:password
; netcam_userpass value
This is are the http version of your IP Camera , you can check by doing curl --head url
; netcam_http 1.0
This is are threshold for motion detection (high number = high sensitivity)
threshold 1500
This is are the video duration split before the movie is encoded to files (3600 Recommended)
max_mpeg_time 0
It will save image even no motion detected (turn it on if you want save the images per-frames and video even no motion detected)
output_all off
This will encode mpeg movies from the camera e.g 01-20140330-09-01-06.avi
ffmpeg_cap_new on
The quality of movie encoding (highest = high quality but takes more CPU load)
ffmpeg_bps 500000
I recommend the default value (it will depends on ffmpeg_bps).
ffmpeg_variable_bitrate 0
I'm currently using msmpeg4 format because it's almost multi-supported almost all platform
ffmpeg_video_codec msmpeg4
This is are where location the images or movies will be saved
target_dir /tmp/motion
After you finish the editing make sure you save the files Now we need to add the process PID and Video folder with (rwxrwxrwx) permissions to avoid motion bug on creating files and PID:
  • Create Pid folder
cd /home/pi/
mkdir process
cd process
mkdir motion
  •  Create Video folder (change the folder to yours)
cd /home/pi
mkdir Video
cd Video
mkdir motion

Start Motion

Now you can test motion by start motion service , you can do so by running :
motion
After you running the motion then there are no problem you can turn on daemon to on (run automatically at startup)
sudo nano /etc/default/motion
start_motion_enable=yes

Extra Scripts

This is are extra scripts list for Raspberry as NVR to ensure working as NVR properly

Remove Old Videos After x Days

Change /path/to/files to your file path and +5 it will find files older than 5 days or how long the video is saved then will deleted:
cd /home/pi
mkdir jobs
cd jobs
echo 'find /path/to/folder* -mtime +5 -exec rm {} \;' > cleanup.sh
crontab -e
Then add an extra line with the configuration of cron scheduler:
0 0 */1 * * /home/pi/jobs/cleanup.sh

Save videos on Windows shared folder (optional)

Raspberry can store the videos on the Windows shared folder , you can follow this steps:
sudo nano /etc/fstab
Then add an extra line with the configuration of your Windows network shared folder:
//YOURSERVERNAME/YOURSHAREDFOLDERNAME /mnt/camshare cifs username=YOURSHAREDFOLDERUSERNAME,password=YOURSHAREDFOLDERPASSWORD,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0
Be sure that the user has the correct permissions to save files to that shared folder. After a reboot, the
Raspberry should have an extra folder /mnt/camshare mounted to the Windows shared folder.

You should now set in your motion.conf:
target_dir /mnt/camshare
so that motion saves all movies to the shared folder on the Windows machine.

Extra Links

Troubleshooting

- Playback issues or freezing
  • Play the video with KMPlayer or another video player.
  • View the file size or diagnose the motion log file.
- Motion disconnect the IP Camera or Endpoint error
  • Use HTTP 1.0 instead of HTTP 1.1.
  • Try change netcam_tolerant_check to on.

Read also

  1. Motion's Wiki
  2. Raspberry Pi as low-cost HD surveillance camera
  3. How Can I keep My Raspbian “Wheezy” Up to Date? - István Simon's answer
  4. Image - Source: adafruit.com

Comments