Jan 072013
 

So I recently got back into the Pi as I could now use the PiSU to do a few things.
I saw a Logitech c110 webcam and decided to pick that up to test out the mjpeg world of webcams.

I started off with a fresh re-install of Raspbian Server Edition (RSE), and found that the instructions in Part 1 don’t work any more. Not sure why that is, but I’m assuming there’s an issue in the code somewhere for ffmpeg so I ended up using git to get the source code to build.

First step is to install git, as RSE didn’t include git.
I was root when I ran the rest of these commands, so if you’re not root, either sudo bash to become root, or just prepend sudo to everything

apt-get install git

Once git was installed, I went into /usr/src to download the source.

cd /usr/src
git clone git://source.ffmpeg.org/ffmpeg.git

Git retrieved the source code I needed to build ffmpeg from scratch, which is next up !
*Note*
If you need sound for ffmpeg, you will need to also install the libasound2-dev package which enables ALSA.

cd ffmpeg
./configure
make && make install

*Note* Compiling ffmpeg on the Pi will take a while, I left it running overnight to let it finish up. */Note*

Once ffmpeg was compiled and installed, I followed the same steps as before to setup ffmpeg.
I’ll repost the config that I used here –

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 320x240
VideoFrameRate 10
VideoBitRate 20000
VideoQMin 1
VideoQMax 10
</Stream>

<Stream stat.html>
Format status
</Stream>

And the script file I use to start ffserver and ffmpeg

ffserver -f /etc/ff.conf & ffmpeg -v quiet -r 5 -s 320x240 -f video4linux2 -i /dev/video0 http://localhost/webcam.ffm

When I tried streaming, I found out that the webcam only supports 30fps and 15fps, so the driver automatically forces it to 15fps.
For some reason, ffserver also has trouble streaming mjpeg when the source is also mjpeg.
To stream straight mjpeg, I used the -vcodec mjpeg parameter by itself in the script to start ffmpeg. The output of ffmpeg shows that it is definitely streaming straight mjpeg but for some reason I can’t tap into the stream.
Looking at the ffmpeg/ffserver status page, it shows that ffserver is definitely pushing data to my devices, so I’m unsure as to what is happening there.

When I was streaming it from raw -> mjpeg, it could stream on my laptop via VLC but not on my Android Mobile.
I’m currently looking into why it might be doing this, but I’m stumped πŸ™
Streaming from raw video -> 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 ~2 second delay.

I tried some of my other cheaper webcams which didn’t have mjpeg format and they fared a bit better than the Logitech one in the Raw department, possibly due to the fact that the Logitech forced 15fps.

CPU usage was around 20% with the 176×144 resolution with a cheap webcam, and 25% with the Logitech c110
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.

I seem to recall in older versions of ffmpeg and Raspbian that the cheaper webcams could still stream well at 320×240, so I’m unsure as to whether it’s ffmpeg causing issues or Raspbian now.

That’s all from me for now, hopefully this post helps someone get their webcams up and running πŸ™‚

Share

  46 Responses to “How To : Stream A Webcam From The Raspberry Pi Part 3”

  1. Thanks for this great series of How To’s. I am also trying to get mjpeg streaming working correctly via ffmpeg, but not having much luck using a MS VX5000. The camera does work with mjpg_streamer, (http://sourceforge.net/projects/mjpg-streamer/), though I would still like to get ffmpeg working so I can try capturing audio as well.

    When setting -vcodec to mjpeg, I get the same unspecified pixel format error that someone mentioned in a comment on your second post. So, I tried specifying a pixel format by setting -pix_fmt to various options but none seem to work. What pixel format is it auto-detecting for your camera when you use mjpeg? Also, I’m trying to figure out if I can get the information from the camera using v4l2-ctl, but I haven’t been able to yet…

    Thanks,
    Brent

    • Thanks for reading πŸ™‚
      My 2 webcams only required -vcodec mjpeg for mjpeg streaming to work.

      What have you been able to get out of v4l2-ctl ?
      Have you tried using uvcdynctrl with the -f argument ?

      For one of them, Pixel format comes up as ‘JPEG’ (compressed) when I use the command v4l2-ctl –list-formats
      Don’t have the other one handy at the moment sorry !

      • output from uvcdynctrl

        # uvcdynctrl -f
        Listing available frame formats for device video0:
        Pixel format: YUYV (YUV 4:2:2 (YUYV); MIME type: video/x-raw-yuv)
        Frame size: 640x480
        Frame rates: 15
        Frame size: 352x288
        Frame rates: 30
        Frame size: 320x240
        Frame rates: 30
        Frame size: 176x144
        Frame rates: 30
        Frame size: 160x120
        Frame rates: 30
        Pixel format: MJPG (MJPEG; MIME type: image/jpeg)
        Frame size: 640x480
        Frame rates: 30
        Frame size: 352x288
        Frame rates: 30
        Frame size: 320x240
        Frame rates: 30
        Frame size: 176x144
        Frame rates: 30
        Frame size: 160x120
        Frame rates: 30

        output from v4l2-ctl


        # v4l2-ctl --list-formats
        ioctl: VIDIOC_ENUM_FMT
        Index : 0
        Type : Video Capture
        Pixel Format: 'YUYV'
        Name : YUV 4:2:2 (YUYV)

        Index : 1
        Type : Video Capture
        Pixel Format: 'MJPG' (compressed)
        Name : MJPEG

        I think ffmpeg wants something like what’s listed in ffmpeg -pix_fmts (list is pretty long but an example is yuv420p). So when I try to run ffmpeg, I get:


        # ffmpeg -v verbose -r 30 -s 640x480 -f v4l2 -pix_fmt yuv420p -vcodec mjpeg -i /dev/video0 http://localhost/webcam.ffm
        ffmpeg version 1.0.1 Copyright (c) 2000-2012 the FFmpeg developers
        built on Jan 19 2013 17:57:01 with gcc 4.6 (Debian 4.6.3-12+rpi1)
        configuration:
        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
        [video4linux2,v4l2 @ 0x21036e0] [3]Capabilities: 4000001
        [video4linux2,v4l2 @ 0x21036e0] The v4l2 frame is 42756 bytes, but 307200 bytes are expected
        [video4linux2,v4l2 @ 0x21036e0] Could not find codec parameters for stream 0 (Video: mjpeg, 640x480, 73728 kb/s): unspecified pixel format
        Consider increasing the value for the 'analyzeduration' and 'probesize' options
        [video4linux2,v4l2 @ 0x21036e0] Estimating duration from bitrate, this may be inaccurate
        /dev/video0: could not find codec parameters

        So it still claims that the pixel format is unspecified, even if I provide one. I’m wondering if it’s a matter of finding the correct pixel format, which is the information I’m trying to glean from v4l2-ctl or published tech specs of the camera. I’ll keep poking at it and I’ll update if I find anything new.

        • Try pixel format of mjpg

          • Well. This is a classic case of PEBKAC! I finally noticed that I had an earlier version of ffmpeg, 1.0.1. I removed and grabbed the latest source and now I am happy to report that it works no problem! Just need to work on the fps now…


            # ffmpeg -v verbose -r 30 -s 640x480 -f v4l2 -vcodec mjpeg -i /dev/video0 -vcodec copy test.mjpg
            ffmpeg version N-49161-g4e69237 Copyright (c) 2000-2013 the FFmpeg developers
            built on Jan 23 2013 03:16:55 with gcc 4.6 (Debian 4.6.3-14+rpi1)
            configuration:
            libavutil 52. 15.100 / 52. 15.100
            libavcodec 54. 90.100 / 54. 90.100
            libavformat 54. 61.103 / 54. 61.103
            libavdevice 54. 3.102 / 54. 3.102
            libavfilter 3. 33.100 / 3. 33.100
            libswscale 2. 2.100 / 2. 2.100
            libswresample 0. 17.102 / 0. 17.102
            [video4linux2,v4l2 @ 0x2d7d3c0] fd:3 capabilities:4000001
            [video4linux2,v4l2 @ 0x2d7d3c0] Estimating duration from bitrate, this may be inaccurate
            Input #0, video4linux2,v4l2, from '/dev/video0':
            Duration: N/A, start: 199896.120767, bitrate: N/A
            Stream #0:0: Video: mjpeg, yuvj422p, 640x480, -5 kb/s, 30 fps, 7.50 tbr, 1000k tbn, 1000k tbc
            Output #0, mjpeg, to 'test.mjpg':
            Metadata:
            encoder : Lavf54.61.103
            Stream #0:0: Video: mjpeg, yuvj422p, 640x480, q=2-31, -5 kb/s, 30 fps, 90k tbn, 1000k tbc
            Stream mapping:
            Stream #0:0 -> #0:0 (copy)
            Press [q] to stop, [?] for help
            frame= 149 fps=5.7 q=-1.0 Lsize= 5843kB time=00:00:29.80 bitrate=1605.7kbits/s

          • Good to see that you got it going πŸ™‚ what’s the plan with the Pi now ?

          • I’m planning to use it as a crib cam for my soon to be arriving daughter πŸ™‚

            I’ve made an app for our phones to access the mjpeg stream and I’m also thinking of dumping the camera’s raw stream to my file server so I can encode and archive for historical purposes.

            Your how to saved me a lot of time trying to figure out the streaming process though, so thanks again!

          • Haha nice. Exactly the reason why I set up my Pi to stream πŸ™‚ except mine was cam only as I already had a separate baby monitor for the audio.

            Glad my tutorials helped πŸ™‚

  2. Hello
    something I do wrong

    I’ve compiled ffmeg as described here.

    Now if I
    ffserver-f / etc / ff.conf & ffmpeg-quiet-v r 5-s 320×240-f video4linux2-i / dev/video0 http://localhost/webcam.ffm

    views coming
    ffserver version N-49389-g9d602a0 Copyright (c) 2000-2013 the FFmpeg developers
    built on Jan 27 2013 23:44:12 with gcc 4.6 (Debian 4.6.3-14 + rpi1)
    configuration:
    libavutil 52nd 17.100 / 52. 17,100
    libavcodec 54th 91.100 / 54. 91,100
    libavformat 54th 61.104 / 54. 61,104
    libavdevice 54th 3.102 / 54. 3102
    libavfilter third 34.101 / 3. 34,101
    libswscale second 2.100 / 2. 2100
    libswresample 0th 17.102 / 0. 17,102
    raspberrypi root @: ~ #

    then I give in Firefox

    http://192.168.151.49:8081/webcam.ffm

    a
    and there is a window with “Open with Webcam.ffm”

    I get no video out …
    I want to use ffmpeg because I want to transfer audio.

    Who can help me to advise what I’m doing wrong?

    Greeting Steffen

  3. sit is this method works in raspbian wheezy…..?????
    and wen i followed your part one tutorial i got this error

    libavcodec/libavcodec.a(allcodecs.o): In function `avcodec_register_all’:
    /home/pi/ffmpeg-dmo-1.0.3/libavcodec/allcodecs.c:506: undefined reference to `ff_mmvideo_decoder’
    collect2: ld returned 1 exit status
    make: *** [ffmpeg_g] Error 1

    im new to to this can you please tell me why this is happening….?????

  4. Hi i followed your tutorial and connected a web cam .and it shows
    $ v4l2-ctl –list-formats
    ioctl: VIDIOC_ENUM_FMT
    Index : 0
    Type : Video Capture
    Pixel Format: ‘YUYV’
    Name : YUV 4:2:2 (YUYV)
    $v4l2-ctl –all
    Driver Info (not using libv4l2):
    Driver name : uvcvideo
    Card type : iBall Face2Face Webcam C12.0
    Bus info : usb-bcm2708_usb-1.3
    Driver version: 3.2.27
    Capabilities : 0x04000001
    Video Capture
    Streaming
    Format Video Capture:
    Width/Height : 640/480
    Pixel Format : ‘YUYV’
    Field : None
    Bytes per Line: 1280
    Size Image : 614400
    Colorspace : SRGB
    Crop Capability Video Capture:
    Bounds : Left 0, Top 0, Width 640, Height 480
    Default : Left 0, Top 0, Width 640, Height 480
    Pixel Aspect: 1/1
    Video input : 0 (Camera 1: ok)
    Streaming Parameters Video Capture:
    Capabilities : timeperframe
    Frames per second: 25.000 (25/1)
    Read buffers : 0
    and when i give ./webcam.sh i got the output as

    root@raspberrypi:/usr/sbin# ./webcam.sh
    ffserver version N-49805-gfd6a021 Copyright (c) 2000-2013 the FFmpeg developers
    built on Feb 11 2013 19:21:47 with gcc 4.6 (Debian 4.6.3-12+rpi1)
    configuration:
    libavutil 52. 17.101 / 52. 17.101
    libavcodec 54. 91.102 / 54. 91.102
    libavformat 54. 62.100 / 54. 62.100
    libavdevice 54. 3.103 / 54. 3.103
    libavfilter 3. 37.101 / 3. 37.101
    libswscale 2. 2.100 / 2. 2.100
    libswresample 0. 17.102 / 0. 17.102
    ffmpeg version N-49805-gfd6a021 Copyright (c) 2000-2013 the FFmpeg developers
    built on Feb 11 2013 19:21:47 with gcc 4.6 (Debian 4.6.3-12+rpi1)
    configuration:
    libavutil 52. 17.101 / 52. 17.101
    libavcodec 54. 91.102 / 54. 91.102
    libavformat 54. 62.100 / 54. 62.100
    libavdevice 54. 3.103 / 54. 3.103
    libavfilter 3. 37.101 / 3. 37.101
    libswscale 2. 2.100 / 2. 2.100
    libswresample 0. 17.102 / 0. 17.102
    [video4linux2,v4l2 @ 0x2d6b360] fd:3 capabilities:4000001
    [video4linux2,v4l2 @ 0x2d6b360] Estimating duration from bitrate, this may be inaccurate
    Input #0, video4linux2,v4l2, from ‘/dev/video0′:
    Duration: N/A, start: 264.130817, bitrate: 6144 kb/s
    Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 320×240, 6144 kb/s, 5 fps, 5 tbr, 1000k tbn, 1000k tbc
    [graph 0 input from stream 0:0 @ 0x2d70320] w:320 h:240 pixfmt:yuyv422 tb:1/5 fr:5/1 sar:0/1 sws_param:flags=2
    [scaler for output stream 0:0 @ 0x2d70630] w:320 h:240 flags:’0x4’ interl:0
    [graph 0 input from stream 0:0 @ 0x2d70320] TB:0.200000 FRAME_RATE:5.000000 SAMPLE_RATE:nan
    [scaler for output stream 0:0 @ 0x2d70630] w:320 h:240 fmt:yuyv422 sar:0/1 -> w:320 h:240 fmt:yuvj422p sar:0/1 flags:0x4
    Output #0, ffm, to ‘http://localhost/webcam.ffm’:
    Metadata:
    creation_time : now
    encoder : Lavf54.62.100
    Stream #0:0: Video: mjpeg, yuvj422p, 320×240, q=1-10, 2000 kb/s, 1000k tbn,5 tbc
    Stream mapping:
    Stream #0:0 -> #0:0 (rawvideo -> mjpeg)
    Press [q] to stop, [?] for help
    frame= 5 fps=0.0 q=1.6 size= 28kB time=00:00:01.00 bitrate= 229.4kbits/sframe= 10 fps=8.8 q=1.6 size= 52kB time=00:00:02.00 bitrate= 213.0kbits/sframe= 17 fps= 10 q=1.6 size= 80kB time=00:00:03.40 bitrate= 192.8kbits/sframe= 21 fps=9.8 q=1.6 size= 100kB time=00:00:04.20 bitrate= 195.0kbits/sframe= 22 fps=8.3 q=1.6 size= 104kB time=00:00:04.40 bitrate= 193.6kbits/sframe= 23 fps=7.3 q=1.6 size= 108kB time=00:00:04.60 bitrate= 192.3kbits/s
    *******************************************************************************************************************************can you please tell me how will i be able to convert this raw format to viewable format ….i already installed vlc …and i want to see this video from another network connected terminal …

    Thank You

  5. […] How To : Stream A Webcam From The Raspberry Pi Part 3 […]

  6. hi,
    i got the stream running with this page3-tutorial, now i am just testing to optimze the video-quality.
    1) is there a way to manually stop the stream once it is running (other then a reboot)?
    2) can someone post a command to include the audio stream (i have a logitech c300 – how do i select the audiosource)?
    so far i am happy i got this running as a complete linux and ffmpeg noob – great work!

    • Yes, killall ffserver which will also stop ffmpeg.
      You will need to compile in audio support if you want audio.
      The package libasound2-dev needs to be installed when you compile ffmpeg for audio, and then the parameters for audio need to be added to the script.
      I’ll update this comment with the specific audio parameters that you need when I get home πŸ™‚

      Thanks for reading the tutorial πŸ™‚ Hope it helps πŸ™‚


      • i installed the libasound2-dev package before compiling…
        the parameters would be great, since i am a complete noob and certainly miss the keywords to google for.
        “killall ffserver” sounds good!
        thank you for helping! dont be in a hurry, we still got ~8 weeks to get the babycam running πŸ™‚

      • “I’ll update this comment with the specific audio parameters that you need when I get home :)”

        i hope you got home, Sir! πŸ™‚

        i tried a thousand combinations of parameters but i can only get video or sound, not the two together.
        what format&codec should the stream be?
        i need a lil push in the right direction or i keep running around in circles ..

        • Haha, sorry about the delay mate.

          Been a bit flat out with a few other things, but I’ll get back to you as soon as I can πŸ™‚

  7. Awesome write up. I’m using this info to do the exact same thing on a Beagleboard-xM. I’m just not too sure how I could view the stream on a Windows PC once I start ffserver and ffmpeg. What would I input into VLC/Firefox/Chrome?

    Thanks!

    • Glad it helped.

      Accessing the stream is as simple as going to http://ipaddress/webcam.mjpeg replacing ipaddress with the devices IP address.

      • SirLagz,

        I appreciate the quick response. I did in fact try that, but VLC just sort of hangs and Chrome attempts to download “webcam.mjpeg” for some odd reason. I checked the stat.html and the state of the connection stays at “HTTP/1.1 WAIT_FEED”.

        After doing some research I found out that this appears to be some sort of bug in some cases. Changing “FileMaxSize 10M” to “FileMaxSize 10G” was supposed to be the fix for this issue, but I tried that and I didn’t seem to work. Do you have any thoughts on this matter?

        Thanks!

        • I seem to recall having similar issues…but I don’t recall having a fix for it unfortunately.
          I’ll have a play around with ffmpeg on my Pi tonight and get back to you on that one πŸ™‚

  8. That sounds great! I can’t thank you enough for the help!

  9. NagNag πŸ™‚
    i am still lost … tried every tutorial on the web so far and about every sample from ffmpeg page – no success.
    (my video runs or only my audio runs – i have no clue which codecs/formats/combinations i should try, i am fishing in the dark)
    any examples / hints / links / tipps on how to stream video AND audio at once would be highly appreciated!!!

    • So sorry !!! Been caught up in a whole bunch of things and forgot. Definitely get to it in the next 48hrs

      • thx for the update!! .. though, same here: cant get it run on vlc .. at least i know now i didnt do sth basically wrong .. and got some new ideas .. will keep trying and report on success

  10. […] has a microphone integrated which the Pi can use to record audio. Everything has been setup as per part 3 of my ffmpeg streaming […]

  11. When I attempted to compile there were numerous errors but it continued to run and I left it overnight to finish. when trying the script or dpkg -s ffserver it tells me it’s not installed. Should i attempt to recompile the file? how should I make sure there are not any leftover files that can screw things up?

  12. Hello-
    How can we set it up so we stream h264 from the webcam module. I can see people stream in 720p with no problem – they are using HLS with segment streaming and some crazy scripts. How can we use ffserver to stream from the webcam module in pure h264 to watch inside the HTML5 web browser? Thanks

    • Webcam Module ? Do you mean USB Webcam ?
      I don’t know how people stream in 720p to be honest, I’ve never been able to get my webcam to stream properly above 640×480.
      Do you have an example ?

  13. First of, thank you for this guide, really helpful. I am hoping to get a higher frame rate from this than with the motion.

    However I got into a bit of problem.

    root@raspberrypi:/usr/sbin# ffmpeg -c /etc/ff.conf -v verbose -r 5 -s 640×480 -f video4linux2 -i /dev/video0 temp.avi
    ffmpeg version N-53439-g5f44f47 Copyright (c) 2000-2013 the FFmpeg developers
    built on Dec 30 2012 20:54:27 with gcc 4.6 (Debian 4.6.3-12+rpi1)
    configuration:
    libavutil 52. 34.100 / 52. 34.100
    libavcodec 55. 12.100 / 55. 12.100
    libavformat 55. 7.100 / 55. 7.100
    libavdevice 55. 1.101 / 55. 1.101
    libavfilter 3. 70.100 / 3. 70.100
    libswscale 2. 3.100 / 2. 3.100
    libswresample 0. 17.102 / 0. 17.102
    [video4linux2,v4l2 @ 0x1a21eb0] fd:3 capabilities:4000001
    /dev/video0: Input/output error

  14. Forgot to say.. I’ve got a Logitech C270 – Bus 001 Device 005: ID 046d:0825 Logitech, Inc. Webcam C270
    I power it with an external USB-hub and it worked when I tried it using motion.

  15. Hi. I am tring to follow this toturial with a loghitec C170 , wen I run the script the cam light goes on but when try to open in my deskto to thigs happen

    Browser
    if i use port 80 i get “This feed is already being received.”

    if I use a port other than 80 the browser starts to download webcam.ffm

    in VLC
    port 80
    cant conect

    other port

    no error but no steams open

    I would really apreciate some help

  16. On a raspberry pi, I’m using the command:

    ffserver -f /etc/ffserver.conf & ffmpeg -v verbose -r 5 -s 640×480 -f video4linux2 -i /dev/video0 http://localhost:8090/webcam.ffm

    VLC is displaying the stream via:

    http://192.168.1.106:8090/webcam.mjpeg

    There is noticeable lag and the R-pi has the cpu at 95%.

    Is ffserver no better than motion for pushing the cpu?

    Any help is appreciated,

    J

    • If your webcam can output mjpeg directly, then use -vcodec mjpeg as well to lower the CPU usage.
      otherwise ffmpeg will take RAW and encode it to mjpeg which chews up CPU.

  17. Hello sir,
    I’m using
    f f mp e g – f video4linux2 – r 2 5 – s 6 4 0 x 4 8 0 – i / d e v / v i d e o o u t . a v i
    Command to record video from webcam using raspberry pi.
    I can record video but the video quality is very poor. How to increase the frame rate of the video being recorded? Also I need to specify how much time the video must be recorded.
    Pls help me with this problem.

    • You may have issues getting much out of using ffmpeg with the Pi. You’d be better off using the raspcam if you want good quality

  18. Thank you for the suggestion.
    Is it possible to make 2 programs run simultaneously upon booting the raspberry pi? I want to record video as well as read gps data and display on lcd.
    I have planned to write 2 separate codes and make it run automatically (stand alone).

  19. […] seen a number of similar tutorials such as this one. They all say I need to compile it because the default version has “streaming […]

  20. […] seen a number of similar tutorials such as this one. They all say I need to compile it because the default version has “streaming […]

Leave a Reply to shashank Cancel reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.