I was playing with face recognition on Raspberry Pi and found a lot of people were having issues with webcam streaming video, so I made this step by step guide to help you to setup the webcam, and also get you to the point where you can stream video from webcam on another machine via a browser.
Compiling FFMpeg For Webcam Streaming
First thing we need to do is to get a version of ffmpeg that can stream. If you haven’t installed Git on your Raspberry Pi, do that first.
sudo apt-get install git
Once git was installed, I went into /usr/src to download the source for ffmpeg.
cd /usr/src git clone git://source.ffmpeg.org/ffmpeg.git
Git retrieved the source code and we will need to build ffmpeg from scratch. If you need sound for ffmpeg, you will need to also install the libasound2-dev package which enables ALSA.
cd ffmpeg ./configure sudo make && make install
Compiling ffmpeg on the Pi will take a while, probably a good idea to leave it overnight. After it’s done do the following.
- Add the following lines into /etc/apt/sources.list
sudo deb-src http://www.deb-multimedia.org sid main sudo deb http://www.deb-multimedia.org wheezy main non-free
sudo apt-get update
sudo apt-get install deb-multimedia-keyring
- Remove the second line from /etc/apt/sources.list
deb http://www.deb-multimedia.org wheezy main non-free
sudo apt-get source ffmpeg-dmo
- You should now have a folder called ffmpeg-dmo-0.11 <– The version number might change as time goes by
- Change the directory to the folder containing the source. e.g.
- setup the source
- compile and install ffmpeg
sudo make && make install
Once ffmpeg is installed, we need to create a configuration file to enable ffmpeg to stream to ffserver.
ffserver is what will host the stream.
- We need to create a configuration file for ffserver, we will place it in /etc/ and call it ffserver.conf
Port 80 BindAddress 0.0.0.0 MaxClients 10 MaxBandwidth 50000 NoDaemon <Feed webcam.ffm> file /tmp/webcam.ffm FileMaxSize 10M </Feed> <Stream webcam.mjpeg> Feed webcam.ffm Format mjpeg VideoSize 640x480 VideoFrameRate 10 VideoBitRate 2000 VideoQMin 1 VideoQMax 10 </Stream>
The last stanza defines the size of the stream, and bitrate. If the parameters don’t suit each other, then the stream will not be smooth.
- Next, the following command needs to be put into a .sh file. This will allow you to start streaming by just running the .sh file. Let’s call it webcam.sh and put it in /usr/sbin as the file needs to be run as root.
ffserver -f /etc/ffserver.conf & ffmpeg -v verbose -r 5 -s 640x480 -f video4linux2 -i /dev/video0 http://localhost/webcam.ffm
- Once the .sh file has been created, and the above code has been placed into it, you need to make the file executable by running chmod +x /usr/sbin/webcam.sh
Start Streaming Video Via Browser
Once the shell script and configuration file has been created, you can start streaming by running
When you run it, you should get this as a result.
** 1 dup!2 fps= 5 q=2.6 size= 51136kB time=00:06:56.40 bitrate=1006.0kbits/s dup=359 drop=0
And ffmpeg is now streaming, and you should be able to access the video stream from web address on
http://<YOUR WEBCAM SERVER>/webcam.mjpeg
When I tried streaming, I found out that the Logitech webcam only supports 30fps and 15fps, so the driver automatically forces it to 15fps.
Streaming from raw video to mjpeg isn’t very cpu intensive for the Pi but it can’t keep up streaming at a good fps on the Logitech Webcam. I tried it on the lowest resolution possible which is 176×144 and I still had a about 2 second delay.
I tried some of my other cheaper webcams which didn’t have mjpeg format and they performed a bit better than the Logitech one in the Raw streaming, maybe because the fact that the Logitech forced 15 fps.
Almost doubling the resolution to 320×240 resulted in barely any video on the Logitech with ffmpeg stalling altogether, and not streaming to ffserver at all, and the same for the cheap webcam.
Problems with Webcam Streaming – Webcam Support
Some people were having issues with video streaming would either not start or would stop very quickly. I had a look at the settings that I was using to stream from my Raspberry Pi and I was using 320×240 at 10fps.
I increase the resolution to 640×480 and I started having similar issue. I was getting errors like the one below
[video4linux2,v4l2 @ 0x1d4e520] The v4l2 frame is 40588 bytes, but 614400 bytes are expected
After some googling, it seems there are webcams that can’t stream video from mjpeg directly, and frame size might be reduced and therefore does not work. Lowering the resolution to 160×120 helps but it’s not what we want.
I tried a second spare webcam, and it worked perfectly on 640×480 without any tweaking. Seems that some webcams just can’t do what we want them to do !