Mar 102013
 

I’ve had a few queries regarding streaming audio as well as video on the Raspberry Pi, so tonight I set up my little Raspberry Pi with a Logitech C110. This webcam also has a microphone integrated which the Pi can use to record audio.
Everything has been setup as per part 3 of my ffmpeg streaming guide.

A little investigation reveals which hardware device the microphone is recognised as.

# arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: C110 [Webcam C110], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0

So the device is recognised as card 1. This comes in later when we are setting up the audio part of the streaming.

I’ve updated the webcam.sh script to reflect the fact that we are now recording sound –

ffserver -f /root/ff.conf & ffmpeg -vcodec mjpeg -v verbose -r 15 -s 176x128 -f video4linux2 -i /dev/video0 -f alsa -ac 1 -i hw:1 http://localhost:81/webcam.ffm

And I also need to update ff.conf with the new streaming settings
The Stream webcam section has now turned into this –
<Stream webcam.avi>
Feed webcam.ffm
Format avi
VideoSize 176x128
VideoFrameRate 15
VideoBufferSize 40
VideoBitRate 64
AudioBitRate 32
AudioChannels 1
AudioSampleRate 11025
VideoQMin 1
VideoQMax 20

With this configuration, I could start ffmpeg with audio and video streaming. However I could not get my laptop with vlc to connect to it for now, so I’ll have to keep investigating that.
Hopefully this helps though.


Please support the continued development of any useful scripts and tutorials that you have found useful !




Share
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
Aug 182012
 

Following on from the previous webcam posts, I decided to see if the Pi has enough grunt to stream 2 webcams at once.
The USB driver for the Pi at the moment isn’t quite up to the task at the moment, but I can get the 2 streaming at low resolutions for hours at a time at the moment.

I am still using the original ffmpeg that I compiled back from the first post.
I modified the ffserver.conf so that it would accept 2 streams of input rather than just one.

The following stanza is duplicated

<feed webcam.ffm>
file /tmp/webcam.ffm
FileMaxSize 20M
</Feed>

Like so – I added the number to the end to identify which webcam was which.

<Feed webcam0.ffm>
file /tmp/webcam0.ffm
FileMaxSize 20M
</Feed>

<Feed webcam1.ffm>
file /tmp/webcam1.ffm
FileMaxSize 20M
</Feed>

Then, the Stream stanzas are also duplicated.
These will be the addresses that are accessed to view the stream.
This is also where the bitrate, framerate and quality settings are defined.

<Stream webcam0.mjpeg>
Feed webcam0.ffm
Format mjpeg
VideoSize 320x240
VideoFrameRate 4
VideoBitRate 10000
VideoQMin 1
VideoQMax 10
</Stream>

<Stream webcam1.mjpeg>
Feed webcam1.ffm
Format mjpeg
VideoSize 320x240
VideoFrameRate 4
VideoBitRate 10000
VideoQMin 1
VideoQMax 10
</Stream>

After the ffserver.conf file is modified, ffmpeg will need to be run twice. Once for each webcam.
The command is the same, except for changing the input file at the end.

ffmpeg -loglevel info -r 4 -s 160x120 -f video4linux2 -i /dev/video0 http://localhost/webcam0.ffm
ffmpeg -loglevel info -r 4 -s 160x120 -f video4linux2 -i /dev/video1 http://localhost/webcam1.ffm

Running those 2 commands will then start streaming both webcams into ffserver, which will in turn make the streams accessible via a web browser.

Share
Aug 162012
 

This is a quarter-continuation from this post : How To : Stream A Webcam From The Raspberry Pi

I had a look at the settings that I was using to stream from my Raspberry Pi as I had it doing other things for a while, and I was using 320×240 @ 10fps. Some people were having some issues where the streaming would either not start, or would stop very quickly.
I bumped up the resolution to 640×480 and I started having the same 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 looking around, I found some utilities that I could use to glean some more information about my webcam.
v4l2-ctl could show the capabilities of the webcam itself. I installed some other v4l utilities while I was at it in case I needed them later.

apt-get install v4l-utils v4l-conf

After I installed it, I could check to see whether my webcam could stream in mjpeg format directly.

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

Alas, it seems like mine can’t. So I’ll have to find out why the frames are getting cut short.
Lowering the resolution to 160×120 helps but that’s not really an ideal solution.
I had a spare webcam to try out, and the spare webcam worked on 640×480 without any tweaking.
Seems that some webcams just can’t do what we want them to do !

For now, I have another webcam streaming 640×480 @ 7fps and it seems to be working. I’ll report back on results soon !

Share
Aug 042012
 

**Update**

An updated version of this post is –>here (Part 3) <–.

Or to stream via Motion, check out this blog post !

*****

There have been quite a few people wanting to stream a webcam from the Raspberry Pi.
If all you want is to view a webcam stream, without any motion detection, then you’ve come to the right post.
Otherwise, I’ll have a blog post up about motion later on for motion detection.

This guide should get you to the point where you can view the webcam stream from another machine via a web browser, or a media player capable of viewing mjpeg streams – e.g. VLC.

1. Compiling FFMpeg

First thing we need to do is to get a version of ffmpeg that can stream. The version that comes with the current version of Raspbian and Debian have issues streaming, so the sources from deb-multimedia are needed. All the below commands are run as root. If you are not running as root, prepend sudo to all the commands.

**Update**
If you need sound for ffmpeg, you will need to also install the libasound2-dev package which enables ALSA.
– Thanks to fbutler for this

  1. Add the following lines into /etc/apt/sources.list
    deb-src http://www.deb-multimedia.org sid main
    deb http://www.deb-multimedia.org wheezy main non-free
  2. Run apt-get update
  3. Run apt-get install deb-multimedia-keyring
  4. Remove the second line from /etc/apt/sources.list
    deb http://www.deb-multimedia.org wheezy main non-free
  5. Run apt-get source ffmpeg-dmo
  6. You should now have a folder called ffmpeg-dmo-0.11 <-- The version will change as time goes by.
  7. Change the directory to the folder containing the source. e.g. cd ffmpeg-dmo-0.11
  8. Run ./configure to setup the source.
  9. Run make && make install to compile and install ffmpeg
  10. if you are not running as root like I am, then you will need to run the above command with sudo

2. 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.

  1. 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.

  2. 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
  3. 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

3. Start Streaming

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 start seeing lines like this

** 1 dup!2 fps= 5 q=2.6 size= 51136kB time=00:06:56.40 bitrate=1006.0kbits/s dup=359 drop=0

This means that ffmpeg is now streaming, and you should be able to access the stream via the web address of http://<YOUR WEBCAM SERVER>/webcam.mjpeg

** Update of sorts – I continue my webcam experimentation here


If you have found this post useful, please consider donating by pressing on the button below. Donating will help keep this site up and running 🙂




Share