Ensure that your camera is being recognized by the system.
Install video4linux utilities
sudo apt install v4l-utils
List devices (cameras)
v4l2-ctl --list-devices
The output should be something like this:
HD Web Camera: HD Web Camera (usb-0000:00:14.0-4): /dev/video0 /dev/video1 /dev/media0
For detailed information on a specific device:
v4l2-ctl -d /dev/video0 --all
To list the resolutions supported by your camera (2 ways):
v4l2-ctl --list-formats-ext
Or
ffmpeg -f v4l2 -list_formats all -i /dev/video0
Another way is to list the video devices available in /dev
ls /dev/video*
If you have only one camera, the device should show up as /dev/video0 in your system.
Or with dmesg (it will print errors if any are found):
dmesg | grep -i camera
If there is necessary to update the camera’s firmware run:
sudo apt install fwupd
sudo fwupdmgr refresh
sudo fwupdmgr get-updates
sudo fwupdmgr update
Install motion
sudo apt-get install motion
Test Mode should give a early hint if motion can handle your camera. Type the following command and if the output is suspended (active) then motion is getting video from the camera.
sudo motion -s
Configure Motion system files
There is a chance that motion won’t create log and lib directories so verify if /var/log/motion and /var/bin/motion folders actually exist so you must create them and set change the directories and files ownership to the motion user:
chown -R motion:motion /etc/motion
chown -R motion:motion /var/log/motion
chown -R motion:motion /var/lib/motion
Configure /etc/motion/motion.conf
Check the following github repo for the full motion.conf file if you wish all options. If not follow the next steps with the default config file.
https://gist.github.com/richardhawthorn/72db3366d9824a3ed85de852bdb5ce0f
sudo nano /etc/motion/motion.conf
Depending on your system, the daemon mode should be off due to that motion would be running as a systemd service.
daemon off
# Restrict to localhost
webcontrol_localhost off
# HTTP control interface (default on port 8080)
webcontrol_port 8080
# Start streaming server on port 8081
stream_port 8081
# Stream quality and settings
stream_quality 100
stream_motion on
stream_maxrate 100
stream_localhost off
# IP camera configuration
stream_localhost off
stream_preview_scale 0.2
stream_preview_new_name on
video_device /dev/video0
target_dir /tmp/motion
output_pictures on
# Other motion configuration options
# See /usr/share/doc/motion/examples/motion-dist.conf.gz for additional options
# Set the settings as needed for your camera and network environment.
Restart motion service:
sudo systemctl restart motion
You should be able to see the live stream at http://ip_address:8080. It’s good practice to use ports above 50000 so lets change that in the config file for both webcontrol and the stream.
If not, you can check via “systemctl status motion” for errors or in the log at /var/log/motion/motion.log. You can also check if motion is running and listening on 8080 and 8081 with ss:
sudo ss -tunap | grep motion
sudo ss -tunap | grep 51081
If you find network problems, check your firewall configuration and open ports 8080 and 8081 if necessary.
Setting up a password to the live stream
Change the following options in /etc/motion/motion.conf. Type of configuration options to allow via the webcontrol (default port 8080). 2 is for advanced web configuration.
webcontrol_parms 2
The authentication method for the webcontrol. 1 is basic (user and password). Authentication string for the webcontrol. Syntax is username:password and you must remove the “;” (semicolon) to uncomment the line. Notice that the username is not a system user. Just make up a username to perform login inside the file.
webcontrol_auth_method 1
webcontrol_authentication someuser:pass
Let’s do the same for webstream
stream_auth_method 1
stream_authentication someuser:pass
sudo restart motion.service
Your camera live feed should be available at your_local_ip:port and this time you will have to provide the user and password you have setup in the config file.
Setup ssl-https with a self-signed certificate
Install openssl if is not present on your system.
sudo apt install openssl
Create the key, csr, crt, and pem files. In the example below, the validity period is 2 years. Create a certs folder in /etc/motion. cd into the folder and type:
sudo openssl genrsa -out /etc/motion/certs/motion.key 4096sudo openssl req -new -key /etc/motion/certs/motion.key -out /etc/motion/certs/motion.csr
sudo openssl x509 -req -in /etc/motion/certs/motion.csr -signkey /etc/motion/certs/motion.key -out /etc/motion/certs/motion.crt -days 730
sudo cat /etc/motion/certs/motion.crt /etc/motion/certs/motion.key > /etc/motion/certs/motion.pem
Change ownership of the folder and all four created files to the motion user:
sudo chown -R motion:motion /etc/motion/certs
Configure motion.conf file again to add the paths to the crt and key files already created and don’t forget to remove the semicolons.
# Use ssl / tls for the webcontrol
webcontrol_tls on
# Use ssl / tls for stream.
stream_tls on
# Full path and file name of the certificate file for tls
webcontrol_cert /etc/ssl/motion/certs/motion.crt
# Full path and file name of the key file for tls
webcontrol_key /etc/motion/certs/motion.key
# Use ssl / tls for stream.
stream_tls on
sudo systemctl restart motion
Remember to configure port forward in your router and to open the ports in your system to successfully receive connections. Your camera should be availabe at your_public_ip:port
Optional configuration for apache server
If you wish to have the live stream available over the internet you will have to setup a virtual host in apache. In this example the live feed is available in a subdomain of my main site.
The following extra modules for a proxy pass configuration need to be enabled.
sudo a2enmod headers proxy proxy_http
Create a new file in /etc/apache/sites-available/cam.yoursite.com.conf. Make sure you already have the required certificates from lets encrypt to your subdomain. Motion will not handle the certificates but rather apache. You must comment the lines with the path to other certificate and private key if you have been using them in motion.conf. The example here is using port 51081.
<VirtualHost *:443>
ServerName cam.yoursite.
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/cam.yoursite.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/cam.yoursite.com/privkey.pem
<Location />
# Motion feed address
ProxyPass http://localhost:51081/
ProxyPassReverse http://localhost:51081/
ProxyPreserveHost On
RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Forwarded-Port "443"
</Location>
</VirtualHost>
Enable the new site and restart apache.
sudo a2ensite cam.yoursite.com.conf
sudo service apache2 restart
You should be able to view the live video stream with SSL by typing https://cam.yoursite.com and to access. I’ve left webcontrol only available over the LAN. Ports redirection will be handled automatically.
Example of a simple motion.conf file.
# Rename this distribution example file to motion.conf
#
# This config file was generated by motion 4.5.1
# Documentation: /usr/share/doc/motion/motion_guide.html
#
# This file contains only the basic configuration options to get a
# system working. There are many more options available. Please
# consult the documentation for the complete list of all options.
#
############################################################
# System control configuration parameters
############################################################
# Start in daemon (background) mode and release terminal.
daemon off
# Start in Setup-Mode, daemon disabled.
setup_mode off
# File to store the process ID.
; pid_file value
# File to write logs messages into. If not defined stderr and syslog is used.
log_file /var/log/motion/motion.log
# Level of log messages [1..9] (EMG, ALR, CRT, ERR, WRN, NTC, INF, DBG, ALL).
log_level 6
# Target directory for pictures, snapshots and movies
#target_dir /var/lib/motion
target_dir /PATH/TO/YOUR/DESIRED/FOLDER
# Video device (e.g. /dev/video0) to be used for capturing.
video_device /dev/video0
# Parameters to control video device. See motion_guide.html
; video_params YUYV
# The full URL of the network camera stream.
; netcam_url value
# Name of mmal camera (e.g. vc.ril.camera for pi camera).
; mmalcam_name value
# Camera control parameters (see raspivid/raspistill tool documentation)
; mmalcam_params value
############################################################
# Image Processing configuration parameters
############################################################
# Image width in pixels.
width 800
# Image height in pixels.
height 600
# Maximum number of frames to be captured per second.
framerate 60
# Rotate image this number of degrees. The rotation affects all saved images as
# well as movies. Valid values: 0 (default = no rotation), 90, 180 and 270.
rotate 0
# Text to be overlayed in the lower left corner of images
text_left Camera_1
# Text to be overlayed in the lower right corner of images.
text_right %Y-%m-%d\n%T-%q
# v4l2_palette allows one to choose preferable palette to be use by motion
# to capture from those supported by your videodevice. (default: 17)
# E.g. if your videodevice supports both V4L2_PIX_FMT_SBGGR8 and
# V4L2_PIX_FMT_MJPEG then motion will by default use V4L2_PIX_FMT_MJPEG.
# Setting v4l2_palette to 2 forces motion to use V4L2_PIX_FMT_SBGGR8
# instead.
#
# Values :
# V4l2 Option FOURCC v4l2_palette option
# V4L2_PIX_FMT_SN9C10X S910 0
# V4L2_PIX_FMT_SBGGR16 BYR2 1
# V4L2_PIX_FMT_SBGGR8 BA81 2
# V4L2_PIX_FMT_SPCA561 S561 3
# V4L2_PIX_FMT_SGBRG8 GBRG 4
# V4L2_PIX_FMT_SGRBG8 GRBG 5
# V4L2_PIX_FMT_PAC207 P207 6
# V4L2_PIX_FMT_PJPG PJPG 7
# V4L2_PIX_FMT_MJPEG MJPG 8
# V4L2_PIX_FMT_JPEG JPEG 9
# V4L2_PIX_FMT_RGB24 RGB3 10
# V4L2_PIX_FMT_SPCA501 S501 11
# V4L2_PIX_FMT_SPCA505 S505 12
# V4L2_PIX_FMT_SPCA508 S508 13
# V4L2_PIX_FMT_UYVY UYVY 14
# V4L2_PIX_FMT_YUYV YUYV 15
# V4L2_PIX_FMT_YUV422P 422P 16
# V4L2_PIX_FMT_YUV420 YU12 17
# V4L2_PIX_FMT_Y10 Y10 18
# V4L2_PIX_FMT_Y12 Y12 19
# V4L2_PIX_FMT_GREY GREY 20
# v4l2_palette 17
############################################################
# Motion detection configuration parameters
############################################################
# Always save pictures and movies even if there was no motion.
emulate_motion off
# Threshold for number of changed pixels that triggers motion.
threshold 1500
# Noise threshold for the motion detection.
; noise_level 32
# Despeckle the image using (E/e)rode or (D/d)ilate or (l)abel.
despeckle_filter EedDl
# Number of images that must contain motion to trigger an event.
minimum_motion_frames 1
# Gap in seconds of no motion detected that triggers the end of an event.
event_gap 30
# The number of pre-captured (buffered) pictures from before motion.
pre_capture 3
# Number of frames to capture after motion is no longer detected.
post_capture 90
############################################################
# Script execution configuration parameters
############################################################
# Command to be executed when an event starts.
; on_event_start value
# Command to be executed when an event ends.
; on_event_end value
# Command to be executed when a movie file is closed.
; on_movie_end value
############################################################
# Picture output configuration parameters
############################################################
# Output pictures when motion is detected
picture_output off
# File name(without extension) for pictures relative to target directory
picture_filename %Y%m%d%H%M%S-%q
############################################################
# Movie output configuration parameters
############################################################
# Create movies of motion events.
movie_output on
# Maximum length of movie in seconds.
movie_max_time 60
# The encoding quality of the movie. (0=use bitrate. 1=worst quality, 100=best)
movie_quality 45
# Container/Codec to used for the movie. See motion_guide.html
movie_codec mkv
# File name(without extension) for movies relative to target directory
movie_filename %t-%v-%Y%m%d%H%M%S
############################################################
# Webcontrol configuration parameters
############################################################
# Port number used for the webcontrol.
webcontrol_port 51080
# Restrict webcontrol connections to the localhost.
webcontrol_localhost off
# Type of configuration options to allow via the webcontrol.
webcontrol_parms 2
# Set the authentication method (default: 0)
# 0 = disabled
# 1 = Basic authentication (username:password)
# 2 = MD5 digest (the safer authentication)
webcontrol_auth_method 1
# Authentication for the http based control. Syntax username:password
# Default: not defined (Disabled)
webcontrol_authentication user:pass
############################################################
# Live stream configuration parameters
############################################################
# The port number for the live stream.
stream_port 51081
# Restrict stream connections to the localhost.
stream_localhost off
# Set the authentication method (default: 0)
# 0 = disabled
# 1 = Basic authentication
# 2 = MD5 digest (the safer authentication)
stream_auth_method 1
# Authentication for the stream. Syntax username:password
# Default: not defined (Disabled)
stream_authentication user:pass
##############################################################
# Camera config files - One for each camera.
##############################################################
; camera /usr/etc/motion/camera1.conf
; camera /usr/etc/motion/camera2.conf
; camera /usr/etc/motion/camera3.conf
; camera /usr/etc/motion/camera4.conf
##############################################################
# Directory to read '.conf' files for cameras.
##############################################################
; camera_dir /usr/etc/motion/conf.d