Stream the Raspberry Pi Cam to Twitch

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

After getting bad results with my shitty webcam (< 10 fps) I’ve upgraded to a Raspberry Pi Cam v2 without integrated infrared filter (NoIR). The Raspberry Pi Cam v2 can output 1080p30 (and better) in a really nice quality. By turning the lense you are also able to adjust the focus easily. But the best part is the native h264 output. Twitch accepts that as input stream so we don’t have to transcode which is very ressource intensive. Currently my Pi 3 has a load of 0.1.

Using the application raspivid and ffmpeg (You maybe have to install it via apt) streaming the cam is very easy.

The command:

raspivid -n -t 0 -w 1920 -h 1080 -b 3500000 -fps 30 -o - | ffmpeg -re -i - -input_format h264 -vcodec copy -an -f flv "rtmp://"


There are two commands executed: Raspivid captures the video and pipes it into ffmpeg.

Raspivid arguments:

ffmpeg arguments:

You should see the follwing output:

Input #0, h264, from 'pipe:':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: h264 (High), yuv420p(progressive), 1920x1080, 25 fps, 25 tbr, 1200k tbn, 50 tbc
Output #0, flv, to 'rtmp://':
    encoder         : Lavf57.56.101
    Stream #0:0: Video: h264 (High) ([7][0][0][0] / 0x0007), yuv420p(progressive), 1920x1080, q=2-31, 25 fps, 25 tbr, 1k tbn, 1200k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
[flv @ 0x23be140] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly
frame=32324 fps= 30 q=-1.0 size=  460976kB time=00:21:32.92 bitrate=2920.8kbits/s speed= 1.2x

The important part is

Stream mapping:
  Stream #0:0 -> #0:0 (copy)

This indicates that the video stream is copied and not transcoded.

Some background foo (optional)

Using raspivid // Twitch bitrate

I first tried not to use raspivid, because ffmpeg could read the camera directly from /dev/video0 with proper kernel module loaded. Since the camera outputs directly h264 I tried to stream it directly to Twitch using this command:

 ffmpeg -i /dev/video0 -input_format h264 -vcodec copy -an -f flv "rtmp://"

The command is nearly the same as the one I use now. The “negative” side was the bitrate. The bitrate would exceed 25 MBit/s. My upload speed can handle it without problems, but Twitch only allows 3,5 MBit/s as a non affiliate streamer. I tried limiting the bitrate with -b:v 2M -maxrate 2M -bufsize 1M, but sinde ffmpeg only copies the stream it had no effect. I would have to do a transcoding to take effect but that would cause unwanted CPU load.
I then discovered raspivid. You are able to set the bitrate there but I actually don’t know how it works.

Choosing wich version of the Raspberry Cam to use (IR vs. NoIR)

The Raspberry Cam v2 comes in two versions: One with integrated IR (infrared) filter and one without (NoIR). For daylight video I would use the version with fixed IR filter.
Since prints are maybe taking up several houres without a light source I decided to get the version without integrated IR filter. I can light up my print with infrared light and the cam can caputure it. That’s perfectly for night videos.

Further reading

Stearming guide from ffmpeg:

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)