Adding RTMPS for streaming to Facebook

Categories: Uncategorized

Facebook stopped support for RTMP on November 1, 2019. The supported method now is to use an RTMPS stream. Nginx by default does not support RTMPS, and therefore we need to apply a workaround.

The workaround will be to add STunnel, a service that helps establish secure encrypted tunnels. In our case, we will create two RTMP streams in our Nginx server, one would connect directly to Youtube, as it still supports RTMP, while the other stream will be piped to Stunnel which is running locally on the same server.

The stunnel would take an RTMP stream as input, convert that to an RTMPS stream and push it to Facebook.

sudo apt-get install stunnel4 -y

Once installed, enable stunnel to automatically startup on boot

sudo nano /etc/default/stunnel4

Look for ENABLE, this would have a default value of 0, change that to 1.

ENABLE=1

Next we need to configure stunnel to accept connections from our ngnix rtmp stream and push that to facebook.

sudo nano /etc/stunnel/stunnel.conf

Copy paste the following lines

pid = /var/run/stunnel4/stunnel.pid
output = /var/log/stunnel4/stunnel.log 
setuid = stunnel4 
setgid = stunnel4
# https://www.stunnel.org/faq.html
socket = r:TCP_NODELAY=1 
socket = l:TCP_NODELAY=1
 
debug = 4
 
[fb-live] 
client = yes 
accept = 1936 
connect = live-api-s.facebook.com:443
verifyChain = no

save and exit using ctrl+x and press Y.

Now once again open up the Nginx configuration file and add this line to send an RTMP stream to stunnel, listening locally on port 1936.

 sudo nano /etc/nginx/nginx.conf

Add this one line to the configuration file, replacing with your Facebook Key

push rtmps://live-api-s.facebook.com:443/rtmp/<YOUR_Facebook_Stream_Key>;

Save the file using CTRL=X and Enter, Enable the stunnel service to start automatically and then restart the service

sudo systemctl enable stunnel4.service
sudo systemctl restart stunnel4.service

Once the service restart, the RTMP server is now ready to send the streams simultaneously to Youtube and Facebook.


    Leave a Reply

    Your email address will not be published. Required fields are marked *