Skip to content

Instantly share code, notes, and snippets.

@navanchauhan
Created July 23, 2022 03:05
Show Gist options
  • Save navanchauhan/6234c4855bc3d0aec05bd7a10c878462 to your computer and use it in GitHub Desktop.
Save navanchauhan/6234c4855bc3d0aec05bd7a10c878462 to your computer and use it in GitHub Desktop.
Stream Raspberry Pi to Twitch (Bullseye)
#!/bin/bash
# =================================================================
# Stream configuration file for Raspberry Pi Camera
#
# @author Russell Feldhausen ([email protected])
# @version 2019-06-05
#
# @author Navan Chauhan ([email protected])
# @version 2022-07-22
#
#
# Tested on Raspberry Pi Zero 2 W (Raspberry Pi OS (32 bit))
# =================================================================
# Set width and height of output video
# Run following command to get options:
# libcamera-vid --list-cameras
# Sample output:
# Available cameras
# -----------------
# 0 : ov5647 [2592x1944] (/base/soc/i2c0mux/i2c@1/ov5647@36)
# Modes: 'SGBRG10_CSI2P' : 640x480 [58.92 fps - (16, 0)/2560x1920 crop]
# 1296x972 [43.25 fps - (0, 0)/2592x1944 crop]
# 1920x1080 [30.62 fps - (348, 434)/1928x1080 crop]
# 2592x1944 [15.63 fps - (0, 0)/2592x1944 crop]
WIDTH=640
HEIGHT=480
# Set output framerate
FRAMERATE=30
# Set keyframe spacing (must be double the framerate)
KEYFRAME=60
# Set bitrate (Twitch recommends 3500000)
BITRATE=3500000
# Set stream URL
URL=
# Set stream key
KEY=
# Command
libcamera-vid -n --width $WIDTH --height $HEIGHT --bitrate $BITRATE --framerate $FRAMERATE -g $KEYFRAME --inline -o - | ffmpeg -f lavfi -i anullsrc -c:a aac -r $FRAMERATE -i - -g $KEYFRAME -strict experimental -threads 4 -vcodec copy -map 0:a -map 1:v -b:v $BITRATE -preset ultrafast -f flv "${URL}/${KEY}"
# =================================================================
# Full Documentation of Command Options
#
# +++ libcamera-vid +++
# -n = no preview window
# -width = video width
# -height = video height
# -framerate = output framerate (max 30 for 1080p, 60 for 720p)
# -bitrate = bitrate
# -g = keyframe rate (refresh period)
# -o - = output to stdout (allows piping to ffmpeg)
#
# +++ ffmpeg +++
# -f lavfi = use lavfi filter (see note below)
# -i anullsrc = grab blank input (see note below)
# -c:a aac = set audio codec to aac
# -r = output framerate (should match raspivid framerate)
# -i - = read input from stdin (piped from ffmpeg)
# -g = keyframe rate (refresh period)
# -strict experimental = allow nonstandard things
# -threads 4 = set number of encoding threads to 4 (# of cores)
# -vcodec copy = use video as-is (do not re-encode video)
# -map 0:a = use the audio from input 0 (see note below)
# -map 1:v = use the video from input 1 (raspivid)
# -b:v = bitrate
# -preset ultrafast = use the ultrafast encoding preset
# -f flv = set output format to flv for streaming
# "${URL}/{KEY}" = specify RTMP URL as output for stream
#
# ** NOTE **
# According to some information online, YouTube will reject a live
# stream without an audio channel. So, in the ffmpeg command above
# a blank audio channel is included. It was not required for Twitch
# in my testing.
# =================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment