More fun with a webcam

I had some time recently to be able to mess about with motion which is a free, open source, software motion detector.

Basically it will capture the frames from one or more cameras and compare each frame with the last. Usually it is configured to save a video clip or sequence of images only when movement is detected, but it can also be used to capture frames at regular intervals as well. Since I’d previously used a webcam to capture a sequence of images and turn them into a time-lapse video using fswebcam I decided to see if I could do the same using motion. Ideally I wanted to be able to produce a time-lapse video without having to process a collection of images, but though it is possible I found the results disappointing.

Before we can start using motion we need to be running as root.

$ su
Password:

OR

$ sudo -i
Password:

Then we need to update the package lists and install motion.

# apt-get update
  :
  :
  :
Fetched 16.5 MB in 31s (523 kB/s)
Reading package lists... Done
# apt-get install motion
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  libmysqlclient18 libpq5 mysql-common
Suggested packages:
  mysql-client postgresql-client
Recommended packages:
  ffmpeg
The following NEW packages will be installed:
  libmysqlclient18 libpq5 motion mysql-common
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 875 kB/1,138 kB of archives.
After this operation, 4,786 kB of additional disk space will be used.
Do you want to continue [Y/n]? Y
  :
  :
  :
Fetched 875 kB in 2s (418 kB/s)
Preconfiguring packages ...
  :
  :
  :
#

You will also need to install mencoder if you intend to combine a series of images into a video later – installing mencoder will also install mplayer which will allow you to play the time-lapse video produced by motion later.

# apt-get install mencoder
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following extra packages will be installed:
esound-common libaudiofile1 libesd0 libgif4 
liblircclient0 liblzo2-2 libsdl1.2debian 
libsvga1 libvdpau1 libx86-1 mplayer
Suggested packages:
pulseaudio-esound-compat lirc 
nvidia-vdpau-driver vdpau-driver mplayer-doc
ttf-freefont netselect fping
The following NEW packages will be installed:
esound-common libaudiofile1 libesd0 libgif4 
liblircclient0 liblzo2-2 libsdl1.2debian
libsvga1 libvdpau1 libx86-1 mencoder mplayer
0 upgraded, 12 newly installed, 0 to remove and 0 not upgraded.
Need to get 5,251 kB of archives.
After this operation, 11.5 MB of additional disk space will be used.
Do you want to continue [Y/n]? Y
:
:
:
#

Save a copy of the original motion configuration file (as it will come in handy later), and if it all goes horribly wrong just overwrite the modified configuration file with the original version you saved.

# cp /etc/motion/motion.conf /etc/motion/motion.conf.sav

Then modify the default configuration file.

# vi /etc/motion/motion.conf

The following sections show the changes needed to capture a sequence of images, and how capture a sequence of frames directly to an mpg video. If possible I prefer to comment out any existing entries using a semi-colon so I can see what changes I’ve made, however the default motion configuration file already has some lines commented this way, but using a semi-colon still makes it easier to keep track of any changes you make.

Note – The values that you need to use for the height and width will depend on your resolutions supported by your webcam, in my case I’m using a Logitech C310 which supports 1280×720.

Time lapse image capture

To configure motion to capture an image every four seconds find the following entries in the motion configuration file and change them to the values shown below. So that I can easily see what changed I’ve made rather than delete the original lines I comment them out using a semi-colon.

width 1280
height 720
pre_capture 4
post_capture 4
output_normal off
ffmpeg_cap_new off
ffmpeg_video_codec mpeg4
target_dir /tmp/motion
snapshot_interval 4
text_right %Y-%m-%d\n%T
webcam_port 8080
webcam_localhost off control_port 0

To start capturing images all you need to do is run motion.

# motion
[0] Processing thread 0 - config file /etc/motion/motion.conf
[0] Motion 3.2.12 Started
[0] ffmpeg LIBAVCODEC_BUILD 3482368 LIBAVFORMAT_BUILD 3478785
[0] Thread 1 is from /etc/motion/motion.conf
  :
  :
  :
[1] Resizing pre_capture buffer to 1 items
[1] Started stream webcam server in port 8080
[1] Resizing pre_capture buffer to 5 items
[1] File of type 2 saved to: /tmp/motion/01-20140810231740-snapshot.jpg
[1] File of type 2 saved to: /tmp/motion/01-20140810231744-snapshot.jpg
[1] File of type 2 saved to: /tmp/motion/01-20140810231748-snapshot.jpg
[1] File of type 2 saved to: /tmp/motion/01-20140810231752-snapshot.jpg
[1] File of type 2 saved to: /tmp/motion/01-20140810231756-snapshot.jpg
  :
  :
  :

Having captured your images all you need to do is to encode them using mencoder. You may need to change the values for the height and width if your images are a different size, and you can adjust the value for the crf (Constant Ratefactor) – lower values will produce a larger, higher quality video.

The default is 23 but I found that using a value of 16 produced a better quality result that was not so large as the be unmanageable.

$ mencoder -nosound \
> mf:///tmp/motion/*.jpg -mf w=1280:h=720:type=jpg:fps=20 \
> -ovc x264 -x264encopts pass=1:crf=16 \
> -lavfopts format=mp4 \
> -o /tmp/motion/time-lapse-video.mp4

To view the resulting video use mplayer.

# mplayer /tmp/motion/time-lapse-video.mp4

 

Time lapse mpg video capture

To configure motion to capture one frame every four seconds in an mpg video find the following entries in the motion configuration file and change them to the values shown below.

width 1280
height 720
pre_capture 4
post_capture 4
output_normal off
ffmpeg_cap_new off
ffmpeg_timelapse 4
ffmpeg_video_codec mpeg4
target_dir /tmp/motion
snapshot_filename lastsnap
webcam_port 8080
control_port 0

To start capturing frames all you need to do is run motion.

# motion
[0] Processing thread 0 - config file /etc/motion/motion.conf
[0] Motion 3.2.12 Started
[0] ffmpeg LIBAVCODEC_BUILD 3482368 LIBAVFORMAT_BUILD 3478785
[0] Thread 1 is from /etc/motion/motion.conf
[1] Thread 1 started
  :
  :
  :
[1] Using V4L2
[1] Resizing pre_capture buffer to 1 items
[1] Started stream webcam server in port 8080
[1] Resizing pre_capture buffer to 5 items
[1] File of type 2 saved to: /tmp/motion/lastsnap.jpg
[1] File of type 32 saved to: /tmp/motion/20140810-timelapse.mpg
[1] File of type 2 saved to: /tmp/motion/lastsnap.jpg
[1] File of type 2 saved to: /tmp/motion/lastsnap.jpg
  :
  :
  :

To view the resulting video use mplayer.

# mplayer /tmp/motion/*timelapse.mpg

Although it was possible to create an mpg video on the fly using motion the quality was comparatively poor compared to one that had been created from a sequence of jpg images using the first method.

Advertisements
This entry was posted in Debian, Linux, Raspbian and tagged , . Bookmark the permalink.

One Response to More fun with a webcam

  1. iroulson says:

    Reblogged this on rPi Kitchen and commented:
    This looks like an excellent summary showing how to use Motion to create time lapse video. I’ll have to try it out on my Raspberry Pi sometime.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s