From Gentoo Wiki
Jump to: navigation, search

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 motion detects motion it can write picture files and/or movie files to a storage location.



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

KERNEL Enable support for V4L
Device Drivers --->
<M> Multimedia support  ---> 
    [*] Media Controller API
    [*] V4L2 sub-device userspace API
    [*] Media USB Adapters  ---> 
        <M> USB Video Class (UVC) 
        [*]   UVC input events device support
        <M> GSPCA based webcams  --->
            ** select the correct GSPCA camera, if any **
        ** select the correct USB camera, if any **
    [*] Media PCI Adapters  ---> 
        ** select the correct PCI camera devices, if any ***
    [*] V4L platform devices  --->
        ** select the correct V4L platform devices, if any **
    [*] Autoselect ancillary drivers (tuners, sensors, i2c, spi, frontends) 
[*] USB support  --->  
    <M> USB Gadget Support  ---> 
        <M> USB Gadget functions configurable through configfs
        <M> USB Webcam Gadget

USE flags

USE flags for media-video/motion A software motion detector

ffmpeg Enable ffmpeg/libav-based audio/video codec support
mmal Enables Multi-Media Abstraction Layer (MMAL) decoding support: Available e.g. on the Raspberry Pi.
mysql Add mySQL Database support
postgres Add support for the postgresql database
sqlite Add support for sqliteembedded sql database
v4l Enable support for video4linux (using linux-headers or userspace libv4l libraries)
webp Add support for the WebP image format


Motion is in portage, emerge it as per normal:

root #emerge --ask --verbose media-video/motion



After emerging sample configuration files are placed in /etc/motion:

root #ls /etc/motion
drwxr-xr-x 1 root root   390 Apr 10 15:14 .
drwxr-xr-x 1 root root  3616 Apr 10 13:31 ..
-rw-r--r-- 1 root root  2772 Apr  9 19:54 /etc/motion/camera1-dist.conf
-rw-r--r-- 1 root root  2771 Apr  9 19:54 /etc/motion/camera2-dist.conf
-rw-r--r-- 1 root root  2772 Apr  9 19:54 /etc/motion/camera3-dist.conf
-rw-r--r-- 1 root root  2772 Apr  9 19:54 /etc/motion/camera4-dist.conf
-rw-r--r-- 1 root root 27681 Apr  9 19:54 /etc/motion/motion-dist.conf

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:

  • motion.conf: contains the general and daemon settings.
  • camera#.conf contains the settings per camera.

Motion looks for the configuration files in the following order:

  • in the current directory
  • in ~/.motion
  • in /etc/motion

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

General settings:

FILE /etc/motion/motion.conf
log_level 5
webcontrol_port 8080
webcontrol_authentication <username:password>
webcontrol_parms 2
ffmpeg_output_movies on
ffmpeg_video_codec mkv
ffmpeg_duplicate_frames false
target_dir /tmp/motion
output_pictures off
camera /etc/motion/camera1.conf
camera /etc/motion/camera2.conf

First camera, a webcam using V4L:

FILE /etc/motion/camera1.conf
camera_id = 1
camera_name = webcam
videodevice /dev/video0
stream_port 8081
stream_authentication <username:password>
stream_motion on
stream_maxrate 25
stream_localhost on

Second camera, an IP camera:

FILE /etc/motion/camera2.conf
camera_id = 2
camera_name = livingroom
netcam_userpass <username:password>
netcam_url rtsp://IP-ADDRESS/stream2
stream_port 8082
stream_authentication <username:password>
stream_motion on
stream_maxrate 25
stream_localhost on

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


Motion 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.

Video codec

Motion supports different codecs. 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 needs 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 SSSE3, SSE4.2, AVX, FMA3, BMI2, and AVX2.

Evaluate the following articles to establish the support for hardware acceleration:

Motion uses media-video/ffmpeg for video encoding. Check ffmpeg's hardware accellation for support.

Ffmpeg USE flags to consider include:

  • opencl
  • vaapi
  • vdpau

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

Motion tuning parameters

Motion 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



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

user $motion
[-1083001888:motion] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion/motion.conf
[-1083001888:motion] [NTC] [ALL] config_camera: Processing camera config file /etc/motion/camera1.conf
[-1083001888:motion] [NTC] [ALL] config_camera: Processing camera config file /etc/motion/camera2.conf
[-1083001888:motion] [NTC] [ALL] motion_startup: Motion 4.1.1 Started
[-1083001888:motion] [NTC] [ALL] motion_startup: Logging to syslog
[-1083001888:motion] [NTC] [ALL] motion_startup: Using default log type (ALL)
[-1083001888:motion] [NTC] [ALL] motion_startup: Using log type (ALL) log level (WRN)

Once motion runs, it provides a webpage at http://localhost:8080. The webpage will show the live stream of the cameras, and allows for some basic control.



Motion can also run as a system daemon, once satisfied with the configuration:

root #rc-config add motion default
root #rc-service motion start


Whenever one uses cameras it should be done in a safe manner:

  • avoid to aim cameras at privacy sensitive areas,
  • use specific usernames or passwords on IP cameras; do not use default ones.

Motion version 4.2 features a few security enhancements:

  • webcontrol_tls
  • webcontrol_auth_method
  • webcontrol_authentication
  • tream_tls
  • stream_auth_method
  • stream_authentication.

Make sure to implement these features once version 4.2 is available. Monitor bug #673410 for updates. Until then it might be better to prevent motion from being controlled by, and streaming to, other computers. Add the following to the configuration files:

  • webcontrol_localhost on
  • stream_localhost on


Be aware that motion 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:

/usr/bin/find <storage-location> -type f -mtime +<days> -delete

from a daily cron job, which would remove files older then <days> time from a folder <storage-location>.