Motion detection with Raspberry Pi. Monitor webcams, also RTSP

A common use case for a Raspberry Pi* is monitoring one or more network or webcams. With the RPi 3, this now also works quite well. I myself use an RPi 3 together with a network camera to monitor my driveway. In this configuration, the RPi detects a change in the monitored area and saves the webcam image. It would also be possible to record a short video. The software used here, "motion", is widely used and very powerful, so that many applications can certainly be covered. Here I will show the commissioning and the most important configuration settings so that you can then build on them to configure your own solutions.

  1. Overview
  2. Preparation
  3. ffmpeg for rtsp (optional)
  4. Install motion
  5. Configure motion
  6. Start motion

Overview: Raspberry Pi Cam, USB Camera, Webcam or Network Camera?

"motion" can monitor almost all cameras. Also the RPi cams. It is important that there is a Linux driver for this camera, then the camera appears in the file directory under /dev/video0.

If you have a network camera that is addressed via its own IP, this is also no problem, these streams can also be monitored. Even RTSP formats are possible, which is very practical since most cheap, but also better, Chinese devices now support this.

2. preparation

We will compile motion and required dependencies ourselves. This has the advantage that we have more functions than in the precompiled version.

Before we start, however, we should first clean up our system. To do this, we need to execute the following commands. The easiest way is to copy the commands into a Bash file and then execute it. Alternatively, the commands can of course also be executed individually:

sudo bash
apt-get update
apt-get upgrade
apt-get remove x264
apt-get remove libx264-dev
apt-get remove libavutil
apt-get remove libpostproc
apt-get remove libswresample
apt-get remove libswscale, ffmpeg
apt-get remove libavcodec
apt-get remove libavfilter
apt-get remove libavresample
apt-get remove libavcodec
apt-get remove libavfilter

Now we install the necessary tools to compile motion itself:

sudo bash
apt-get install autoconf automake build-essential libtool libjpeg8-dev libzip-dev
apt-get install libavformat-dev libavcodec-dev libavutil-dev libav-tools libswscale-dev
cd /
git clone git://
cd x264
./configure --enable-shared --disable-opencl
sudo make install && ldconfig

3. ffmpeg for RTSP (optional)

If we want to be sure that motion supports our camera or for RTSP, we now have to install ffmpeg. This takes almost 2 hours on the RPi 3 B:

cd /
sudo bash git clone git:// cd ffmpeg ./configure --arch=armhf --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree --enable-shared make sudo make install && ldconfig

4. Install motion

Now we install the actual monitoring programme, which is much faster again:

sudo bash
git clone
cd motion
autoreconf -fiv
sudo make install && ldconfig

5. Configure motion

Finally, we only have to adapt motion to our needs. This is done with the motion.conf file which we still have to set up:

sudo mv /usr/local/etc/motion/motion-dist.conf /usr/local/etc/motion/motion.conf

Then we can open the file for editing. Important settings are:

If you have a USB or RPi cam, you must enter the path here. If you have a network camera, this setting must be completely commented out.

If you have a network camera, the path to the stream (e.g. rtsp) must be entered here.

The optional access data to the network camera in the format username:password

width and height
The width and height of the webcam image. You should set these values to the same settings as the camera delivers the image. Otherwise motion converts the image, which costs quite a bit of computing power on the RPi.

Specifies how many pixels must change in the image for a change to be detected. Caution: This is strongly dependent on the video resolution! The specified 1500 are not usable with HD resolutions.

Specifies how much a pixel must deviate in order to be recognised as changed. This is useful to prevent noise from being recognised as movement.

Draws the number of detected changed pixels in the upper right corner. Very useful to find the right threshold.

Folder in which photos and videos are to be saved.

Specifies on which port an RTSP stream is provided with the image to be monitored.

If enabled, the stream can only be retrieved from localhost

Indicates on which port a small web server can be reached for control purposes.

If activated, the web server is only accessible from localhost.

Defines the location of the logfile. A very useful helper, especially at the beginning!

6. Start motion

Motion can now be gestured

/usr/local/bin/motion start


  1. Super instructions, many thanks. Of course, Git must also be installed...

    With x264 I get the following after
    ./configure -enable-shared -disable-opencl

    Unknown option -enable-shared, ignored
    Unknown option -disable-opencl, ignored

    Does that have to make me nervous?

  2. Somehow the instructions above contain the wrong ASCII characters. With -enable-shared it works. The same applies to ffmpeg.

  3. Another hint:
    sudo mv /usr/local/etc/motion/dist-motion.conf /usr/local/etc/motion/motion.conf

    "dist-motion.conf" must be "motion-dist.conf" here.

  4. Many thanks for the instructions! Unfortunately, when I do "make" in the "Motion" section, I get the following message:
    sudo make
    Welcome to the setup procedure for Motion, the motion detection daemon! If you get
    error messages during this procedure, please report them to the mailing list. The
    Motion Guide contains all information you should need to get Motion up and running.

    Version: 4.1.1+git804c28c
    Platform: Linux (if this is incorrect, please read README.FreeBSD)

    Compiling Motion object files...
    -e Compiling ffmpeg.c into ffmpeg.o...
    ffmpeg.c: In function 'ffmpeg_set_quality':
    ffmpeg.c:541:41: error: 'CODEC_FLAG_QSCALE' undeclared (first use in this function)
    ffmpeg->ctx_codec->flags |= CODEC_FLAG_QSCALE;
    ffmpeg.c:541:41: note: each undeclared identifier is reported only once for each function it appears in
    ffmpeg.c: In function 'ffmpeg_set_codec':
    ffmpeg.c:666:33: error: 'CODEC_FLAG_GLOBAL_HEADER' undeclared (first use in this function)
    ffmpeg->ctx_codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
    Makefile:108: recipe for target 'ffmpeg.o' failed
    make: *** [ffmpeg.o] Error 1

    I have not noticed any error message (apart from some deprecated warnings). I am grateful for any help!

  5. autoreconf -fiv bringt auf einem Raspbian GNU/Linux 10 (buster) folgende Fehlermeldung:
    Can’t exec „autopoint“: No such file or directory at /usr/share/autoconf/Autom4te/ line 345.

    Fix dazu:
    sudo apt-get install autopoint; sudo apt-get install autopoint

  6. autoreconf -fiv bringt auf einem Raspbian GNU/Linux 10 (buster) folgende Fehlermeldung:
    Can’t exec „autopoint“: No such file or directory at /usr/share/autoconf/Autom4te/ line 345.

    Fix dazu: sudo apt-get install autopoint; sudo apt-get install gettext

Leave a comment

E-mail address will not be published.