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:
- Video Codec: H.264 (libx264)
- Audio Codec: AAC (libfdk_aac or native aac)
- Rate Control: Constant Bitrate (CBR)
- Keyframe Interval: Exactly 2 seconds
- H.264 Profile: High or Main
- Maximum Bitrate: 6000 Kbps for non-partners (up to 8000 Kbps for partners/selected creators)
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.
-profile:v high: Restricts the encoder to the High profile, which is compatible with almost all modern playback devices and delivers better quality at lower bitrates.-preset veryfast: Controls the trade-off between encoding speed and compression ratio.veryfastis the recommended starting point for live streaming to keep CPU usage low.
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.
-b:v 6000k: Sets the target video bitrate to 6000 Kbps.-maxrate 6000k: Limits the maximum bitrate spikes to 6000 Kbps.-bufsize 6000k: Sets the encoder’s buffer size. Matching this to your target bitrate ensures the encoder checks and corrects the bitrate limit regularly.
3. Configuring the Keyframe Interval (GOP)
Twitch’s delivery system relies on keyframes occurring exactly every 2 seconds.
-g 120: Sets the Group of Pictures (GOP) size. For a 60 fps stream, a keyframe every 2 seconds requires a GOP size of 120 (60 fps * 2 seconds). For a 30 fps stream, change this to60.-keyint_min 120: Forces the minimum keyframe interval to match the maximum.-sc_threshold 0: Disables scene change detection to prevent FFmpeg from inserting extra keyframes between the designated 2-second intervals.
4. Audio Settings
Twitch accepts AAC audio. Recommended audio bitrates range between 128 Kbps and 160 Kbps.
-c:a aac: Selects FFmpeg’s native AAC encoder.-b:a 160k: Sets the audio bitrate to 160 Kbps.-ar 44100: Sets the audio sample rate to 44.1 kHz, which is the Twitch standard.
5. Output Format and RTMP URL
FFmpeg must package the output stream into an FLV container before transmitting it via RTMP.
-f flv: Forces the output format to FLV.rtmp://live.twitch.tv/app/live_your_stream_key: The target ingest URL. You can replacelive.twitch.tvwith a region-specific Twitch ingest server (e.g.,live-jfk.twitch.tvfor New York) to reduce latency and improve stability.