Categories
Cloud

Multicasting Livestreams to Twitch, Twitter and More with a VPS

I’ve had a couple of people ask me how I do my multicast to both Twitch and Twitter at the same time. If you stream to periscope currently your broadcasts appear in your twitter feed therefore for twitter you need a periscope account linked to your twitter account – probly a good idea to do this soon as Periscope is closing soon.

My solution actually applies to Facebook and Youtube also – though not instagram (for reasons I’ll explain later…). Re periscope closing – according to twitter’s docs we’ll continue to be able to use the periscope api to broadcast direct to twitter until the api is migrated to twitter – so in theory come march 31st we’re still in business…

Anyway… In summary – what I’m doing is broadcasting an RTMP stream of video and audio to a VPS I rent. Then I’m using an NGINX web server on that VPS to multiply that stream to post to more than one source. Then I have the chat feed from both services open in front of me.

Another quick note re twitter – It shows your broadcast live in your feed v but then all that is left aft’r you finish is a seemingly random 30s excerpt. So probably good to then repost another link from twitch/fb/youtube etc…

1. Generating an RTMP Stream

You can do this with a number of apps on Android or iOS or if you have a desktop device then OBS can do it. I use a raspberry pi 4 with https://github.com/iizukanao/picam installed, a standard pi camera, and a usb interface to grab the audio.

Something like this: https://play.google.com/store/apps/details?id=com.wmspanel.larix_broadcaster&hl=en should work on Android – probably there is something similar on iOS. You can use a USB audio device to input audio on android and iOS if you have a fairly modern version of the OS. So in theory you can have a mixer going into your tablet pc and balance your mic etc to record a whole band!

More detail is beyond the scope of this article or though at some point I’m likely to post set up instructions for the camera etc on a pi…

2. The VPS

I use gandi.net for vps’s ‘cos they’re a great company with a reliable service. But you can use digitalocean etc there are a ton of good providers.

Only need a half a gig of RAM and one CPU to do the job so we’re talking no more than a tenner a month. Note also you could conceivably only turn it on when you use it and pay next to nothing – it’s just one button to click in gandi’s dashbaord. I use mine for a ton of other things hence leave it turned on…

Steps are Debian because that’s what I use but probly ubuntu is almost identical and no doubt others can be adapted.

To Install NGinx:

  su
  apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
  wget http://nginx.org/download/nginx-1.9.15.tar.gz
  tar -zxvf nginx-1.16.1.tar.gz
  wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
  cd nginx-1.16.1
  unzip master.zip  

You *should* be able to type this line. However with my debian 10 I found two issues.

  ./configure –-with_http_ssl_module –-add-module=../nginx-rtmp-module-master

Firstly the with_http_ssl_module option did not work. My solution was to edit /nginx-1.16.1/auto/options and search for HTTP_SSL=NO and change it to ‘YES’ (without quotes…)

Secondly the add module option syntax was insanely picky… I ended up typing it like this:

  ./configure –-add-module="../nginx-rtmp-module-master"

Then I also found my gcc was set with a different kind of error reporting so I needed to do:

  grep -r 'Werror' 

…in the nginx directory and remove all instances of that option in compile commands otherwise the compilation would break… sigh… linux how I love you…

Anyway, At that point I had a working nginx installation and could edit my /usr/local/nginx/conf/nginx.conf

Just add this to the end:

  rtmp{
  server {
  listen 1935;
  chunk_size 4096;
  buflen 4s;
  
  application <whatever_you_want_as_a_password> {
  live on;
  record off;
  push rtmp://fr.pscp.tv:80/x/your_key ;
  push rtmp://live-cdg.twitch.tv/app/live_your_key ;
  }
  }

Then the two commands you need (as root) are:

  /usr/local/nginx/sbin/nginx

…to start… and…

  /usr/local/nginx/sbin/nginx -s stop

…to stop…

3. Putting it together

The stream url for your rtmp app/pi/OBS whatever will be:

  http://your-server.com/your-password/this-stream

…where ‘this-stream’ is just a random name you gave to this stream… And you’ll get streaming to however many services you’ve listed!

The only thing that is driving me up the wall right now is the quality settings. The command I’m using right now to go live is:

  screen -d -m bash -c "ffmpeg -i tcp://127.0.0.1:8181?listen -c:v copy -b:v 1.5M -maxrate 1.5M -bufsize 500k -x264-params "keyint=48:min-keyint=48" -c:a aac -ar 44100 -ab 112k -ac 2 -strict -2 -flags +global_header -bsf:a aac_adtstoasc -f flv 'rtmp://your-server.com/yourpassword/thisstream'" &&
  sleep 5 &&
  /home/pi/picam/picam --alsadev hw:1,0 --tcpout tcp://127.0.0.1:8181

Whatever the quality settings are they need to work on all the services you are using… Hopefully you can grab the relavent numbers out of the above to fit into whatever app you are using to stream!

Happy streaming!

Categories
Raspberry Pi

A twitch chatroom display that appears on an e-ink screen at the press of a button.

I’ve been trying recently to get a twitch video broadcast system that I can just push a button and we’re go. As usual I’m working with a raspberry pi and its basic (but more than adequate) camera.

One of the annoying things I’m running into is firing up my twitch chat server on my phone and picking it up between songs etc. I don’t like having to press/swipe a load more things and I don’t honestly like an lcd screen flashing away at me or a mobile phone/wifi device right next to me while I’m playing (yes I’m sensitive to them – both the blue light and the wifi – I know you probably think I’m crazy but no amount of the government/manufacturers telling me this stuff is safe is ever going to stop me feeling like sh*t when I’m exposed to them too much!).

My pi gives me the potential to eliminate all this and have a wifi/mobile/lcd free device where I literally just have to press one button and the chat server appears on the screen! Cool huh!

Problem is… Twitch lets you into its chatroom with oauth tokens. This means when you disconnect from the chatroom you have to get another token! Really annoying if you do not have a permanent internet connection. Therefore I’ve got a VPS that is permanently logged into my twitch chatroom:-)

So what do we do? Well… Step number one is to actually get twitch to accept me into its chatroom. I’m using weechat as my irc.

Steps are:

  • Log into twitch in your browser
  • Go here: https://twitchapps.com/tmi/ and press to generate a token, then copy it.
  • then ssh into your vps and run ‘screen’ – this way it’ll stay even if you disconnect
  • then run ‘weechat’ in your screen session
  • then in weechat: server add twitch irc.twitch.tv/6667 -password=<pasted oauth token> -nicks=<username> -username=<username>
  • then connect to twitch with: /connect twitch
  • then join your channel with: /join #<username>
  • save your settings: /save

Now if you ssh back into your VPS you can type ‘screen -r -d’ and your twitch chat server will appear again!

Oh and I find Ctrl A – F really useful if I’ve used it on the wrong screen size and now it looks silly – it’ll have a good look at wherever you’re ssh’ing from and figure out what size screen you’re using now and modify itself…

Great, but what happens when your vps host has to reboot to do some security updates? (happened to me recently…) or you manage to completely gum up your vps with zombie processes to the point where you have to reboot from the hosting panel (ah… I errr… once heard a friend of mine did that…) Well then, here’s your set of instructions to sort out your chat screen if it disconnects:

  • go back here and get a new token: https://twitchapps.com/tmi/ Didn’t you know there is an infinite supply!
  • start screen again then weechat
  • back in weechat: /set irc.server.twitch.password <paste it here!>
  • /connect twitch
  • join #<username> and you’re back in!

But what about the e-ink screen I told you about? It’s a waveshare 800*450 one. Terrible refresh rate (we’re talking seemingly endless thumb-twiddling seconds…) But really cheap, good enough for displaying chat messages and has this excellent software for it: https://github.com/joukos/PaperTTY

Essentially what I’ve done is set up my old pi3 (aw I love the lil’ thing – I actually used it as my main work machine for the best part of a year – no kidding… Recorded a load of music on it too…) ummm where was I? Ah yes I set up the pi with the PaperTTY terminal command loading at startup.

In fact I messed with the config of the tty1 systemd process so that instead of coming up with a login screen, it shows tmux right from boot.

And then… And here’s the cool bit – I’ve got a script on my main machine (a pi4 in case you were wondering) that is triggered by xbindkeys so I press one keypress to:

  • log into the pi3 via ssh
  • trigger a local script on the pi3 that does a ‘writevt /dev/tty1 bash connect_to_my_vps.sh’

And connect_to_my_vps.sh does exactly that. It has an RSA key to log in so no password and it autoloads shell from the .bashrc on login.

Voila! One keypress and I’ve got a twich chat server on a reasonably sized e-ink screen.