Skip to content

Instantly share code, notes, and snippets.

@tomatrow
Last active March 29, 2020 17:45
Show Gist options
  • Save tomatrow/dbff88ce66d59c19f0b5331dd05e1a7c to your computer and use it in GitHub Desktop.
Save tomatrow/dbff88ce66d59c19f0b5331dd05e1a7c to your computer and use it in GitHub Desktop.
Twitch streaming customized for coding (mostly reading documentation)."
# AJ Caldwell - tomatrow@gmail.com
function codestream -d "Twitch streaming customized for coding (mostly reading documentation)."
# Input configuration.
set IN_VIDEO_ID "1" # The main screen we code from.
set IN_AUDIO_ID "0" # The Twitch-Input we set previously.
set SOURCE "$IN_VIDEO_ID:$IN_AUDIO_ID" # avfoundation assigns numbers to video/audio devices.
set INRES "1440x900" # input resolution for a macbook air
set IN_FPS "10" # Our rate of video capture.
set AUDIO_RATE "44100" # What we record at.
# Output video configuration.
set OUTRES "$INRES" # for consistentcy
set FPS "$IN_FPS" # target FPS
set GOP (math "$FPS * 2") # i-frame interval, should be double of FPS,
set GOPMIN "$FPS" # min i-frame interval, should be equal to fps,
set CBR_INT "800" # The constant-bit-rate.
set CBR (string join "" "$CBR_INT" "k") # The screen is usually staic => we select a low rate.
set QUALITY "ultrafast" # one of the many FFMPEG presets
set VIDEO_BUF_SIZE (math "$CBR_INT * 2") # maxrate/bufsize = frequency of checks
set VIDEO_THREADS "1" # 0 <-> optimal, max 6
# Output audio configuration.
set AUDIO_THREADS "0" # 0 <-> optimal, max 6
set AUDIO_BIT_RATE "96k" # the recommended bitrate for maximum compatability
set AUDIO_BUF_SIZE "$AUDIO_BIT_RATE" # for quality audio
set AUDIO_FILTER "highpass=f=200, lowpass=f=3000" # Try to isolate voice frequencies [av8r](https://superuser.com/a/835585)
set OUT_PIXEL_FORMAT yuv420p # Twitch requires this according to the Broadcast Requirments
# Setting our stream url.
set USERNAME "tomatrow" # Your Twitch username.
set SERVICE "twitch-stream-key" # The *service* we identify our key by.
set STREAM_KEY (security find-generic-password -a $USERNAME -s $SERVICE -w) # replace tomatrow with your username and twitch-stream-key with the service you set in keychain.
set SERVER "live-lax" # twitch server in LA, see http://bashtech.net/twitch/ingest.php for list
set URL "rtmp://$SERVER.twitch.tv/app/$STREAM_KEY"
# Passing our configuration to FFmpeg.
set GLOBAL_OPTIONS -hide_banner -loglevel info -thread_queue_size 512
set INPUT_FILE_OPTIONS -f avfoundation -capture_cursor 1 -capture_mouse_clicks 1 -video_size $INRES -framerate $FPS -pixel_format yuyv422
set INPUT_URL "$SOURCE"
set OUTPUT_VIDEO_OPTIONS -codec:v libx264 -r $FPS -threads $VIDEO_THREADS -g:v $GOP -keyint_min $GOPMIN -b:v $CBR -minrate $CBR -maxrate $CBR -s $OUTRES -preset $QUALITY -bufsize $VIDEO_BUF_SIZE -pix_fmt $OUT_PIXEL_FORMAT -f flv
set OUTPUT_AUDIO_OPTIONS -codec:a libmp3lame -ar $AUDIO_RATE -threads $AUDIO_THREADS -strict normal -b:a $AUDIO_BIT_RATE -async $AUDIO_RATE -bufsize $AUDIO_BUF_SIZE # -af $AUDIO_FILTER
set OUTPUT_FILE_OPTIONS $OUTPUT_VIDEO_OPTIONS $OUTPUT_AUDIO_OPTIONS
set OUTPUT_URL "$URL"
set ARGUMENTS $GLOBAL_OPTIONS $INPUT_FILE_OPTIONS -i $INPUT_URL $OUTPUT_FILE_OPTIONS $OUTPUT_URL
echo $ARGUMENTS
ffmpeg $ARGUMENTS
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment