Raspberry Pi Video Streaming

From philcrump.co.uk
Jump to: navigation, search

PiCam H264 -> RTMP Server

This example streams a Video-only H.264 720x405 25fps 2Mbps Stream to an RTMP server (such as batc.tv)

raspivid -n -w 720 -h 405 -fps 25 -t 1000000 -b 2000000 -ih -o - | ./bin/ffmpeg -i - -vcodec copy -an -f flv rtmp://batc.tv/live/<streamid>/<streamid>

Pi USB Video Capture -> RTMP Server

This example captures video from a V4L2 device at /dev/video0 at 640x480, runs it through a denoising filter with factor=15, and then encodes the Video as H.264 and streams it to batc.tv

The HQDN3D denoising filter is the key to achieving a decent quality encoded stream with the limited processing power available. It's a lowpass filter that takes a factor of 0-255 where 0 is no effect, and 255 will leave nothing left. You can read more about the filter here: avisynth.nl/index.php/Hqdn3d

A Pi2 (quad-core 1GHz) is required for reliable realtime encoding at 25fps.

ffmpeg -f v4l2 -video_size 640x480 -i /dev/video0 -vf hqdn3d=15 -c:v libx264 -b:v 500k -maxrate 500k -bufsize 2048k  -preset veryfast -an -f flv rtmp://batc.tv/live/<streamid>/<streamid>

sidenote: Use of a denoising filter is the reason why VP6 outperforms H.264 when streaming noisy pictures in Flash Media Live Encoder. Their VP6 implementation uses a denoising filter before the encoding, throwing away a lot of the high-frequency information, the H.264 does not.

Querying available resolutions from a V4L2 Device

Useful for when working with non-standard devices, useful commands are below.

v4l2-ctl -n
v4l2-ctl -i <x>
ffmpeg -f v4l2 -list_formats all -i /dev/video0

Building FFMPEG on the Pi

The following script compiles and installs ffmpeg with just the x264 library. Instructions for installing HEVC, MP3, Opus and other optional libraries can be found in the source article: trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

Compile Script: ffmpeg-compile-rpi.sh

Script Source
sudo apt-get update && sudo apt-get -y dist-upgrade
sudo apt-get -y --force-yes install autoconf automake build-essential libasound2-dev libass-dev libfreetype6-dev libtool pkg-config texinfo zlib1g-dev
mkdir /home/pi/ffmpeg_sources
cd /home/pi/ffmpeg_sources/
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar -xf yasm-1.3.0.tar.gz 
cd /home/pi/ffmpeg_sources/yasm-1.3.0/
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make install
make distclean
cd /home/pi/ffmpeg_sources/
wget http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
tar -xf last_x264.tar.bz2 
cd /home/pi/ffmpeg_sources/x264-snapshot-*/
PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static
PATH="$HOME/bin:$PATH" make -j4
make install
make distclean
cd /home/pi/ffmpeg_sources/
wget http://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
tar -xf ffmpeg-snapshot.tar.bz2 
cd /home/pi/ffmpeg_sources/ffmpeg/
PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --pkg-config-flags="--static" --extra-cflags="-I$HOME/ffmpeg_build/include" --extra-ldflags="-L$HOME/ffmpeg_build/lib" --bindir="$HOME/bin" --enable-gpl --enable-libass --enable-libfreetype --enable-libx264 --enable-nonfree
PATH="$HOME/bin:$PATH" make -j4
make install
make distclean
hash -r
cd /home/pi/
rm -rf ffmpeg_sources/

Raspberry Pi Service File

# systemd service file for '/home/pi/stream.sh'
# To Install:
# * Copy this file to /etc/systemd/system/stream.service
# * Then 'systemctl daemon-reload' to reload systemd config files
# * Then 'systemctl enable stream' to enable auto-start on boot
# To Start: systemctl start stream
# To Restart: systemctl restart stream
# To Stop: systemctl stop stream
Description=BATC Stream