How to Stream to Twitch Using FFmpeg

Streaming directly to Twitch using FFmpeg offers unparalleled control over your broadcast’s technical parameters. This guide provides a straightforward walk-through on how to configure FFmpeg command-line arguments to target Twitch’s ingest servers, apply specific H.264 encoding profiles, set precise video and audio bitrates, and optimize your stream for reliable, low-latency live playback.

Twitch Streaming Requirements

To ensure your stream is accepted by Twitch without transcoding issues, your FFmpeg command must comply with Twitch’s strict ingest guidelines:


The FFmpeg Command Structure

Here is a standard, optimized FFmpeg command to stream a video source to Twitch at 1080p at 60 frames per second:

ffmpeg -f lavfi -i testsrc=size=1920x1080:rate=60 -f lavfi -i sine=frequency=1000 \
-c:v libx264 -preset veryfast -profile:v high -level:v 4.2 \
-b:v 6000k -maxrate 6000k -bufsize 6000k -pix_fmt yuv420p \
-g 120 -keyint_min 120 -sc_threshold 0 \
-c:a aac -b:a 160k -ar 44100 \
-f flv rtmp://live.twitch.tv/app/live_your_stream_key

(Note: Replace -f lavfi -i ... with your actual video and audio input sources, and change live_your_stream_key to your actual Twitch stream key.)


Command Breakdown and Configuration

1. Setting the Encoding Profile and Preset

Twitch recommends using the High or Main profile. The profile dictates the compression features used by the H.264 encoder.

2. Enforcing Constant Bitrate (CBR)

Twitch requires a strict Constant Bitrate to prevent packet loss and buffering for viewers. To force CBR in FFmpeg, you must set the target bitrate, maximum bitrate, and buffer size to the same value.

3. Configuring the Keyframe Interval (GOP)

Twitch’s delivery system relies on keyframes occurring exactly every 2 seconds.

4. Audio Settings

Twitch accepts AAC audio. Recommended audio bitrates range between 128 Kbps and 160 Kbps.

5. Output Format and RTMP URL

FFmpeg must package the output stream into an FLV container before transmitting it via RTMP.