Stream a webcam to Twitch over a Raspberry Pi

Achtung! Dieser Artikel ist älter als ein Jahr. Der Inhalt ist möglicherweise nicht mehr aktuell!

I wanted to stream a webcam connected to my headless Raspberry Pi (also running OctoPrint) to Twitch. The problem: The encoding in h264 is a ressource hog. But thankfully somebody reverse engineered the hardware acceleration and made it open source.
You can stream directly to Twitch using ffmpeg. But you habe to compile it with the ONX encoder.

Hardware used is a Raspberry Pi 3 running a headless Raspbian and a Logitech C270 webcam. The webcam only has 8-10 FPS at 1920x960 pixels. But that’s enough for me at the moment.

Install dependencies

Install the needed software (as root):

apt-get install autoconf automake build-essential libass-dev libfreetype6-dev libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texinfo zlib1g-dev libomxil-bellagio-dev git

Then clone the ffmpeg repo:

git clone --depth 1

Change into that directory and let the configure script running:

./configure --enable-gpl --enable-nonfree --enable-mmal --enable-omx --enable-omx-rpi

Then compile it. This takes roughly 10 - 15 minutes:

make -j4

After successfully compiling it, there should be an ffmpeg binary in the current directory. Check if the OpenMAX encoder is present:

veloc1ty@raspberrypi:~$ ./ffmpeg -encoders | grep h264_omx
ffmpeg version git-2018-10-27-59a35fe Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 6.3.0 (Raspbian 6.3.0-18+rpi1+deb9u1) 20170516
  configuration: --enable-gpl --enable-nonfree --enable-mmal --enable-omx --enable-omx-rpi
  libavutil      56. 20.100 / 56. 20.100
  libavcodec     58. 34.100 / 58. 34.100
  libavformat    58. 19.102 / 58. 19.102
  libavdevice    58.  4.106 / 58.  4.106
  libavfilter     7. 38.100 /  7. 38.100
  libswscale      5.  2.100 /  5.  2.100
  libswresample   3.  2.100 /  3.  2.100
  libpostproc    55.  2.100 / 55.  2.100
 V..... h264_omx             OpenMAX IL H.264 video encoder (codec h264)

If your output matches roughly the one above everythign should be okay. Now you can stream with this binary.

Stream with ffmpeg

Be sure to use the compiled binary and replace yourlivekey with your Twitch key.

./ffmpeg -f video4linux2 -s 1280x960 -framerate 24 -i /dev/video0 -c:v h264_omx -threads 0 -an -f flv "rtmp://"

The load should be pretty low with the hardware encoder. I’ve roughly 10% load on this machine now. Without the compiled binary (so using the packaged one) my load is really high.

Hint: Audio streaming is disabled with the “-ar” flag.


Source 1:

Source 2:

Du hast einen Kommentar, einen Wunsch oder eine Verbesserung? Schreib mir doch eine E-Mail! Die Infos dazu stehen hier.

🖇️ = Link zu anderer Webseite
🔐 = Webseite nutzt HTTPS (verschlüsselter Transportweg)