Install Motion and get it working with multiple cameras

If you’ve found out what Motion is then you’re probably looking for a way to view video feeds or stills from one or more webcams or network cameras in a web browser – for whatever reason. This is a quick guide to be able to view web feed  from a USB web camera as well as a network camera (D-Link DCS-5222L).

Start by installing Motion and libav-tools.

Motion actually needs ffmpeg, but it is not available with apt-get for Raspbian. It’s possible to compile ffmpeg from source, but avconv in libav-tools uses the same syntax as ffmpeg. This means you can create an alias in bash so that programs that try to access ffmpeg actually use avconv. Add the following line to /etc/bash.bashrc or to ~/.bashrc.

To allow motion to run as a daemon, use an editor to edit the file /etc/default/motion. Set the variable ‘set_motion_daemon’ to yes.

Now change to the directory where Motion was installed.

The default motion.conf file includes a lot of configuration options – most of the default values can remain unchanged. If you only have a single camera you want to access view a web browser, then you can make all the changes in the motion.conf file. If you want to view feeds from multiple cameras, then you will need a threadX.conf file – one for each camera, where X is 1, 2, 3…

This tutorial uses two cameras, meaning there are three files in the motion directory: motion.conf, thread1.conf, and thread2.conf. Common configuration that apply to all cameras are included in motion.conf, camera specific configurations are included in the respective camera configuration file. Here, thread1.conf is configured for a usb camera connected to the Raspberry Pi, and thread2.conf is configured for a network camera.

The tutorial also covers how to increase security by using HTTPS to access Motion.

Generic configuration – motion.conf

Browse thru the default configuration file and get familiar with the configuration options. You may want to tweak the default resolution of images capture, the frame rate, auto-brightness, etc. These setting can also be configured specifically for each device in the associated thread configuration.

Motion has a web based control interface that can be presented as html output or raw output. For manual configuration, I find the html output to be the easiest to use since it provides links and forms that can be access thru the web browser. The raw interface is more suitable, IMHO, for web applications, e.g. if you are creating your own front-end or automation scripts.

Configure the port to access the web control interface with the variable webcontrol_port. The default value is 8080. If you only want to access the web control interface from the localhost, make sure webcontrol_localhost is set to on. This is important if the server is connected to the Internet. Raw or html output is configured thru the variable webcontrol_html_output.

You can also set a username and password to access the control interface by setting webcontrol_authentication username:password

Lastly, uncomment the reference to the thread configuration files  – this was furthest down in the default motion.conf file.

USB camera – thread1.conf

Run the following command to detect which device is associated with the connected usb web camera.

In thread1.conf, the videodevice is set to /dev/video0.

By default, Motion will store an image whenever motion is detected. This can be disabled by setting the variable output_pictures to ‘off’.

Set the directory where Motion will store videos and images for the camera with the variable target_dir. By default, Motion will store files in /var/lib/motion

You can configure the name of the camera to be displayed on the feed using the variable text_left.

In particular, take care to assign a unique port for the video stream, e.g. 8081. You can also set the stream_maxrate and set whether the stream is only accessible via the localhost with stream_localhost on.

Limit the number of streams by setting stream_limit to a positive non-zero value.

Network camera – thread2.conf

To access my D-Link, I found that all I needed was to set the variable netcam_url and netcam_userpass.

netcam_url <IP-address-to-netcam>/dms?nowprofileid=1

netcam_url <IP-address-to-netcam>/video1.mjpg

Motion and HTTPS

Motion does not support HTTPS access by default. But you can use nginx to proxy traffic between the end user and the Motion interfaces provided to localhost. The nginx site configuration file may look like the one below. Make sure to point the ssl_certificate, ssl_certificate_key, and ssl_dhparam to the location of the files on your server.

In this example, the port of the localhost that should be accessed is specified in the url. So localhost:8081 is accessed via https://server_name/8081/ in this configuration. It is possible to configure specific access rules for each location section, e.g. to allow/deny connections from specific IP address or IP address ranges. This may be desired if you only want the video streams to be accessed from within your own network or by specific hosts.

You can also enforce password protection of the site via Nginx directly by enabling basic authentication. Uncomment this section in the configuration below and create the file /etc/nginx/users. Add a <username>:<password_hash> pair for each use you want to add. Nginx supports a couple of ways to create the hash for basic authentication, one way is to use the openssl utility in the command line.

Provide the password when prompted and copy the output hash into the users file. The flag -crypt enforces the use of that standard Unix password algorithm. At least try to stay clear of MD5 as the hash algorithm.

The Nginx site configuration file may look like this:

Save the configuration and restart Nginx.