Scheduling a pre-recorded video file to an RTMP server as if it were live
Every Friday night at 8PM, we debut on TV a new episode of a TV show. I've been asked to simultaneously broadcast this new episode to Facebook Live, so there is more than one place to watch it. This gets tricky, because facebook live isn't necessarily set up for this purpose. Live is meant more for broadcasting from professional equipment, rather than a pre-recorded video file.
The way I accomplish this is the following:
- Create a droplet on Digital Ocean
- Install FFMPEG on said droplet
- Transfer the video file to the droplet
- Set a Cron Job that plays the video via FFMPEG with facebook's RTMP as the output.
Installing FFMPEG
Installing FFMPEG is a pain: but there are several resources out there. I'm on Ubuntu, so I'll explain with that.
Ubuntu 18.04/16.04:
sudo add-apt-repository ppa:jonathonf/ffmpeg-3
sudo apt-get update
sudo apt-get install ffmpeg libav-tools x264 x265
Transfer the video file to the droplet
This should be pretty straight forward. I usually use FileZilla or something similar, to drag the video file there.
Scheduling the Video - CRON
Now you need to create a cronjob that plays the video at the time you set. In my example, I chose 8:00PM on Friday.
I created a file at /var/www/livestream called stream.sh that contains the following:
/usr/local/bin/ffmpeg -re -i /var/www/livestream/video.mp4 \
-acodec aac -ar 44100 -b:a 128k -pix_fmt yuv420p -profile:v baseline \
-bufsize 6000k -vb 2000k -maxrate 4000k -deinterlace -vcodec libx264 \
-preset ultrafast -g 30 -r 30 -f flv \
"rtmp://live-api.facebook.com:80/rtmp/YOUR_FACEBOOK_STREAM_KEY_HERE"
I then modified that script to run as the user I created it as:
chmod 700 stream.sh
And then edited my crontab:
crontab -e
And then added the following:
0 3 * * 6 /var/www/livestream/stream.sh >/dev/null 2>&1
8PM PST on Friday equates to the 0 3 * * 6. The /var/www/livestream/stream.sh is where I placed my script, and the output is set to null. This means that when the job runs, it does so silently without altering me or logging it.