How to Stream to Facebook Live Using FFmpeg RTMP

Streaming directly to Facebook Live using FFmpeg provides complete control over your encoding settings, bandwidth usage, and media sources without relying on third-party GUI software. This guide covers how to retrieve your Facebook stream credentials, configure the required audio and video parameters, and run the precise FFmpeg commands needed to broadcast either a pre-recorded file or a live webcam feed directly to Facebook's RTMP/RTMPS ingest servers.


Step 1: Obtain Your Facebook Live Stream Key

  1. Navigate to your Facebook page, profile, or group and select Live Video to open the Live Producer dashboard.

  2. Under the Select a video source panel, choose Streaming software.

  3. Locate the Stream setup section to find your Server URL and Stream Key.

  4. Combine the Server URL and Stream Key into a single target URL format:

    rtmps://live-api-s.facebook.com:443/rtmp/YOUR_STREAM_KEY

    (Note: Facebook requires RTMPS on port 443 for secure streaming. Ensure your FFmpeg build includes OpenSSL or GnuTLS support).


Step 2: Facebook Live Ingest Requirements

Facebook imposes strict requirements on RTMP/RTMPS streams:

  • Video Codec: H.264 (AVC)
  • Audio Codec: AAC
  • Keyframe Interval (GOP size): Must be exactly 2 seconds (e.g., 60 frames at 30 fps)
  • Pixel Format: YUV420p
  • Audio Sample Rate: 44.1 kHz or 48 kHz
  • Audio Bitrate: 128 kbps (stereo)
  • Maximum Resolution: 1080p (1920x1080) at 60 fps (720p is standard for non-whitelisted accounts)

Step 3: Stream a Pre-recorded Video File

To stream an existing video file (input.mp4) to Facebook as if it were a live broadcast, run:

ffmpeg -re -i input.mp4 \
  -c:v libx264 -preset veryfast -b:v 4000k -maxrate 4000k -bufsize 8000k \
  -pix_fmt yuv420p -g 60 -r 30 \
  -c:a aac -b:a 128k -ar 44100 \
  -f flv "rtmps://live-api-s.facebook.com:443/rtmp/YOUR_STREAM_KEY"

Breakdown of Parameters:

  • -re: Reads the input file in real-time (native frame rate). Crucial for video files to avoid overwhelming the ingest server.
  • -c:v libx264: Uses the H.264 software encoder.
  • -preset veryfast: Balances CPU usage and video compression efficiency.
  • -b:v 4000k -maxrate 4000k -bufsize 8000k: Constrains the video bitrate to a steady 4 Mbps.
  • -pix_fmt yuv420p: Sets the color subsampling format required by Facebook.
  • -r 30: Forces a frame rate of 30 frames per second.
  • -g 60: Sets the GOP (Group of Pictures) size to 60 frames, creating a keyframe every 2 seconds (30 fps × 2 = 60).
  • -c:a aac -b:a 128k -ar 44100: Encodes audio into 128 kbps AAC at a 44.1 kHz sample rate.
  • -f flv: Sets the transport container format to FLV, which is standard for RTMP delivery.

Step 4: Stream from a Connected Camera or Capture Device

If you are streaming live from hardware, remove the -re flag and specify your operating system's input device format.

Windows (DirectShow)

ffmpeg -f dshow -i video="Integrated Camera":audio="Microphone (Realtek Audio)" \
  -c:v libx264 -preset veryfast -b:v 4000k -maxrate 4000k -bufsize 8000k \
  -pix_fmt yuv420p -g 60 -r 30 \
  -c:a aac -b:a 128k -ar 44100 \
  -f flv "rtmps://live-api-s.facebook.com:443/rtmp/YOUR_STREAM_KEY"

Linux (V4L2 and ALSA/PulseAudio)

ffmpeg -f v4l2 -framerate 30 -video_size 1280x720 -i /dev/video0 \
  -f alsa -i default \
  -c:v libx264 -preset veryfast -b:v 3000k -maxrate 3000k -bufsize 6000k \
  -pix_fmt yuv420p -g 60 -r 30 \
  -c:a aac -b:a 128k -ar 44100 \
  -f flv "rtmps://live-api-s.facebook.com:443/rtmp/YOUR_STREAM_KEY"

macOS (AVFoundation)

ffmpeg -f avfoundation -framerate 30 -video_size 1280x720 -i "0:0" \
  -c:v libx264 -preset veryfast -b:v 3000k -maxrate 3000k -bufsize 6000k \
  -pix_fmt yuv420p -g 60 -r 30 \
  -c:a aac -b:a 128k -ar 44100 \
  -f flv "rtmps://live-api-s.facebook.com:443/rtmp/YOUR_STREAM_KEY"

Step 5: Completing the Broadcast

  1. Execute the chosen command in your terminal.
  2. Return to the Facebook Live Producer dashboard.
  3. Within 5 to 15 seconds, the preview screen in Facebook Live Producer should display your incoming stream.
  4. Verify the audio and video health in the dashboard.
  5. Click Go Live in the bottom-left corner of the Facebook Live interface to publish the broadcast to your audience.