Streaming to twitch.tv

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. 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

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:

Note: Depending on your internet upload speed, you may need to modify the Ffmpeg parameters. use the breakdown list for reference.
Ffmpeg Parameter breakdown
ParameterDescription
ffmpegThe 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 pulseforces 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:vmaps video stream
-map [a]maps audio stream
-f flvforces format to FLV
-ac 2sets audio channels to 2
-ar ${AUDIO_RATE}-ar sets audio rate
-vcodec libx264sets 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 yuv420psets 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 aacsets audio codec to use aac
-threads 0sets cpu threads to start, 0 autostarts threads based on cpu cores
This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.