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.
- Go to the YouTube Creator Studio.
- Click on the Create button (camera icon) in the top right and select Go live.
- In the Stream Settings tab, locate the Stream URL and Stream key.
- 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
2. Recommended FFmpeg Settings for YouTube
YouTube has specific recommendations for video and audio encoding to ensure stable playback:
- Video Codec: H.264 (libx264)
- Audio Codec: AAC (aac)
- Keyframe Interval (GOP): 2 seconds (crucial for YouTube live ingestion)
- Pixel Format: yuv420p
- Format Container: FLV (required for RTMP streaming)
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_KEYParameter Breakdown:
-re: Read the input file at its native frame rate. This is required when streaming static files to simulate a live feed. Do not use this flag if you are capturing from a hardware device (like a webcam or capture card).-i input.mp4: The source video file. Replace this with your source file path or hardware input path.-c:v libx264: Encodes the video stream to H.264.-preset veryfast: Balance between compression efficiency and CPU usage.-b:v 4500k -maxrate 4500k -bufsize 9000k: Sets a constant bitrate of 4500 Kbps (ideal for 1080p at 30fps).-pix_fmt yuv420p: Sets the pixel format to YUV 4:2:0, which is widely compatible with consumer devices and streaming platforms.-g 60: Sets the Group of Pictures (GOP) size to 60 frames. For a 30fps video, this creates a keyframe every 2 seconds, as required by YouTube.-c:a aac: Encodes the audio stream to AAC.-b:a 128k: Sets the audio bitrate to 128 Kbps.-ar 44100: Sets the audio sample rate to 44.1 kHz.-f flv: Forces the output format to FLV, the standard container format for RTMP streams.rtmp://.../YOUR_STREAM_KEY: The destination RTMP URL. ReplaceYOUR_STREAM_KEYwith your actual YouTube stream key.
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_KEYOn 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