Stream to any OR Multiple RTMP Destinations + Record Simultaneously

This is a guide that enables you simulcast to Youtube, Facebook, Periscope & any rtmp server
EDIT: Added support to Stream & Record Simultaneously. See NGINX-RTMP Section

You need to make sure your firewall doesnt block rtmp

Step 1: jitsi-jibri setup

I’m not going to detail it as @Woodworker_Life has written an excellent guide already

OR if you prefer to have both jitsi & jibri running on the same machine, use this jitsi-jibri installer script developed by @Ark74

Step 2: Setup rtmp NGINX Proxy

It can be setup in any linux machine (VM or hardware). It might work in RPi but i’ve not tested it

a. Spin up an Ubuntu VM.

I prefer to run as root

apt update && apt upgrade -y

b. Install nginx and the required packages:

apt install build-essential libpcre3 libpcre3-dev libssl-dev nginx libnginx-mod-rtmp ffmpeg -y

c. Edit nginx.conf with vim, vi OR nano

nano /etc/nginx/nginx.conf

and Add the following lines in the end of the file. Make sure to adjust

rtmp {
        server {
                listen 1935;
                chunk_size 4096;

                application live {
                        live on;
                         #Set this to "record off" if you don't want to save a copy of your broadcasts
                         record all;
                         # The directory in which the recordings will be stored. 
                         record_path /var/www/html/recordings;
                         record_unique on;

                        #Facebook
                        push rtmp://127.0.0.1:1936/rtmp/INSERT-YOUR-STATIC-STREAM-KEY;

                        #Periscope -- Goto pscp.tv/account/producer & create a stream. select the region of your choice and copy & paste it at the end of the next line
                        exec /usr/bin/ffmpeg -i rtmp://127.0.0.1/live/$name -crf 30 -preset ultrafast -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 64k -vcodec libx264 -x264-params keyint=60:no-scenecut=1 -r 30 -b:v 500k -s 960x540 -f flv rtmp://ca.pscp.tv:80/x/your-persicope-key;

                        #Youtube Stream - YT doesnt assign a permanent stream key, BUT if you reuse an earlier stream, the key doesnt change. EDIT: apparently you can create one i am told. but i use the earlier method
                        push rtmp://a.rtmp.youtube.com/live2/YOUTUBE-stream-key;
                }
        }
}

Create the directory for recordings and make it writeable to the web server software:

mkdir -p /var/www/html/recordings
chown -R www-data:www-data /var/www/html/recordings/

d. restart nginx service

systemctl restart nginx.service

Step 3. Facebook uses rtmps and hence you need stunnel to send rtmps to facebook as NGINX RTMP proxy does only rtmp

a. install stunnel

apt-get install stunnel4 -y

b. Enable stunnel auto start

nano /etc/default/stunnel4

& Change ENABLE from 0 to 1

ENABLE=1

c. edit stunnel config. (This file is new & doesnt exist so go ahead )

nano /etc/stunnel/stunnel.conf

add 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

d. enable stunnel after boot

systemctl enable stunnel4.service

e. restart stunnel

systemctl restart stunnel4.service

Double check Ports. ngnix an stunnel need to be on different ports when used on the same machine.
Facebook can be a bit finicky. If you dont see the preview in your producer page, just restart the stunnel4 service

Step 4: ffmpeg rtmp Script

@lodopidolo as create a brilliant yet a simple script to proxy the video from jitsi/jibri to the rtmp server/sink

THIS HAS TO BE DONE ON THE JIBRI SERVER

a. Add a directory

mkdir -p /opt/util

cd /opt/util

b. create a file

nano ffmpeg

I’ve changed the language to english, but it really doesnt matter. you can use his script code. paste the script below into the file & save

#!/bin/bash

COMMAND="/usr/bin/ffmpeg"

while test $# -gt 0
do
    T="$1"
    if [ "${1:0:32}" == "rtmp://a.rtmp.youtube.com/live2/" ]; then
        # T  will contain the rtmp key from jitsi meet page. Make sure you use the correct IP Address OF the rtmp server you setup earlier
        COMMAND="$COMMAND rtmp://192.168.10.50:1935/live/${T:32}"             
    else
        COMMAND="$COMMAND $T"
    fi
    shift
done

echo "RUNNING FFMPEG: «$COMMAND»."

exec $COMMAND
PROCESS_FFMPEG=$!

echo "Esperando finalización del proceso: ${PROCESO_FFMPEG}."
wait $PROCESS_FFMPEG

MAKE SURE of the location of /bin/bash & /usr/bin/ffmepg (which bash & which ffmpeg)

c. convert the file to be executable

chmod +x ffmpeg

d. now we have to make jibri run this script instead of the main ffmpeg

systemctl edit --full jibri

No add this line in [Service] section. please ensure your java_path is correct. else it will fail

Environment="PATH=/opt/util:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Save and you are all set to go

I recommend that you test it with burner accounts on all platforms. I prefer having a manual control in going live. You can enable auto-start but i prefer to CLICK on the GO LIVE button on all the platforms

Now start a meeting. Click on “Start Live Stream”. You can put any thing in the box. it really doesnt matter. I put the YT streamkey.

Once you go live, you should see the preview show up on all platforms

Feedback welcome

PRO-TIP - DO THIS ONLY IF YOU KNOW WHAT YOUR’RE DOING

Thanks to @xranby . He has show how you can modify the code to address the ONE-TO-MANY use case.

Head over here. once you make changes, you have to “make” your own build.

12 Likes

Really nice!.. Well done, iLike! :+1:

This command is not working on my server.
OS: Ubuntu 16.04, Niginx is installed.

Error: E: Unable to locate package libnginx-mod-rtmp

What should i do to fix this?

Use Ubuntu 18.04 LTS - why are you using 16.04 LTS ?

The package you need is not available in Ubuntu 16.04 LTS.

1 Like

doesnt work on 16.04

Super @masteryoda,

This is valuable guide. May I suggest you to write this too on Jibri’s Wiki https://github.com/jitsi/jibri or Jits-Meet Wiki https://github.com/jitsi/jitsi-meet/wiki (if you have been a Jitsi contributor, i hope)?. Love to see it could help a lot of people for the same quest.

Also, my question: How and does this change has been secured and saved outside jibri folder from future Jibri’s overwrite update (you, know, I am not too smart :slight_smile: actually) attempt in future?

Thank you.

thanks, good idea. I’m not a contributor though.

since the script is standalone one, not a part of jibri, any updates wouldnt affect it.

Not sure if this is a very basic question as I am not well versed with how streams works , but what if FaceBook and Youtube having different Live Stream Key ?. Stream Key passed as parameter to jibri from meet and same used in script here. Pardon me if I missed anything here

jibri is “made to believe” that its streaming to youtube. jibri, internally, hands off the job to ffmpeg. what the script is doing is kind of proxying that ffmpeg stream to a ngnix/rtmp proxy which in turn can stream to multiple platforms simultaneously

3 Likes

Thanks a lot for explaining so clearly. If I understood correctly, here jitsi-meet will pass stream key for youtube only. and internally same key passed to nginx rtmp server. Youtube streamed using jitsi-meet passed stream key and FB live will use static hard coded key mentioned in script.

you have to use respective streamkeys from each provider

the streamkey passed from jitsi to jibri will anyway be dropped by the nginx/rtmp proxy. you can actually put any character in the streamkey dialog box on jitsi-meet. it will still work… its just safer to use a streamkey. you can pretty much us eany key.

Use the CORRECT Stream-key in the nginx.conf file of the rtmp/nginx server though. else it wont work

Thanks

Bow shall you take @masteryoda :pray:t2:

This works very well! I made slight modifications to make it configurable and added it to my Terraform configuration for AWS here:

I have tested it with simulcast streaming to Facebook, YouTube, Periscope/Twitter, and Twitch at the same time. Also made all recordings available under /recordings with basic auth enabled. Wooohoo!

4 Likes

there is a redundant step i guess. if you enable recording folder in jibri & also in the nginx proxy, it will be recorded in 2 places. you can disable in 1 place.

@masteryoda I’m moving Jibri’s recorded files from /tmp/recordings to the webroot in my finalize_recording.sh script that runs after a recording is stopped. So ultimately they all stay under one location on the filesystem.

Ref:

1 Like

Continuing the discussion from Stream to any OR Multiple RTMP Destinations + Record Simultaneously:

This is regarding facebook live script, first of all thanks for posting such a useful script.

I have upgraded ffmpeg in system which support rtmps: protocol , I made a few changes in script to directly send to FB via rtmps://, and my script is as below,

++++++

#!/bin/bash

COMANDO="/usr/local/bin/ffmpeg"
while test $# -gt 0
do
T="1" if [ "{1:0:32}" == “rtmp://a.rtmp.youtube.com/live2/” ]; then
# T will contain the rtmp key from jitsi meet page.
COMANDO="COMANDO rtmps://live-api-s.facebook.com:443/rtmp/{T:32}"
else
COMANDO="$COMANDO $T"
fi
shift
done
echo “$COMANDO”>/tmp/logs
#exec COMANDO #PROCESS_FFMPEG=!
#wait $PROCESS_FFMPEG
$COMANDO
++++
and can in logs that below command getting executed ,
/usr/local/bin/ ffm peg -y -v info -f x11grab -draw_mouse 0 -r 30 -s 1280x720 -thread_queue_size 4096 -i :0.0+0,0 -f alsa -thread_queue_size 4096 -i plug:cloop -acodec aac -strict -2 -ar 44100 -c:v libx264 -preset veryfast -maxrate 2976k -bufsize 5952k -pix_fmt yuv420p -r 30 -crf 25 -g 60 -tune zerolatency -f flv rtmps://live-api-s.facebook.com:443/rtmp/2952996061451936?s_bl=1&s_sc=2952996121451930&s_sw=0&s_vt=api-s&a=AbyCwP8KZbDGAq7_

But on FaceBook live, no audio video is coming just a simple screenshot with log in event that “Live service received the video”

is it frame rate or codec issue ?

ok.

just remember that jibri on its own can only stream OR record not both. thats why i’ve removed the record button in my setup and do all recording (if required) using the nginx/rtmp server .

one more thing. you must install prosody 0.11.x for secure domain + room password to work. else if you set a room password or meeting password, jibri will not work

i dont think it will work. the streamkey for FB is not the same as YT. so if you dont use the nginx/rtmp proxy you can only stream to FB OR Youtube.

Yes right, its not a final script I was just testing with FaceBook without apache and stunnel as my ffmpeg support rtmps:// . This script is serving purpose by changing url for FaceBook , and I can notice same in ps -ef and log
+++++
/usr/local/bin/ ffm peg -y -v info -f x11grab -draw_mouse 0 -r 30 -s 1280x720 -thread_queue_size 4096 -i :0.0+0,0 -f alsa -thread_queue_size 4096 -i plug:cloop -acodec aac -strict -2 -ar 44100 -c:v libx264 -preset veryfast -maxrate 2976k -bufsize 5952k -pix_fmt yuv420p -r 30 -crf 25 -g 60 -tune zerolatency -f flv rtmps://live-api-s.facebook.com:443/rtmp/xxxxxxxxxxyyyy?s_bl=1&s_sc=2952996121451930&s_sw=0&s_vt=api-s&a=AbyCwP8KZbDGAq7_
++++++++++

But audio/video not coming on facebook live , despite events log of Facebook says video received

Does YouTube and FaceBook not support same ffmpeg options like bitrate/codec. Facebook receiving my command on live url using stream key , but video and audio not coming

i guess not… FB requirements are quite finicky, so not sure…