Motion

is a software package able to use cameras to detect motion.

It supports different camera types:
 * Network cameras via RTSP, RTMP and HTTP
 * V4L2 webcams
 * Video capture cards
 * Existing movie files

Once detects motion it can write picture files and/or movie files to a storage location.

Kernel
The linux kernel needs to be configured for V4L in case such cameras are being used.

Emerge
is in portage, emerge it as per normal:

Configuration
After emerging sample configuration files are placed in :

These configuration files are well documented, and can be used for creating the actual configuration files. The actual configuration files do not have "-dist" in their name. Note that simple renaming the files will not yield a working configuration. It may be better to create the configuration files from scratch, taking note of the individual settings needed for camera(s), and desired output options.

The structure of the configuration files is as follows:


 * : contains the general and daemon settings.
 * contains the settings per camera.

looks for the configuration files in the following order:


 * in the current working directory
 * in
 * in

An example working set of configuration files for two cameras, one laptop webcam and one IP-camera is as follows.

General settings:

First camera, a webcam using V4L:

Second camera, an IP camera:

Make sure to update credentials and the URL. Please refer to 's documentation for further details for the configuration.

Optimization
can be quite taxing on your hardware, especially if it is continuously receiving a stream of pictures, which it might need to process into video files, stream to client devices, and store somewhere on disk. It might be worth spending some time on optimization. With a modern the right optimization the system utilization could drop by as much 90%, YMMV depending on how hardware support there is available for the target system.

Video codec
supports different codecs via the parameter [//motion-project.github.io/motion_config.html#movie_codec movie_codec]. Some considerations for the choice of codec might be:
 * CPU load for the encoding: hardware acceleration might be configured to ease the load
 * Amount of disk space needed for storing the videos: some codecs are more efficient then others
 * Codec support on the devices that are used to view the video files: not all client devices support all codecs

Hardware acceleration
Various CPUs and GPUs have support for offloading the task of encoding videos. A choice should to be made, depending on the video codec, the type of CPU and GPU available.

Follow the instructions at GCC_optimization to at least set the proper march and the CPU_FLAGS. This will configure the system to use specific instructions that can optimize video encoding, e.g. instructions like [//en.wikipedia.org/wiki/SSSE3 SSSE3], [//en.wikipedia.org/wiki/SSE4#SSE4.2 SSE4.2], [//en.wikipedia.org/wiki/Advanced_Vector_Extensions AVX], [//en.wikipedia.org/wiki/FMA_instruction_set FMA3], [//en.wikipedia.org/wiki/Bit_Manipulation_Instruction_Sets#BMI2 BMI2], and [//en.wikipedia.org/wiki/Advanced_Vector_Extensions#AVX2 AVX2].

Evaluate the following articles to establish the support for hardware acceleration:
 * Intel CPUs:
 * [//en.wikipedia.org/wiki/Intel_Quick_Sync_Video#Hardware_decoding_and_encoding Intel Quick Sync Video] hardware support
 * GuC/HuC firmware
 * AMD GPUs:
 * AMDGPU
 * [//en.wikipedia.org/wiki/Video_Coding_Engine AMD's Video Coding Engine]
 * [//en.wikipedia.org/wiki/Video_Core_Next AMD's Video Core Next]
 * NVIDIA GPUs:
 * [//developer.nvidia.com/video-encode-decode-gpu-support-matrix Nvidia's encode/decode support matrix]

uses for video encoding. Check [//trac.ffmpeg.org/wiki/HWAccelIntro ffmpeg's hardware acceleration] for support.

Ffmpeg USE flags to consider include:
 * opencl
 * vaapi
 * vdpau

Support for Intel's libmfx is currently lacking in Gentoo, see for details. Note that hardware acceleration might require additional linux kernel configuration.

Motion tuning parameters
supports various configuration parameters which can be used for tuning purposes. These include:
 * framerate
 * movie_quality
 * movie_passthrough

Play around with it to establish the optimum between quality and resource utilization

Commandline
can be simply run from the commandline for testing purposes:

Once runs, it provides a webpage at. The webpage will show the live stream of the cameras, and allows for some basic control.

OpenRC
As of motion-4.3.1 there are new configuration parameters in :

Take note of the supervise-daemon USE flag, which, when set, will make run under OpenRC's supervise-daemon. Once satisfied with the configuration start the daemon process:

Security
Whenever one uses cameras it should be done in a safe manner:
 * avoid to aim cameras at privacy sensitive areas
 * for IP cameras:
 * use specific usernames or passwords on IP cameras; do not use default ones
 * prevent direct access to or from the Internet to your cameras; block access on the firewall or router.

Password protection
The webcontrol page and the streams can be protected with username and passwords, using three authentication mechanisms: 0 = disabled 1 = Basic authentication 2 = MD5 digest (the safer authentication)

SSL/TLS
Motion-4.2.2 and later supports SSL/TLS, which is great if the webserver or the streams are exposed over LAN, WAN or the Internet. A few quick steps to get going.

It's possible to create own self signed certificates as follows:

Update :

The motion webserver is now accessible over https instead of http.

Restrict webcontrol and streams to localhost
It might be a good idea to prevent the webcontrol page and/or the streams to be accessible on other computers:

Database
contains functionality to log events in a database. Supported databases include MySQL, MariaDB, Postgress, and [//sqlite.org sqlite3]. To use this functionality, the relevant USE flag has to be enabled, tables need to be created in the database, and the functionality need to be enabled in the configuration of. This chapter contains an example for sqlite, adapt as needed for other databases.

Create a sqlite database e.g. at and define a table called ""security"", as follows:

Exit with -.

Include the following to the configuration file:

Now every time a new picture, snapshot or movie is created, a new entry with details is written in the database. An application can use these details to retrieve the relevant file.

Housekeeping
Be aware that can produce many large files each day, which can take up a lot of storage. It might be a good idea to purge older files. Consider running a purging command like:

from a daily cron job, which would remove files older than time from a folder .