Facebook Live using FFmpeg

You may have see that some FaceBook Live posts look better than others. More like they are shot in a studio and not on a cell phone. Well, that's because they are. There are various hardware devices and web streaming services that can do it but I'm going to show you an easy way to do it for free (assuming you have the needed computers and cameras etc).

Step One. Setup a computer with FFmpeg. FFmpeg is a free (opensource) software that is described as "A complete, cross-platform solution to record, convert and stream audio and video." and it is very powerful. With it I have converted video files, recorded video files, ripped audio from a video, and a few other things. Today we're going to take a live stream from an Axis IP camera and then stream it out to FaceBook Live using just one command on the command line. So, I'm using the Windows version of FFmpeg which you can get here https://ffmpeg.zeranoe.com/builds/ or you can get Linux, Mac, and any other build from http://ffmpeg.org

Step Two. After you've gotten FFmpeg installed it's time to setup your video source. Being that we're talking about FaceBook Live, we're going to choose a live source. For that I'm using a standard definition Axis encoder Q7401 connected up to the churches AV system through a series of converters to our very expensive HD cameras. This process would work the same if you were just using an IP camera as well. So, for me the RTSP string from the camera looks something like this rtsp://10.x.x.x/Axis/media.aspx I would recommend testing this out on VLC Player to make sure you get the syntax correct before moving on. There is an awesome site that will tell you just about every connection string to every device here.

Step Three. Once you've gotten the correct syntax for your camera, you need to feed that into FFmpeg and tell it to do something. So, let's take that input and stream it out to Facebook Live. To do that we first need to schedule an event and get the one-time connection key. Unfortunately you cannot use this method with a personal Facebook page. You have to have a "Page" to be able to do this. Once you are an admin of a page, you will get more options at the top like this:

Click on "Publishing Tools" and then click on "Videos" on the left menu. When you do you will see a screen with past videos if you have any and on the top right you will see "Live"  Next, you will see a page like this showing your connection string:

This page tells us exactly what we need. We'll use the top "Server or Stream URL". Don't worry if you didn't get it before you click next because it will display again. Take note to the note at the bottom. Once you click next you have 5 hours to go live then it will no longer be valid. Click next. A new screen comes up that asks for you to name your event and then allows you to preview your stream before you go live.

On this screen you can see your stream key again and name the video.
If we had an active stream it would show up here. It won't let you click the "Go Live" button until you have a stream going. But you can schedule a future stream by click the dropdown arrow and picking "Schedule Live". That part is cool because it puts a blurb on your feed saying that your going live at a specific time which could help drive traffic to your broadcast. For now we're going to just do the Live option so let's get your feed going.

Step Four. Now is when the rubber meets the road. Open up a command window (on Windows 8.1 or 10, press Win + X key and then select Command Prompt). At the prompt we're going to use the following format:
  ffmpeg -i  -f flv

Facebook Live is also picky on the audio and video settings. You can view the requirements here. To get this to work in my situation I found that I had to do some work to my stream to get a reliable output.
ffmpeg -rtsp_transport tcp -y -i "rtsp://" -t 5400 -c:a copy -ac 1 -ar 44100 -b:a 128k -c:v libx264 -pix_fmt yuv420p -r 30 -g 60 -vb 2048k -minrate 2000k -maxrate 4000k -bufsize 4096k -threads 2 -f flv "rtmp://rtmp-api.facebook.com:80/rtmp/133333333333?ds=1&s_l=1&a=ATg99929999x84tR"

As you can see there are a lot of options that you can customize with FFmpeg! In a nutshell, I created a different "Stream Profile" on my Axis device with the source settings I wanted like if it should display the date at the top, the resolution, the frame rate and bitrate, and some other settings. Then I use the -t option to run the script for 5400 seconds or 90 minutes. I copy the audio stream and set the sample and bit rates. For the video I had to run it through the h.264 encoder and set the color space as well as the frame rate and the min and max bitrates. I also found that I needed to dedicate two processors to this to keep up with the transcoding on my computer with the -threads 2 command. Then I pipe it to the -f flv command that sends the flash ramp stream to the Facebook API with my streaming key.
If I press the enter key and run the script it will throw up a bunch of stuff in terminal and then it will settle down to something like this:

You may see some errors when the feed is just starting out but they should go away and look like the bottom half of this command prompt window. If you go back to your web browser and Facebook Live scheduling you should see the window has changed to have "Preview" in it and showing your video! You will notice that the "Go Live" button is now clickable. You can click it to start right away or click the down arrow and schedule it for later. Just make sure your feed is scheduled to go long enough or don't put the -t option in at all. You will just have to press Ctr + C in the command prompt to terminate the feed when you want.

Some additional notes:
  • If you lose your stream for any reason the Facebook live event will stop and post to your timeline.
  • This is a one time key. You will need to do this process each time. There is probably a better way of doing this with an API but I haven't gotten to that yet.
  • If you don't want the live post to be viewable after the event ends you can select "Unpublish after live video ends" This will preserve your stats and allow you to post it later if you wanted.
  • If you have any prerecorded music or video in your stream Facebook will most likely not catch it in the live feed, but will take down your replay soon after it is posted. We've had this happen many times. You usually have the option to post it if you agree that you have the proper copyright information, but it's just a real pain. The better option would to send a different feed to the live stream that doesn't have audio from Spotify or other music. I imagine they send the video through something like Shazam app to identify any copyrighted music.
  • You will get better quality with a better encoder obviously. One weird limit is that FBlive only goes up to 720p right now so you'll have to downconvert if you've got a 1080p stream.
So, this is the low cost, DIY Facebook Live solution. There is software out there to do it for you like Wirecast but many cost much more and don't offer much more features mainly because Facebook has it so locked down.