Streaming to twitch.tv
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Twitch.tv is a popular RTMP-based streaming service. As so, this page serve as a list of solutions for streaming to Twitch.tv. Use cases may include streaming games, Linux desktop, etc.
Twitch Broadcast Requirements
From Twitch.tv support:
Video Requirements
- Codec: H.264 (x264)
- Mode: Strict CBR
- Keyframe Interval: 2 seconds
Audio Requirements
- Codec: AAC-LC, Stereo or Mono
- Maximum bit rate: 160 kbps
- Sampling frequency: any
Note: Support for the MP3 codec has been deprecated
Other Requirements
Not listed on their page is a requirement of the Y'UV420p pixel format, as Y'UV444 is not widely supported just yet.
GUI solutions
- Open Broadcaster Software (obs-studio) is a popularly used streaming program that is now available in the community repo. Alpha Linux builds (obs-studio-gitAUR from the AUR) are also available for compiling & testing.
- Castawesome (castawesomeAUR from the AUR) is a Gtk3 frontend for ffmpeg streaming with builtin Twitch.tv support.
- SimpleScreenRecorder (lib32-simplescreenrecorder from the official repositories) can be configured to stream to twitch.
- For this to work:
- The container needs to be set to FLV
- RTMP URL needs to be put in the 'save as' field
- make sure 'separeate file per segment' is unchecked
- video codec set to libx264 (NOT H.264)
- set bitrate to reasonable value, such as 2000 kbps
- in the custom option field, enter
preset=fast,minrate=2000,maxrate=2000,bufsize=2000,keyint=60
- Note: The value of 'minrate', 'maxrate' and 'bufsize' should be equal to the bit rate
- For this to work:
CLI solutions
FFmpeg
The following solutions make use of FFmpeg for streaming to Twitch.tv.
Shell script method
Shell script for streaming to Twitch.tv using FFMPEG. It supports streaming of both desktop and OpenGL elements. The script can be called by running stream-to-twitch path/to/stream_key
into a shell, being the key securely stored with pass. The script is as follows:
/usr/local/sbin/stream-to-twitch
#!/usr/bin/env sh # # Stream screen and audio (speakers and microphone) to Twitch.tv using FFmpeg. # # Usage: stream-to-twitch path/to/key set -euo pipefail ####################################### # Stream to Twitch.tv. # Globals: # None. # Arguments: # Stream key. A string. # Returns: # None. ####################################### stream_to_twitch() { res_input="1920x1080" # input resolution res_output="1280x720" # output resolution fps="30" # target FPS gop="60" # i-frame interval, should be double of fps gop_min="30" # min i-frame interval, should be equal to fps probesize="42M" # https://stackoverflow.com/a/57904380 threads="2" # max 6 cbr="1000k" # constant bitrate (should be between 1000k–3000k) quality="ultrafast" # one of the many FFmpeg presets audio_input_speakers="0" # speakers' sink id audio_input_mic="default" # microphone's sink id audio_rate="44100" stream_server="live-prg" # see https://stream.twitch.tv/ingests for list stream_key="${1}" # key will be passed as an agument from the command line loglevel="warning" # supress unecessary information from printing ffmpeg \ -loglevel "${loglevel}" \ -f x11grab -s "${res_input}" -r ${fps} -probesize ${probesize} -i :0.0 \ -f pulse -i "${audio_input_speakers}" \ -f pulse -i "${audio_input_mic}" \ -filter_complex "[2]highpass=f=200,lowpass=f=3000[hl]; [1][hl]amix=inputs=2[a]" \ -map 0:v -map [a] \ -f flv -ac 2 -ar ${audio_rate} \ -vcodec libx264 -g ${gop} -keyint_min ${gop_min} -b:v ${cbr} \ -minrate ${cbr} -maxrate ${cbr} -pix_fmt yuv420p \ -s ${res_output} -preset "${quality}" -tune film -acodec aac \ -threads ${threads} -strict normal \ -bufsize ${cbr} \ "rtmp://${stream_server}.twitch.tv/app/${stream_key}" } # Get stream key securely stored with the password manager "pass" # and pass the key to the script to start the stream. stream_to_twitch "$(pass "${1}")"
Tip: If using PulseAudio, run
pactl list sinks short
to find your Input Audio stream. You can also use a mixer (e.g. pulsemixer or pavucontrol) to edit audio sources while the script is running.
Note: Depending on your internet upload speed, you may need to modify the Ffmpeg parameters. use the breakdown list for reference.
Parameter | Description |
---|---|
ffmpeg | The converter |
-loglevel "${LOGLEVEL}" | -loglevel sets logging level output. |
-f x11grab | -f forces input to be from x11grab |
-s $RES_INPUT | -s sets a specific image size from input source, given by variable $RES_INPUT |
-r $FPS | -r sets framerate to be the value equal to $FPS |
-probesize "${PROBESIZE}" | -probesize sets size of the data to analyze to get stream information. |
-i :0.0 | -i gets input, in this case its pulling in screen :0.0 from x11. Can be adjusted, e.g. -i :0.0+500,100 to start at screenpos 500/100 |
-f pulse | forces input to be from PulseAudio |
-i ${AUDIO_INPUT_SPEAKERS} | selects the speakers' sink ID |
-i ${AUDIO_INPUT_MIC} | selects the microphone's sink ID |
-filter_complex ... | apply filter to microphone for reducing noise and merge audio streams |
-map 0:v | maps video stream |
-map [a] | maps audio stream |
-f flv | forces format to FLV |
-ac 2 | sets audio channels to 2 |
-ar ${AUDIO_RATE} | -ar sets audio rate |
-vcodec libx264 | sets video codec to libx264 |
-b:v $CBR | -b:v specifies that the video bitrate is to be changed. the value of the bitrate is set by $CBR |
-pix_fmt yuv420p | sets pixel format to Y'UV420p. Otherwise by default Y'UV444 is used and is incompatible with twitch |
-s $RES_OUTPUT | -s sets a specific image size for output, given by variable $RES_OUTPUT |
-preset "{$QUALILY}" | sets the preset compression quality and speed |
-acodec aac | sets audio codec to use aac |
-threads 0 | sets cpu threads to start, 0 autostarts threads based on cpu cores |