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.
cd ffmpeg-dmo-0.11
- setup the source
./configure
- compile and install ffmpeg
sudo make && make install
Configuring ffmpeg
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
/usr/sbin/webcam.sh
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 !
21 comments
The user can use the webcam for the webcam streaming via raspberry by following the above article, the post is showing the process by which the user will do that.
Hi ,
I want to display USB camera video on my Raspberry pi ??? help me !!!
Hi,
Thanks for this tutorial. I am getting this error:
.....
Input #0, video4linux2,v4l2, from '/dev/video0':
Duration: N/A, start: 11400.222941, bitrate: 24576 kb/s
Stream #0:0: Video: rawvideo, 1 reference frame (YUY2 / 0x32595559), yuyv422, 640x480, 24576 kb/s, 5 fps, 5 tbr, 1000k tbn, 1000k tbc
[http @ 0x118b540] HTTP error 404 Not Found
http://localhost/webcam.ffm: Server returned 404 Not Found
And my config file is so:
HTTPPort 81
HTTPBindAddress 0.0.0.0
MaxClients 10
MaxBandwidth 100000
File /tmp/webcam.ffm
FileMaxSize 10M
Feed webcam.ffm
Format mjpeg
VideoSize 1280x720
VideoFrameRate 15
VideoBitRate 5000
VideoQMin 1
VideoQMax 10
What am I doing wrong?
Sorry,
I changed the portnumber to 81, it was the reason for “not found” error. Now I am getting this error:
[tcp @ 0x99cd40] Connection to tcp://localhost:81 failed: Connection refused
localhost:81/webcam.ffm: Connection refused
Hi, this is probably, one of the most perfectly explained post on FFserver, so thank u so much for this.
I managed to get it to work a few times, and now, I always have the error: Missing audio stream which is required for this ffm.
even if I add NoAudio in the stream section of my ffserver.conf.
I tried a lot of different format to, and still get stuck…
Any ideas where it could come from?
Thanks a lot
Hi my friend, I’m using ffmpeg and ffserver and also meet this problem. I added ‘ NoAudio’ in ffserver.conf and even in command “-c:an” but the error still exits.
I’m searching the solutions through the internet and found nothing.
So have you found out how to solve it ? So could you please tell me what’s going on there?
It will be kind of you if you could send the solution to my email: jfjxxdy2007 xx 126.com
I enter my url and places: File ??stream.html? not found. . what’s going on?
Great tutorial.
Unfortunately when I’m opening the stream in my browser, it always wants to download the file to my disk.
I also can’t embed the video using html5 (because html doesn’t support mjpeg?)
What am I doing wrong?
Is there a way to change to codex to h264?
Man you were not kidding when you said “Compiling ffmpeg on the Pi will take a while”.
Hi! Nice looking tutorial. I wanted to accomplish the exact same thing, but I couldn’t because of this error:
.
…
…..
…….
Input #0, video4linux2,v412, from ‘/dev/video0’ :
Duration: N/A, start: 281.544110, bitrate: 24576 kb/s
Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 640×480, 24576 kb/s, 5fps, 5tbr, 1000k tbn, 1000k tbc
[http @ 0xfe3030] HTTP error 401 Unauthorized
http://192.168.1.1/webcam.ffm: Input/output error
Conversion failed!
[raspberrypi@alarmpi ~]$ _
And yes, not I am on Arch Linux Arm. I have set up everything like said, but this error doesn’t have to do with (I don’t think at least…) Arch Linux vs Raspbian or Debian or whatever…
Anything you can tell me?
Hey,
thanks for your Tutorial. But I’m just not able to get this to work. It’s always refusing..Any idea? I’m getting this:
sudo /usr/sbin/webcam.sh
ffserver version 2.1.3 Copyright (c) 2000-2013 the FFmpeg developers
built on Feb 19 2014 21:40:56 with gcc 4.6 (Debian 4.6.3-14+rpi1)
configuration:
libavutil 52. 48.101 / 52. 48.101
libavcodec 55. 39.101 / 55. 39.101
libavformat 55. 19.104 / 55. 19.104
libavdevice 55. 5.100 / 55. 5.100
libavfilter 3. 90.100 / 3. 90.100
libswscale 2. 5.101 / 2. 5.101
libswresample 0. 17.104 / 0. 17.104
ffmpeg version 2.1.3 Copyright (c) 2000-2013 the FFmpeg developers
built on Feb 19 2014 21:40:56 with gcc 4.6 (Debian 4.6.3-14+rpi1)
configuration:
libavutil 52. 48.101 / 52. 48.101
libavcodec 55. 39.101 / 55. 39.101
libavformat 55. 19.104 / 55. 19.104
libavdevice 55. 5.100 / 55. 5.100
libavfilter 3. 90.100 / 3. 90.100
libswscale 2. 5.101 / 2. 5.101
libswresample 0. 17.104 / 0. 17.104
[video4linux2,v4l2 @ 0x22b7de0] fd:3 capabilities:85000001
[video4linux2,v4l2 @ 0x22b7de0] The driver does not allow to change time per frame
[video4linux2,v4l2 @ 0x22b7de0] ioctl(VIDIOC_STREAMON): Operation not permitted
/dev/video0: Operation not permitted
Streamer and Motion don’t work either. I tried 2 webcams already. LED is blinking short and then no reaction. On a HUB and directly at the Pi. Nothing. Did I miss anything? Any Idea?
Which RP image did you use to start off with?
when I run it it says
[video4linux2 @ 0x1d5660] Cannot find a proper format for codec_id 0, pix_fmt -1. /dev/video0: Input/output error
Anybody have a solution?
What do I put in the field in my browser?
I’m a noob.
the web address, that is
the IP address of your raspberry Pi.
we have a little problem ! is that the system keeps showing this message on the screen the whole night and we dont know if its the right way or not ! please if you could help us it would be great thnk you !
message :
” make : warning : file ‘testes/fate/wma.mak’ has modification time 5.8 e+06 in the future
is your OS set to the right time? I suggest make sure the time is right, and start over. If the time is not correct or changed during a build, Make does not work properly.
Very good article! We will be linking to this particularly great post on our website.
Keep up the good writing
Great posting on FFmpeg, that is useful, I looked around and this was the only good tutorial I found on FFmpeg.
Hey Oscar,
Nice blogs!
I’ve tried webcam on raspberry pi a couple month ago.
When using the motion on the raspberry pi, the pi freezes after about 2 minutes, quite disappointing. Then i tried beaglebone black.
It is much faster and never freezes. Just in case you want a try, analogdigitallab.org/articles/beaglebone-black-webcam