Streaming to twitch.tv: Difference between revisions

From ArchWiki
(→‎Shell script method: Fix case error)
(Archive page flagged for more than a week)
Tag: Replaced
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Multimedia]]
[[Category:Streaming]]
[[ja:Twitch.tv を使ってストリーミング]]
[[zh-hans:Streaming_to_twitch.tv]]
Twitch.tv is a popular [[wikipedia:Real_Time_Messaging_Protocol|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 ==
#redirect [[ArchWiki:Archive]]
 
[[Category:Archive]]
From [https://help.twitch.tv/s/article/broadcast-guidelines?language=en_US 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 [[wikipedia:YUV#Y.27UV420p_.28and_Y.27V12_or_YV12.29_to_RGB888_conversion|Y'UV420p pixel format]], as Y'UV444 is not widely supported just yet.
 
== GUI solutions ==
 
* [[Open Broadcaster Software]] ({{Pkg|obs-studio}}) is a popularly used streaming program. Alpha Linux builds ({{AUR|obs-studio-git}}) are also available for compiling & testing.
* Castawesome ({{AUR|castawesome}}) is a Gtk3 frontend for ffmpeg streaming with builtin Twitch.tv support.
* SimpleScreenRecorder ({{Pkg|lib32-simplescreenrecorder}}) 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 {{ic|1=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|FFmpeg]] ({{pkg|ffmpeg}})
** PyLivestream ({{AUR|pylivestream-git}})
* [[GStreamer]] ({{pkg|gstreamer}})
** [https://github.com/joanrieu/gst-live gst-live]
 
=== 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 {{ic|stream-to-twitch path/to/stream_key}} into a shell, being the key securely stored with [[pass]]. The script is as follows:
 
{{hc|/usr/local/sbin/stream-to-twitch|<nowiki>
#!/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}")"
</nowiki>}}
 
{{Tip|If using PulseAudio, run {{ic|pactl list sinks short}} to find your Input Audio stream. You can also use a mixer (e.g. {{pkg|pulsemixer}} or {{pkg|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.}}
 
{| class="wikitable"
|+ '''FFmpeg Parameter breakdown'''
! 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
|}

Latest revision as of 10:37, 7 December 2023

Redirect to: