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:
- a spare laptop or computer (a raspberry pi works as well), I use a thinkpad R61
- an internet connection of some sort (if wifi isn't available, a PLC might be useful)
- (optionnal) an external usb webcam. I use a Innex C470 Camera
- a mostly waterproof box
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 #
- Install your favorite arch distribution (i use arch, btw)
- Remove any unecessary graphical packages such as wayland and Xorg
- Ensure network connectivity and the webcam is recognized. On my setup it's in
/dev/video0
- Register on twitch and get your
stream_key
from the menuStream Key & Preferences
. The url looks likehttps://dashboard.twitch.tv/u/<YOURNICKNAME>/settings/stream
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 !
/usr/bin/ffmpeg
runsffmpeg
-f video4linux2
tells it to use video4linux2 demuxer to read from the input file-input_format h264
in what formatffmpeg
should read the stream from your webcam (see input config)-video_size 1920x1080
what resolution to read from the webcam (see input config)-i /dev/video0
what is the webcam file on your system-c:v libx264
FIXED: the video codec used as an output of the command. Twitch expects this, so don't touch it.-preset ultrafast
Optionnal: this makesffmpeg
care for speed way more than for quality-pix_fmt yuv420p
FIXED: the video format used as an output of the command. Twitch expects this, so don't touch it.-s 1920x1080
This is the same asvideo_size
but is somehow needed ? I'm not sure, let me know if it is for you !-tune zerolatency
Optionnal: another setting to make encoding faster at the detriment of quality-threads 0
Optionnal: I want it on a single thread to avoid hogging the CPU-b:v 2M
tellsffmpeg
to limit the bitrate to 2Mb. It should be adjusted according to your network speed (see network speed)-maxrate 2M
same as above, but sets a hard cap to 2Mb instead of an average.-bufsize 1M
tellsffmpeg
to reevaluate the current bitrate every1Mb
, can be set to half ofb:v
andmaxrate
-f flv
FIXED: the video format used as an output of the command. Twitch expects this, so don't touch it.rtmp://live.twitch.tv/app/live_xxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Well, that's youstream_key
, so, you know, put yours.
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.
- Install
iperf3
on both computers.iperf3 -p 5200 -s
on your working computer to run the serveriperf3 -c <YOUR_WORKING_COMPUTER_IP> -p 5200 --format M --verbose
from the streaming laptop to run the client
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 !