Stream to YouTube Live via RTMP using FFmpeg

This article provides a direct, step-by-step guide on how to stream live video to YouTube Live using FFmpeg via the RTMP protocol. You will learn how to locate your YouTube stream credentials, configure the optimal FFmpeg encoding settings required by YouTube, and execute the command to start your live broadcast from a video file or a hardware input.

1. Retrieve Your YouTube Stream Key and URL

To stream to YouTube, you need your unique Stream Key and the RTMP ingestion URL.

  1. Go to the YouTube Creator Studio.
  2. Click on the Create button (camera icon) in the top right and select Go live.
  3. In the Stream Settings tab, locate the Stream URL and Stream key.
  4. Keep the Stream key private, as anyone with access to it can broadcast to your channel.

Typically, the primary YouTube RTMP ingestion URL is: rtmp://a.rtmp.youtube.com/live2

YouTube has specific recommendations for video and audio encoding to ensure stable playback:

3. The FFmpeg Command

Below is the standard FFmpeg command to stream a local video file to YouTube Live.

ffmpeg -re -i input.mp4 \
  -c:v libx264 -preset veryfast -b:v 4500k -maxrate 4500k -bufsize 9000k \
  -pix_fmt yuv420p -g 60 \
  -c:a aac -b:a 128k -ar 44100 \
  -f flv rtmp://a.rtmp.youtube.com/live2/YOUR_STREAM_KEY

Parameter Breakdown:

4. Streaming a Live Device (Webcam/Capture Card)

If you are streaming from a physical input device rather than a pre-recorded file, remove the -re flag and specify your device path.

On Linux (using v4l2 and ALSA):

ffmpeg -f v4l2 -i /dev/video0 -f alsa -i hw:0 \
  -c:v libx264 -preset veryfast -b:v 4500k -maxrate 4500k -bufsize 9000k \
  -pix_fmt yuv420p -g 60 \
  -c:a aac -b:a 128k -ar 44100 \
  -f flv rtmp://a.rtmp.youtube.com/live2/YOUR_STREAM_KEY

On Windows (using dshow):

ffmpeg -f dshow -i video="Integrated Camera":audio="Microphone" \
  -c:v libx264 -preset veryfast -b:v 4500k -maxrate 4500k -bufsize 9000k \
  -pix_fmt yuv420p -g 60 \
  -c:a aac -b:a 128k -ar 44100 \
  -f flv rtmp://a.rtmp.youtube.com/live2/YOUR_STREAM_KEY