Streaming video from your Raspberry Pi
Posted
Friday, March 4th 2016 in
Linux, Other Tech -
Permalink
I recently got a video stream from my Raspberry Pi 2 working on my web server. I got this to work with two Logitech C270 webcams, a powered USB hub, and the humble Pi.
The first thing I did is to try to get this to work using FreeBSD 11, which had a snapshot available for the Raspberry Pi 2. After a bunch of tries, I could not get the webcam to work with it. I’m guessing that something was wrong with the USB and the cameras, so I instead installed Raspbian onto the Pi.
The first thing I had to do was install Ffmpeg. The program is not available from the package manager on Raspbian, so I had to compile it from source. There are quite a few tutorials online to show you how to do this. I wanted to be able to use the microphones on the webcams, so I had to make sure that lib-asound2 was installed before I compiled Ffmpeg.
The Raspberry Pi has a pretty weak processor, so I didn’t think I would be able to get it to encode Webm on the fly. After a bunch of trial and error, I was able to get it to send Mjpeg video over the LAN to my server, and my server would do the encoding to Webm. Here is the Ffmpeg command that I used to stream video to my server:
ffmpeg -f v4l2 -s 640x360 -r 15 -input_format rawvideo -i /dev/video0 -an -s 640x360 -r 15 -pix_fmt yuv420p -qscale:v 4 -vcodec mpeg4 -f mpegts udp://server:10111/
I streamed the sound from the microphone separately using Mp2:
ffmpeg -f alsa -ac 1 -i hw:1,0 -acodec mp2 -b:a 48k -f mp2 udp://server:10113/
On the server, I used the following command to combine a video stream and an audio stream together into a video:
ffmpeg -i udp://raspberrypi:10111/ -i udp://raspberrypi:10113/ http://localhost:8090/cam1.ffm
This command feeds the stream to Ffserver, which I also have running on my server to serve the video over HTTP. Here is an example from my configuration:
<Feed cam1.ffm>
File /tmp/feed1.ffm
FileMaxSize 500M
ACL allow 192.168.0.15 192.168.0.250
ACL allow 127.0.0.1
</Feed>
<Stream cam1.webm>
Feed cam1.ffm
Format webm
AudioCodec libvorbis
AudioBitRate 48
AudioSampleRate 22050
AVOptionAudio flags +global_header
VideoCodec libvpx
VideoSize 640x360
VideoFrameRate 15
VideoBitRate 600
AVOptionVideo cpu-used 0
AVOptionVideo qmin 10
AVOptionVideo qmax 42
AVOptionVideo quality realtime
AVOptionVideo flags +global_header
PreRoll 15
StartSendOnKey
</Stream>
First start the streams on the Pi, then start Ffserver on the server. Finally, start the Ffmpeg transcoders on the server, and you should have streaming video!
 The CPU used on the Raspberry Pi.
 The stream being read on the server.
 The CPU used on the server to convert the video stream from the Raspberry Pi into Webm for browser streaming.
|