Streaming to twitch.tv

出自 Arch Linux 中文维基

Twitch.tv 是一個流行的 RTMP 的流媒體服務。因此,本頁面為 Twitch.tv 提供流媒體服務的解決方案列表。使用案例可能包括流媒體遊戲、Linux 桌面等。

Twitch 廣播要求[編輯 | 編輯原始碼]

根據 Twitch.tv 支持頁面:

視頻要求
  • 編解碼器:H.264 (x264)
  • 模式:嚴格恆定比特率
  • 關鍵幀間隔:2 秒
音頻要求
  • 編解碼器:AAC-LC,立體聲或單聲道
  • 最大比特率:160 kbps
  • 採樣頻率:任意
注意: 已取消支持 MP3 編解碼器
其他要求

圖形用户界面解決方案[編輯 | 編輯原始碼]

  • Open Broadcaster Softwareobs-studio)是一款流行的流媒體程序。Alpha Linux 構建的安裝包(obs-studio-gitAUR)也可用於編譯和測試。
  • Castawesome(castawesomeAUR)是一款用於 ffmpeg 流媒體的 Gtk3 前端,內置 Twitch.tv 支持。
  • SimpleScreenRecorder(lib32-simplescreenrecorder)可對 Twitch 串流(直播)進行配置:
    • 容器需設置為 FLV
    • RTMP URL 需要放在『另存為』欄中
    • 確保沒有選中「文件分段」
    • 視頻編解碼器設置為 libx264(不是 H.264)
    • 設定合理的比特率,如 2000 kbps
    • 在自定義選項欄中,輸入preset=fast,minrate=2000,maxrate=2000,bufsize=2000,keyint=60
注意: 「minrate」、「maxrate」和「bufsize」的值應等於比特率。

命令行界面解決方案[編輯 | 編輯原始碼]

FFmpeg[編輯 | 編輯原始碼]

下面的解決方案利用了 FFmpeg 來實現向 Twitch.tv 串流(直播)。

shell 腳本方法[編輯 | 編輯原始碼]

使用 FFmpeg 實現的向 Twitch.tv 串流(直播)的 shell 腳本。它支持對桌面和 OpenGL 元素進行串流。可通過運行 stream-to-twitch path/to/stream_key 在 shell 中調用,密鑰則使用 pass 安全存儲。腳本內容如下:

/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}")"
提示:如果使用 PulseAudio,請運行 pactl list sinks short 尋找輸入音頻流。也可使用混音器(例如 pulsemixerpavucontrol)在腳本運行時編輯音源。
注意: 根據你的網絡上傳速度,你可能需要修改 FFmpeg 的參數。請使用參數明細作為參考。
FFmpeg 參數明細
參數 描述
ffmpeg 轉換器
-loglevel "${LOGLEVEL}" -loglevel 設置日誌輸出級別
-f x11grab -f 強制從 x11grab 輸入
-s $RES_INPUT -s 從輸入源設置一個指定的圖像尺寸,由變量 $RES_INPUT 提供
-r $FPS -r 設置幀率為 $FPS
-probesize "${PROBESIZE}" -probesize 設置要分析的數據大小,以獲得流信息
-i :0.0 -i 獲取顯示(器)輸入,在這裏,是從 x11 的 :0.0 顯示(器)獲取。可以調整,例如:-i :0.0+500,100 是指在屏幕位置 500/100 處開始獲取
-f pulse 強制從 PulseAudio 輸入
-i "${AUDIO_INPUT_SPEAKERS"} 選擇揚聲器的 sink ID
-i "${AUDIO_INPUT_MIC"} 選擇麥克風的 sink ID
-filter_complex ... 將過濾器應用到麥克風,以減少噪音並合併音頻流
-map 0:v 映射視頻流
-map [a] 映射音頻流
-f flv 強制格式為 FLV
-ac 2 設置聲道為 2
-ar "${AUDIO_RATE}" -ar 設置音頻比特率
-vcodec libx264 設置視頻編解碼器為 libx264
-b:v "$CBR" -b:v 指定要改變到的視頻比特率。比特率的值由 $CBR 決定
-pix_fmt yuv420p 設置像素格式為 Y'UV420p。否則默認使用 Y'UV444,這與 Twitch 不兼容
-s $RES_OUTPUT -s 為輸出設置一個指定的圖像尺寸,由變量 $RES_OUTPUT 提供
-preset "{$QUALITY}" 設置預設的壓縮質量和速度
-acodec aac 設置音頻編解碼器以使用 AAC
-threads 0 設置啟動的 CPU 線程,為 0 則根據 CPU 核心數自動啟動線程