NOTE: This will only work with Facebook Pages, it will not work for your own personal Facebook feed as Facebook does not allow API access to users currently. You’ll get an error if you try.

In my endeavors to make my #PiCam more useful, I wanted to see if I could make it connect and stream to Facebook Live automagically. Why you ask? Well simple, because I’d like to use the #PiCam to stream to something more than just Chew.TV or YouTube. What if I wanted to load up the #PiCam as a live event stream, etc. Honestly the #PiCam can be amazingly useful. So why automate it to connect to Facebook Live? Because I wanted to see if it could be done for cheaper than the MeVo by Livestream. You see, the MeVo is a camera by livestream that will stream to LiveStream, Facebook Live, and Twitter/Periscope. The catch? They want you to spend $400 on it. While I admit, their app looks pretty sweet in setting up multiple shots from the one wide-angle camera feed, it is iOS only. And I’m, at the very least, not an Apple fan 😉

So, can you setup a Raspberry Pi to be able to automatically stream to Facebook? Yes… with a bit of magic. So let’s begin.

First, I making the assumption you’ve viewed and followed the guides to setup your basic #PiCam ( 1, 2, 3 ). Similar to the Chew.TV automation guide we can accomplish what we want with a little bit of PHP and some bash scripting.

At first, I was going to try to use the Facebook PHP-SDK and build it around that.. but that just seemed… excessive. I mean, we can already make calls to a restful api, we did that with the ChewCam. So with a little adaptation, and some finesse. I was able to generate a working prototype to share with you.

Get Your Facebook Page Access Token

The first step is to get your Facebook Page access token. So far, unfortunately you’ll need to have a Facebook app created to do this, even if you don’t do anything with it. I tried using the Graph API Explorer using the user token, but it errors out consistently. Once I switched from the user token to an app token, as is shown in the official Facebook Live API Quick Start Guide, things went smoothly. I’m not going to walk you through setting up a Facebook app, you can Google that yourself. So what am I going to show you here? How to get your access token so you can do the #PiCam magic. So go create your Facebook app, then come back here.

Step 1

Navigate your favorite web browser to the Graph API Explorer. In the area I circled in the screenshot below, change from Graph API Explorer to the FBapp you just created.

Step 2

Once you’ve changed to your new application, click the get token, then select “Get Page Access Token”. This will generate a token into the access token field.


Step 3

After you’ve generated your Page Access Token, click the “Get Token” drop down again and select the page you want to publish your live video to. In this case I’ve chosen Mikey Malone. Copy and paste the token somewhere safe, or simply leave this open in a tab if you want and we’ll go on to the next part of the automation steps. You’ll also want to write down the ID that is returned. It is your Page ID, which you’ll need for your POST API call.

Step 4

You’ll need to request publish_pages permission as well. Click the “Request publish_pages” link and you’re all set.


Scripting for Automation

PHP Page

Now we get to the scripting parts. First, we’re going to build a simple PHP page that will make a POST request to the Facebook Graph. Open your favorite editor and create your page. For simplicity, I’ve named my php page “facebook.php”.

nano facebook.php

Now in your editor, paste the following. Note where it says “{YOUR_ACCESS_TOKEN_GOES_HERE}”  replace that with your actual access token that you just generated. This makes a POST request, which if you’ve properly generated your access_token, will retrieve a JSON response from the Facbeook Graph, it then turns it into an array object so we can pull the stream_url from the JSON, and then it outputs it with the echo command. This is necessary to pass the stream_url on to the bash script we will make. Also you’ll need to substitute the Page ID you made not of for {PAGE_ID}.


$access_token = "{YOUR_ACCESS_TOKEN_GOES_HERE}";

$ch = curl_init('https://graph.facebook.com/{PAGE_ID}/live_videos?access_token='.$access_token);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
$decoded = json_decode($result);

$streamurl = $decoded->stream_url;

echo $streamurl;


Bash Script

Now we’ll create a bash script that will process the PHP script, store the returned URL in a variable, then launch GStreamer to begin streaming automatically to Facebook Live on the Facebook page you set this up for. In your favorite editor, create a shell script. I named mine fbstart.sh, but you can name it whatever.

nano fbstart.sh

Now, you’ll add the scripting into the bash script.


url=`php /home/pi/facebook.php`
gst-launch-1.0 rpicamsrc bitrate=2000000 do-timestamp=true ! video/x-h264,width=1280,height=720,framerate=60/1,profile=high ! h264parse ! queue ! flvmux name=mux streamable=true alsasrc device=plughw:1,0 ! audio/x-raw,rate=44100,channels=2 ! queue! voaacenc ! queue ! aacparse ! queue ! mux. mux. ! rtmpsink location="$url"

Line 1: declares the shell script processor.
Line 2: Sets the bash script variable, in this case called “url” to equal the results of the command executed, php /home/pi/facebook.php
Line 3: This line should look familiar to you! Yes! It’s our old friend Gstreamer launching. However, you’ll notice there’s a few changes. First, the output format is changed to fit the maximum specs supported by FB Live. According to the document I just linked, the current max video specs are 720p video at 30FPS. This may change, however I just set this to the specs provided in the document.

The other change to the gstreamer launch line is with the  rtmpsing location="$url" portion. In the other steps, you’d normally have your RTMP url there, however since you don’t know the URL, you can’t set it. Hence why we went thru these automation steps. The facebook.php page connects to the Graph, gets the rtmp url, then echoes it out. The bash script captures it in the $url variable, then passes the $url variable to the gstreamer command.

Save this shell script and exit to your command line.

Allow Execution

Now, you’ll need to give the shell script execute permissions so you can run it stand-alone:

chmod +x fbstart.sh

Now you should be ready to go!

Using your new #PiCam with Facebook Live

If you’ve followed all the steps from the previous guides and setup your #PiCam, and then done the above, you simply have to start the stream to go live!


You should see the familiar Gstreamer output. To end your output Ctrl+C and it will end publishing the video.


As an aside, the access_token you generate is a temporary access code that expires after 24hours. Facebook does offer longer duration access_tokens but you have to do a bit of work for them. Instead of trying to recreate the wheel, I’ll simply link you to this awesome StackOverflow on the subject: click here

I make no guarantees that it will always work, but it works for now. You can extend it to two months by generating a long_lived_access_token, or even set it to expire never. Doing the never_expire step will require to regenerate your access token if you want to revoke that later.

Also, with a bit of setup, you could manage this to run automatically once powered on. Once again, I won’t be doing this, but the option is there.


Ok, so you’ve got it setup, you’re rocking a new PiCam incorporated into your home/studio streaming option. So what now? Enjoy it! If you enjoyed it and want to support other guides like this, feel free to donate to motivate for other solutions etc!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.