Pages

2012-05-18

Raspberry Pi webcam

*** UPDATE NOTE: I am now using Motion instead of ffserver see this new post: Battery powered, Wireless, Motion detecting Raspberry Pi ***

Here's a walk-through of the steps I took to get a webcam stream on the Internet from my Raspberry Pi.

Equipment:
 Raspberry Pi
 8G class 4 Sandisk SD card
 5V 1A MicroUSB power supply
 Ethernet cable
 HDMI cable
 USB keyboard
 Webcam (PS3 EyeToy)

Prepare the OS
 Arch Linux ARM - you can get the image file from the Raspberry Pi download page.
 Write the image to the SD card. (I use Win32DiskImager)
 Expand the main partition to fill the SD card. From my Windows laptop I booted to a GParted Live CD to do this.

Go headless
 Change the root password!

passwd root
 Set up a fixed IP address. (You may need to change your router settings to define a fixed IP address range) Edit /etc/rc.conf using nano:
nano /etc/rc.conf
 Follow the instructions to set up your fixed IP something like this:
interface=eth0
address=192.168.0.101
netmask=255.255.255.0
broadcast=192.168.0.255
gateway=192.168.0.1
 Also change resolv.conf to use your router for name resolution:
nameserver 192.168.0.1
 Since this Pi is running headless we don't need so much RAM allocated to the GPU. So swap the default start.elf for the 224 version:
cd /boot
mv start.elf orig-start.elf
cp arm224_start.elf start.elf
 Unplug the keyboard and HDMI, reboot and then ssh to your Pi. (I use PuTTY)

Start streaming
 Update your packages and install mplayer and ffmpeg:

pacman -Syu
pacman -S mplayer
pacman -S ffmpeg
 Create an ffserver.conf configuration file in /root. Here's mine:
Port 8090 
BindAddress 0.0.0.0 
MaxClients 4
MaxBandwidth 10000 
NoDaemon 

<Feed webcam.ffm> 
File /tmp/webcam.ffm 
FileMaxSize 5M 
</Feed> 

<Stream webcam.asf> 
Feed webcam.ffm 
Format asf 
VideoCodec msmpeg4 
VideoFrameRate 2
VideoBufferSize 80000 
VideoBitRate 200 
VideoQMin 1 
VideoQMax 10 
VideoSize qvga
PreRoll 0 
Noaudio 
</Stream>

<Stream webcam.mjpeg>
Feed webcam.ffm
Format mpjpeg
VideoSize qvga
VideoFrameRate 2
VideoIntraOnly
Noaudio
Strict -1
</Stream>
 Start the streaming server:
ffserver -f /root/ffserver.conf & ffmpeg -v 2 -r 5 -s 640x480 -f video4linux2 -i /dev/video0 http://localhost:8090/webcam.ffm
 You should now have two streams available from your Raspberry Pi. One is an asf stream which you can connect to from players like Windows Media Player or VLC. The other is an mjpeg stream which you can view from a web browser without requiring any plugins.
 The mjpeg URL is:
http://192.168.0.101:8090/webcam.mjpeg
 The asf stream is:
http://192.168.0.101:8090/webcam.asf

Put it on the Internet
If you just want to see this on the Internet temporarily simply open up port 8090 on your home router firewall settings. Set up an Inbound Service on port 8090 to 192.168.0.101. Now find your external ip address and you should be able to connect through that. Your external address will probably be shown somewhere in your router web interface or you can find it by going to here: http://ip.dnsexit.com/
Your external address will change occasionally unless you have a fixed IP address. In another post I'll show how to set up Dynamic DNS with an address updater so you can reach your Pi on the net through a name rather than address. The updater ensures that if your external address changes the Dynamic DNS provider is given the new one.

*** UPDATE NOTE: I am now using Motion instead of ffserver see this new post: Battery powered, Wireless, Motion detecting Raspberry Pi ***

My Raspberry Pi webcam stream is sometimes available here: http://jerbly.uk.to/picam
Please be gentle. If you don't see a stream try again another time!

50 comments:

  1. How did you get the Eyetoy to play nicely with the pi? Did you have to compile drivers? I'm trying a xbox live camera in debian with no success yet.

    ReplyDelete
  2. Nick, I'm using Arch Linux ARM and it worked right out of the box. No problems - no compilation required.

    ReplyDelete
  3. I suppose the beauty of the raspi is that I can create a separate SD card with Arch to experiment with that too. I'll give it a go. You should put your findings on the wiki as a compatible peripheral.

    ReplyDelete
  4. I've updated the wiki: http://elinux.org/RPi_VerifiedPeripherals#USB_Webcam

    ReplyDelete
  5. I think the Debian pi distro's kernel doesn't have the uvc module that's usually required.

    ReplyDelete
  6. Thanks for writing this Jeremy - you've probably saved me a few hours of my life. Didn't work first time for me (don't know why - motion could see /video0 but couldn't get images from it) but I created a fresh SD and it's running Microsoft life cam cinema with motion with no problems.

    can't handle the full 1280 x 720 in motion... but when you spend £20 on a computer....

    ReplyDelete
  7. Hey Jeremy,
    first thanks for this great Blog Post, but i'm still facing some problems.
    I use the same equipment as you.
    I start the stream like you. Then i connect via vlc to the asf stream. The stream runs no, but on Rasp it'S throwing:"Codec for stream 0 does not use global headers but container format requires global headers
    " Sometimes it doesn't even work. The mpeg stream on browser doesnt work at all.
    Do you have any idea why?
    Thanks!

    ReplyDelete
  8. HI All;

    this might be crazy, but what if I would hook up another webcam? would I be able to create two streams?

    ReplyDelete
  9. @Anonymous (1): I get that warning sometimes but it doesn't stop anything working. Sorry.

    ReplyDelete
  10. @Anonymous (2): I guess you could try two webcams but streaming from both might stretch the Raspberry Pi a bit! ffmpeg is CPU hungry - take a look at top when you're streaming. I assume the second webcam would show up as /dev/video1.

    ReplyDelete
  11. Hi Jeremy,
    I managed to get it working tonight for my xbox live camera. I used Arch with
    mjpg_streamer -i "input_uvc.so -d /dev/video0" -o "output_http.so -p 8092 -w www"

    I'll see if it is stable enough over the next 24 hours and maybe get the camera on a website like you have.

    ReplyDelete
  12. Hey Jeremy!
    Awesome tutorial! I was wondering... would I be able to send real time video at at least 14FPS through WiFi on the same network with the setup you mention here? Thank you very much for sharing this information!

    ReplyDelete
  13. Thanks for this Jeremy, I want to try and get a WebCam working with the pi. I followed your instructions but got stuck. I posted what happened on The R Pi forum any ideas appreciated

    ReplyDelete
  14. I'm hoping to use a Raspberry Pi with the 'motion' software for my bird box webcams e.g. http://astrobeano.blogspot.co.uk/2012/03/xbox-birdbox-mk-iii.html

    I guess the good thing about not having my Raspberry Pi yet it other folk can sort out some of the road bumps in using it with a webcam - thank you!

    ReplyDelete
  15. Hi,

    I have found your article very interesting.

    I have an issue: there is no video in my /dev

    I have followed your steps... I use an old Philips web cam

    If some one can tell me what do to about it I would be very great full.

    Thank you,

    Andrej

    ReplyDelete
  16. Hey Jeremy,

    I want to try your tutorial (for creating a babymonitor with a Raspberry Pi)

    What kind of webcam would you adivice? (I will buying it new).

    Many thanks in advance and great blog!

    Grts

    ReplyDelete
  17. @JT Take a look at my Raspberry Pi page where I list all the equipment I've used so far.

    ReplyDelete
  18. Hello,

    I follow your instructions. I got this message:"
    libv4l2: error allocating conversion buffer". I need solutions for my problem. Thank you all!!

    ReplyDelete
  19. I am using an xbox live webcam and am getting the same libv412 error

    ReplyDelete
  20. @lazycurler @pokmon Are you using Arch or Raspbian? This setup was all done with Arch.

    ReplyDelete
  21. FYI: I've just set up the new raspbian wheezy distro with the PS3 camera and motion. It works. Just:
    sudo apt-get install motion

    ReplyDelete
  22. @Bill Smith I tray with Logitech C310HD and 2 others webcams. Everytime I got this "
    libv4l2: error allocating conversion buffer" error. I using last ARCH version. Thans for ideas!

    ReplyDelete
  23. It worked for me also but only on Arch. On Raspian i couldn't, motion still works great. I decided to use motion becouse it consume less ram, I still need some for 3g module, I'll describe all on my blog: http://sudoxanthippe.wordpress.com
    Greetings

    ReplyDelete
  24. Hi Jeremy,

    This is a great tutorial, but I'm having an issue when I try to run the ffmpeg and ffserver. I get:

    libv4l2: error allocating conversion buffer

    I'm using a Microsoft LifeCam Cinema attached to a powered hub. It works fine under motion, but motion doesn't give me the live stream quality that I want. any thoughts?

    ReplyDelete
  25. Jeremy, this is a really interesting post. RaspberryPi + webcam + ffmpeg = very useful product. Thanks!

    ReplyDelete
  26. When I enable ffserver I get the error "Segmentation fault". Any suggestions how to fix it?

    ReplyDelete
  27. Have you considered doing time lapse stills

    ReplyDelete
  28. thank you for article. I have bought webcam on ebay:
    http://www.ebay.com/itm/250783605298?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649
    and works perfectly with arch linux&motion!

    ReplyDelete
  29. Just a quick question trying the Raspbian Wheezy image that is now the default RPI basic download.

    I am downloading ffmpeg, but is the mplayer used for when web serving?

    Adrian

    ReplyDelete
  30. Thanks for an inspiring and very nice raspberry project site!
    Just got my raspberry up and run. I was hoping to use mine allmost like you, but I dont need the motion and webserver etc, just wantet that the raspberry with turn Microsoft HD usb cam into network cam. I then use another software (blueiris) to controll the motion and webserver part. But sadly i discovered that my skills is far away when it comes to linux. Do you think that in the future it will come ready configured sd cards for the use I need?

    ReplyDelete
  31. @AdrianH mplayer is not used for web serving. To be honest I have had most success with Motion for serving mjpeg on the internet. I have recently tried this with the latest Raspbian and it works fine.

    ReplyDelete
  32. anyone gets ffserver/ffmpeg running on raspbian? I got "Segmentation fault".

    ffmpeg 0.8.2-6:0.8.2-1+rpi1+b1
    Linux raspberrypi 3.1.9+ #168 PREEMPT Sat Jul 14 18:56:31 BST 2012 armv6l GNU/Linux

    ReplyDelete
  33. Hi Jeremy,

    This is a great tutorial, but I'm having an issue when I try to run the ffmpeg and ffserver. I get:

    libv4l2: error allocating conversion buffer

    I'm using a Microsoft LifeCam Cinema. Any thoughts? Thanks

    ReplyDelete
  34. @Anonymous - on Raspian I get a seg fault.

    On Arch I get the libv4l2: error allocating conversion buffer

    I am using an old Logitech webcam I had lying around. Unfortunately I have no solutions and googling that allocation error doesn't bring up much help.

    ReplyDelete
  35. I got "error allocating conversion buffer" message too.


    Logitech QuickCam Messenger(very old)
    @Arch Linux

    ReplyDelete
  36. Yeah, I've got same problem at Raspbian as you guys. I've got a Logitech webcam and the same "segmentation fault".

    ReplyDelete
  37. I too have followed this exactly and am getting the error
    libv4l2: error allocating conversion buffer

    I am using the eye toy camera, I got the exact same camera as i thought by doing so i would not get any problems! but problems exist......

    I tried several times on different sd cards and always the same!

    cheers
    Andrew

    ReplyDelete
  38. hi, andrew again, here is what happens when i try and start the ffserver


    [root@alarmpi ~]# ffserver -f /root/ffserver.conf & ffmpeg -v 2 -r 5 -s 640x480 -f video4linux2 -i /dev/video0 http://localhost:8090/webcam.ffm
    [1] 372
    ffserver version 0.11.1 Copyright (c) 2000-2012 the FFmpeg developers
    built on Jul 9 2012 21:58:39 with gcc 4.7.1
    configuration: --prefix=/usr --enable-libmp3lame --enable-libvorbis --enable-libxvid --enable-libx264 --enable-libvpx --enable-libtheora --enable-libgsm --enable-libspeex --enable-postproc --enable-shared --enable-x11grab --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libschroedinger --enable-libopenjpeg --enable-librtmp --enable-libpulse --enable-libv4l2 --enable-gpl --enable-version3 --enable-runtime-cpudetect --disable-debug --disable-static
    libavutil 51. 54.100 / 51. 54.100
    libavcodec 54. 23.100 / 54. 23.100
    libavformat 54. 6.100 / 54. 6.100
    libavdevice 54. 0.100 / 54. 0.100
    libavfilter 2. 77.100 / 2. 77.100
    libswscale 2. 1.100 / 2. 1.100
    libswresample 0. 15.100 / 0. 15.100
    libpostproc 52. 0.100 / 52. 0.100
    libv4l2: error allocating conversion buffer
    [root@alarmpi ~]#

    ReplyDelete
  39. Andrew again...

    is there any chance of getting a image file of your working sd card?

    its a good guide, just disapointed i cant get it to work, it would be ideal if it worked

    ReplyDelete
  40. I have just tried it on raspbian as cant get it working on arch and i too get the "segmentation fault"

    just dont know what to do next!!

    and the raspberry pi was designed for schoolkids to do programming on and none of us can get any current packages working on any operating system, very fustrating!! i must be so stupid!

    Andrew

    pi@raspberrypi ~ $ sudo ffserver -f /root/ffserver.conf & ffmpeg -v 2 -r 5 -s 640x480 -f video4linux2 -i /dev/video0 http://localhost:8090/webcam.ffm
    [1] 1682
    avserver version 0.8.2-6:0.8.2-1+rpi1+b1, Copyright (c) 2000-2011 the Libav developers
    built on Jun 27 2012 01:07:05 with gcc 4.6.3
    Segmentation fault
    pi@raspberrypi ~ $

    ReplyDelete
  41. I had the same experience both on Arch and on Raspbian...
    The webcam is a PS3 and it goes (I tried to take some photo with fswebcam).
    bye
    giorgio

    ReplyDelete
  42. Hello jeremy you wrote ps3 eyetoy camera but ps3's camera is called eye.The eyetoy camera is the ps2 camera so which of the two cameras you use?

    ReplyDelete
  43. Yeah I'm having the same problem with "Codec for stream 0 does not use global headers but container format requires global headers" whenever I try to connect to the stream with VLC. I followed all of the instructions, and here is my log:

    libavutil 51. 73.101 / 51. 73.101
    libavcodec 54. 59.100 / 54. 59.100
    libavformat 54. 29.104 / 54. 29.104
    libavdevice 54. 2.101 / 54. 2.101
    libavfilter 3. 17.100 / 3. 17.100
    libswscale 2. 1.101 / 2. 1.101
    libswresample 0. 15.100 / 0. 15.100
    libpostproc 52. 0.100 / 52. 0.100
    libv4l2: error allocating conversion buffer
    [root@alarmpi ~]# Codec for stream 0 does not use global headers but container format requires global headers

    ReplyDelete
  44. Jeremy, this has been a great break into linux project with the Pi. I have already learned a lot, thanks. My problem is getting audio to run as well. I want to use the pi as a baby monitor and in this case audio is more important. Any direction in getting the audio working would be great.

    ReplyDelete
  45. @Manny: To fix the global headers issue, I put this in my ffserver.conf: AVOptionVideo flags +global_header

    That got rid of that error. Now I just get the [video4linux2,v4l2 @ 0x1d77540] mmap: Cannot allocate memory
    /dev/video0: Cannot allocate memory error.

    ReplyDelete
  46. I've found a solution to the memory allocation issues at:

    http://home.nouwen.name/RaspberryPi/webcam.html

    ReplyDelete
  47. Dear Jeremy,

    could you show me the command to set up the inbound service on port 8090 to 192.168.0.101?

    ReplyDelete