Twitch sheep

· berru's blog

A recipe to cook a twitch live stream of your flock of sheeps

Table of Contents

In this recipe we will cook a delicious live stream of your flock of sheep. It will be availaible here on twitch

If you really want to, you might use it to stream something else out of your webcam. Be careful though, this recipe was meant to stream sheeps.

Ingredients #

For this recipe you will need:

Assembly #

Put everything in the box ! Plug the computer, power chord, ethernet cables and PLC device accordingly. If your setup involves an external USB webcam, you can make a hole so it sits outside the box.

Preparation #

Now you're all setup, let's cook !

Cooking #

Cooking a systemd unit file #

It will tell the computer how to start and restart your streaming program. Then you'll be able to start and stop it, and have it run automagically at boot.

The file should look like this:

 1[Unit]
 2Description=Twitch sheep streaming
 3After=network.target
 4
 5[Service]
 6Type=simple
 7User=root
 8Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
 9ExecStart=<YOUR_STREAMING_PROGRAM_HERE>
10Restart=always
11RestartSec=3
12
13[Install]
14WantedBy=multi-user.target

We will complete the <YOUR_STREAMING_PROGRAM_HERE> in a moment.

Feel free to change the description if you like. In this setup, the program will be restarted after a 3 seconds delay if it crashes. It's run as root for no good reason, and after the computer as finished booting (multi-user.target) and is online (network.target).

Cooking the streaming program #

To cook the stream, we'll use ffmpeg.

Testing #

Your stream_key should look like this:

rtmp://live.twitch.tv/app/live_xxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

you can append ?bandwidthtest=true so when you test your program, your followers don't get notified. You can then check if everything is working fine using the twitch inspector. It should look like this.

rtmp://live.twitch.tv/app/live_xxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?bandwidthtest=true

Don't forget to remove it when you're done !

ffmpeg stream #

The magic command is :

1/usr/bin/ffmpeg -f video4linux2 -input_format h264 -video_size 1920x1080 -i /dev/video0 -c:v libx264 -preset ultrafast -pix_fmt yuv420p -s 1920x1080 -tune zerolatency -threads 0 -b:v 2M -maxrate 2M -bufsize 1M -f flv rtmp://live.twitch.tv/app/live_xxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Run it and see if it works ! Now let's dive into the many parameters. Beware, Their order is important !

input_config #

For this step you'll need video4linux2 which is provided by v4lw-utils on my distribution.

Get the resolutions and image formats your webcam supports by using ffmpeg -f v4l2 -list_formats all -i /dev/video0. As an example, I get:

[video4linux2,v4l2 @ 0x563b256b2bc0] Compressed:       mjpeg :          Motion-JPEG : 3840x2160 2560x1440 1920x1080 1280x720 1024x576 960x540 800x600 640x480 640x360 480x270 352x288 320x240
[video4linux2,v4l2 @ 0x563b256b2bc0] Compressed:        h264 :                H.264 : 3840x2160 2560x1440 1920x1080 1280x720 1024x576 960x540 800x600 640x480 640x360 480x270 352x288 320x240
[video4linux2,v4l2 @ 0x563b256b2bc0] Raw       :     yuyv422 :           YUYV 4:2:2 : 640x480 640x360 480x270 352x288 320x240 320x180 160x120

I chose to use h264 in 1920x1080.

network_speed #

My first attempts had the stream freeze every few seconds on twitch. Turns out my PLC plugs don't handle very well being separated by several 50m chords and the connectivity isn't that good. So I decided to test the speed between my streaming laptop, and my work computer.

The output looks like this:

[ ID] Interval           Transfer     Bitrate
[  5]   0.00-11.90  sec  1.00 MBytes   2.5 Mbits/sec

If you don't have a way to run iperf as a server on another computer, you can pick one from this public list.

Finishing #

You're mostly done ! Then final file in /etc/systemd/system/twitch_webcam.service should look like this, with values adapted accordingly :

 1[Unit]
 2Description=Twitch sheep streaming
 3After=network.target
 4
 5[Service]
 6Type=simple
 7User=root
 8Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
 9ExecStart=/usr/bin/ffmpeg -f video4linux2 -input_format h264 -video_size 1920x1080 -i /dev/video0 -c:v libx264 -preset ultrafast -pix_fmt yuv420p -s 1920x1080 -tune zerolatency -threads 0 -b:v 2M -maxrate 2M -bufsize 1M -f flv rtmp://live.twitch.tv/app/live_xxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
10Restart=always
11RestartSec=3
12
13[Install]
14WantedBy=multi-user.target

You can have this file detected by systemd by running : systemctl darmon-reload.

Serving #

Start you fantastic twitch channel immediatly by running systemctl start twitch_webcam.service and make it start automagically when the computer starts with systemctl enable twitch_webcam.service.

Taste it ! #

Wanna see live sheeps in Normandy ? Or mostly grass if they're sleeping away from the camera ? Well here you go !

last updated: