Monitor a Webcam

From SingletonMillerWiki
Jump to: navigation, search

Plug in your webcam

Before you plug in your webcam, try the following command at a console:

ls /dev/video*

I don't have any video devices on my system so I get:

No such file or directory

Now Plug in your webcam

List the USB devices using


ou should see an entry that looks sometihng like this.

Bus 001 Device 005: ID 1415:2000 Nam Tai E&E Products Ltd. or OmniVision Technologies, Inc. Sony Playstation Eye


Bus 001 Device 005: ID 045e:075d Microsoft Corp. LifeCam Cinema

Repeat the first command and make a note of the new devices names.

ls /dev/video*

Now we get


This means the device has automatically been detected and recognised by the system. It doesn't mean you have a means of watching the video data, not yet.

Video Tools

First we're going to need some video capture tools

MPlayer is capable of displaying a webcam video stream.

MEncoder can record from a webcam to video files. The companion to MPlayer,

FFmpeg is a cross-platform audio/video recording, conversion and streaming application.

sudo apt-get install mplayer mencoder ffmpeg

Wait patiently for the programmes and the dependencies to be installed.

User Permissions

List the current users who can access video devices, using

cat /etc/group | grep video

Modify the group to add your user

sudo gpasswd -a [user] video

replace [user] with your username eg. jack


Group changes only change when you next login, so logout and log back in.

View a live feed

mplayer tv:///dev/video0 

when viewing this via a remote SSH connection you'll need your RPi display on to see the output or don’t forget the -X option to enable X forwarding.Enable_SSH#Forwarding_the_X_terminal

Use q or ctrl-c to terminate the feed.

I noticed that there were a large number of dropped frames reported by mplayer.

MPlayer interrupted by signal 2 in module: filter_video
v4l2: ioctl set mute failed: Invalid argument
v4l2: 416 frames successfully processed, 1225 frames dropped

You can control the frame rate using the -fps switch.

mplayer -fps 3 tv:///dev/video0

Debian Squeeze Mplayer performance

The following table was determined whilst running mplayer monitoring the RPi performance using the command top in a separate terminal (via SSH Enable SSH).

The command used to test mplayer was sent via an SSH CLI whilst the RPi was pluged in to an HDMI monitor.

mplayer -fps 1 tv:// -tv driver=v4l2:device=/dev/video0:width=320:height=240

the following code was used to monitor the CPU usage via a second SSH terminal, there appears to be an initial spike in CPU usage during start-up, the spike is ignored in the subsequent performance table.

top | grep mplayer

at least ten updates from top was used to record the performance, a typical result is

 2245 pi    20   0 71948  14m 6312 S  33.9  7.8   0:01.03 mplayer           
 2245 pi    20   0 71948  14m 6312 S   3.3  7.8   0:01.13 mplayer           
 2245 pi    20   0 71948  14m 6312 S   3.3  7.8   0:01.23 mplayer           
 2245 pi    20   0 71948  14m 6312 S   3.3  7.8   0:01.33 mplayer           
 2245 pi    20   0 71948  14m 6312 S   3.3  7.8   0:01.43 mplayer           
 2245 pi    20   0 71948  14m 6312 S   3.3  7.8   0:01.53 mplayer           

Testing with higher frame rates and the output was unstable (showed green frames). 320x240 seems the most usable.

mplayer FPS performance @320x240 squeeze
fps CPU(%) MEM(%)
1 3.9 - 4.3 5.4
2 7.2 - 7.8 5.4
5 17.0 - 18.7 5.4
10 31.0 - 34.8 5.4
15 30.1 - 36.8 5.4
20 30.3 - 40.8 5.4
30 34.7 - 45.0 5.4

Raspian Mplayer Performance

Test with Raspian version Linux raspberrypi 3.1.9+ #168 PREEMPT Sat Jul 14 18:56:31 BST 2012 armv6l GNU/Linux

Raspian utilises the hard floating point capabilities of the GPU and should offer improved performance for some applications.

mplayer FPS performance
fps CPU(%) MEM(%)
1 3.3 - 3.9 7.8
2 5.9 - 6.6 7.8
5 13.8 - 14.8 7.8
10 23.6 - 29.0 7.8
15 25.6 - 32.1 7.8
20 26.1 - 32.4 7.8
25 25.7 - 37.1 7.8
30 24.3 - 33.4 7.8

There appears to be little difference in CPU usage for frame rates above 15fps, this might be a stability issue.

Save video to a file

ffmpeg -f video4linux2 -s vga -i /dev/video0 out.mpg

avconv -f video4linux2 -s vga -i /dev/video0 out.mpg

ffmpeg is deprecated and replaced by avconv on some systems (Raspian). I've found that ffmpeg seems more stable at higher resolutions 640x480.

The frame rate achieved is variable using Squeeze I get 4-7fps. There doesn't appear to be a way to control the frame rate as the video4linux appears to dictate the frame rate even when the Pi can't capture it all.

You may need to experiment with the driver -f video4linux2 or the size -s vga to match your webcam and optimise performance.

ffmpeg comsmes all the CPU time (>97%) in an attempt to capture the data at as high a rate as possible, a typical output is

Input #0, video4linux2, from '/dev/video0':
  Duration: N/A, start: 81238.769032, bitrate: N/A
    Stream #0.0: Video: rawvideo, yuyv422, 640x480, 1000k tbr, 1000k tbn, 1000k tbc
Output #0, mpeg, to 'out.mpg':
    Stream #0.0: Video: mpeg1video, yuv420p, 640x480, q=2-31, 200 kb/s, 90k tbn, 60 tbc
Stream mapping:
  Stream #0.0 -> #0.0
Press [q] to stop encoding
Frame=  430 fps=  7 q=31.0 size=     624kB time=7.15 bitrate= 714.9kbits/s

Remember to press 'q' to stop the process

Convert the video format

mencoder out.mpg -ovc lavc -lavcopts vcodec=mpeg4 -o out2.avi

This converts the mpeg2 data to mpeg4.

Note, transcoding video formats on your RPi will be slow.

Now you can playback using the hardware accelerated player omxplayer, assuming you have it installed. see Playing Videos

omxplayer out2.avi

omx players mpeg4 using 7% CPU, mplayer require 98% !

For playing streams on the RPi we really need is omxplayer to access the streams directly and the webcam can output in H.264 or MPEG4. Unfortunatly my webcam only does MPEG2, so i can't test this and I'm not really sure if omxplayer can access a video device direcly anyway.


  • USB Camera not detected.
  1. The RPi has limited capability to supply power to USB devices, try again using a powered USB hub.
  2. Search google to check if your webcam is support by linux.[1]


This page has been tested on the following RPi build standards

  1. Linux raspberrypi 3.1.9+ #95 PREEMPT Thu May 31 13:21:40 BST 2012 armv6l GNU/Linux
  2. Linux raspberrypi 3.1.9+ #138 PREEMPT Tue Jun 26 16:27:52 BST 2012 armv6l GNU/Linux