Video streaming with a Raspberry Pi and ffmpeg

In a previous post I detailed how to set up a web-based streaming from a Raspberry Pi device. After having updated Raspbian to its latest stable version, jessie, I thought it could be a good moment for reconsidering the streaming approach I was using.

This time, since I was not taking any advantage of the RTMP protocol anyway, and I was simply serving the content via a webpage, I thought it would be easier to skip Nginx rtmp module. Moreover, hls segment generation could be taken care by ffmpeg instead of nginx itself and installing/using gstreamer was then not necessary anymore either.

The only relatively cumbersome step was compiling ffmpeg for a few hours (some binaries are around, though). avconv (ffmpeg fork) jessie version, available from default repositories, did not seem to include the needed hls options.

Below a gist of a raspivid + ffmpeg command, generating its output into a hls directory and (with a m3u8 list file at the same time):

It's worth mentioning that this script will create a video stream with a certain delay (1 min or more), but with the advantage of being very lean and easy to broadcast from a home installation. More details about hls video handling with ffmpeg can be checked here.

Since jessie uses systemd in a Debian system for the fist time, I also took this opportunity for writing my first custom unit in order to start/stop the video streaming service.

If you would prefer the camera to stop working during the night (it makes no sense unless you have a noIR model), you can simply place a cron script in /etc/cron.d as well. Example:

0 13 * * * root systemctl start hls
0 22 * * * root systemctl stop hls

And, that's all!

Reference