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 https://github.com/ffmpeg/FFMpeg --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)
veloc1ty@raspberrypi:~$

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://live.twitch.tv/app/yourlivekey"

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.

Sources

Source 1: https://www.reddit.com/r/raspberry_pi/comments/5677qw/hardware_accelerated_x264_encoding_with_ffmpeg/

Source 2: https://potluru.wordpress.com/2016/06/26/compile-ffmpeg-for-raspberry-pi-3/#comment-10


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)
Zurück