Sunday, May 10, 2015

HOWTO - Stream live video from Raspberry Pi via Youtube.


A friend asked me one day to put up some surveillance camera's around his house.  He wanted to be able to access the camera's from everywhere and the streaming quality needed to be superior. 
There was one constraint however, the budget on which this needed to be realized was rather limited.
When making inventory of the materials that were readily available to us we noted a Synology diskstation, a Raspberry Pi, and a Raspberry camera module.
The first project that we realised with this bill of material, was to set-up the Raspberry Pi with an RTSP streaming server that was usable as IP Camera, which we could connect to with our Surveillance program on the Synology nas.   The constraint however was that the CPU power on the nas was rather limited since it was only a small model nas and rather old.  Each time we approached the nas over the internet with the native surveillance app we needed to wait a long time for the app to connect and retrieve our Raspberry video stream.   Now, we didn't have the budget for a more powerfull nas, so we were appointed to find another creative solution for our problem that didn't include the expenditure of any money.  After searching for a while we stumbled onto the fact that u can stream live video to youtube these days, this would provide a solution for the slow streaming and also provided us with the youtube application which was readily available on our mobile devices.
After a lot of digging onto the internet we came up with below procedure to get this worked out on our Raspberry Pi.

To get to a working solution with this, we need to stream video from the Raspberry Pi with the raspivid executable, pipe this through ffmpeg for transcoding, and then send it to youtube for live capturing and broadcasting.

After some experimenting with the ffmpeg version in the Rasbian repo's it became apparent that we could not get decent results, the bitrate was not ok, and youtube was complaining that we had a too slow bitstream each time we tried sending it our feed,   No matter what we tried.

The trick in getting this to work was to compile the latest version of ffmpeg from source.
we followed this guide to do this: https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu


  • set up build environment
sudo apt-get update
sudo apt-get -y --force-yes install autoconf automake build-essential libass-dev libfreetype6-dev libgpac-dev \
  libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev \
  libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev
mkdir ~/ffmpeg_sources
  • compile yasm from source
cd ~/ffmpeg_sources
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make
make install
make distclean
  • compile libx264 from source
cd ~/ffmpeg_sources
wget http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
tar xjvf last_x264.tar.bz2
cd x264-snapshot*
PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static
PATH="$HOME/bin:$PATH" make
make install
make distclean


  • compile libx265 from source
sudo apt-get install cmake mercurial
cd ~/ffmpeg_sources
hg clone https://bitbucket.org/multicoreware/x265

note: before compiling we needed to install a fix, since a certain function was not available on the armv7 platform:
cd ~/ffmpeg_sources/x265
wget https://github.com/archlinuxarm/PKGBUILDs/blob/master/extra/x265/arm.patch
patch -p1 -i ./arm.patch

cd ~/ffmpeg_sources/x265/build/linux
PATH="$HOME/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
make
make install
make distclean

compile libfdk-aac from source


sudo apt-get install unzip

cd ~/ffmpeg_sources
wget -O fdk-aac.zip https://github.com/mstorsjo/fdk-aac/zipball/master
unzip fdk-aac.zip
cd mstorsjo-fdk-aac*
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
make distclean
  • compile libmp3lame from source
sudo apt-get install nasm
cd ~/ffmpeg_sources
wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
tar xzvf lame-3.99.5.tar.gz
cd lame-3.99.5
./configure --prefix="$HOME/ffmpeg_build" --enable-nasm --disable-shared
make
make install
make distclean
  • compile libopus from source
cd ~/ffmpeg_sources
wget http://downloads.xiph.org/releases/opus/opus-1.1.tar.gz
tar xzvf opus-1.1.tar.gz
cd opus-1.1
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
make distclean
  • compile libpvx from source
cd ~/ffmpeg_sources
wget http://webm.googlecode.com/files/libvpx-v1.3.0.tar.bz2
tar xjvf libvpx-v1.3.0.tar.bz2
cd libvpx-v1.3.0
PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --disable-examples --disable-unit-tests
PATH="$HOME/bin:$PATH" make
make install
make clean


  • compile ffmpeg from source

cd ~/ffmpeg_sources
wget http://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
tar xjvf ffmpeg-snapshot.tar.bz2
cd 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-libfdk-aac \
  --enable-libfreetype \
  --enable-libmp3lame \
  --enable-libopus \
  --enable-libtheora \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nonfree
PATH="$HOME/bin:$PATH" make
make install
make distclean
hash -r

After this guide you have ffmpeg compiled and ready for transcoding with the Raspberry Pi.
Now it's time to set up your live video stream on Youtube and let youtube know that u will be sending a video stream to them.
You need to open the youtube web page, go to your channel, and then go to your video management to create a live event.
When creating the live event on you tube you can choose for a custom encoding, and you will be able to select your speed.

Once you have your live event set-up you can go to your live control room and start sending youtube your video feed.




raspivid -o - -t 0 -w 1280 -h 720 -fps 25 -b 600000 -g 50 | ./ffmpeg -re -ar
44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i -
-vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv
rtmp://a.rtmp.youtube.com/live2/XXXXXXX

No comments:

Post a Comment