If you want to start live streaming on Twitch using a Raspberry Pi, the process is very straightforward.
You need to have Raspberry Pi OS installed and configured on the Raspberry Pi.
1. Install ffmpeg
First, make sure you have ffmpeg
installed on your Raspberry Pi. You can install it using the following command:
sudo apt update
sudo apt install ffmpeg
2. Connect your webcam
Connect your webcam to the Raspberry Pi via USB.
3. Identify the Camera Device
Identify the device name for your camera by running the following command:
codev4l2-ctl --list-devices
You should see something like /dev/video0
if your camera is recognized as /dev/video0
.
4. Obtain Your Twitch Stream Key
Find your “Primary Stream Key”. Copy this key as you’ll need it for your ffmpeg
command.
- Log in to your Twitch account.
- Go to the Twitch Dashboard.
- Navigate to the “Stream” section.
5. Start Streaming To Twitch
Webcam Stream Only
Replace YOUR_TWITCH_STREAM_KEY
with your actual Twitch stream key in the following ffmpeg
command:
ffmpeg -f v4l2 -thread_queue_size 1024 -input_format mjpeg -framerate 30 -video_size 1920x1080 -i /dev/video0 \
-f alsa -thread_queue_size 1024 -i hw:2,0 \
-vcodec libx264 -b:v 4500k -maxrate 4500k -bufsize 9000k -preset veryfast -g 60 -keyint_min 60 \
-f flv rtmp://live.twitch.tv/app/Your_Twitch_Key
Run this command in your terminal to start streaming directly to Twitch.
Example Command Breakdown
rtmp://live.twitch.tv/app/YOUR_TWITCH_STREAM_KEY
: The RTMP URL for Twitch, including your unique stream key.
-f v4l2
: Specifies the input format as Video4Linux2 (used for capturing video from devices on Linux).
-input_format mjpeg
: Specifies that the input format from the camera is MJPEG.
-framerate 30
: Sets the frame rate to 30 fps.
-video_size 1920x1080
: Sets the video resolution to 1920×1080 (Full HD).
-i /dev/video0
: Specifies the input device (your camera).
-vcodec libx264
: Uses the x264 video codec for encoding.
-preset
veryfast: Sets the encoding speed preset to veryfast (lower latency, medium CPU usage).
-f flv
: Specifies the output format as FLV (Flash Video).
Webcam Stream + Overlay
To add an Overlay, use this command:
ffmpeg -f v4l2 -thread_queue_size 1024 -input_format mjpeg -framerate 30 -video_size 1920x1080 -i /dev/video0 \
-i /PATH_TO_OVERLAY.png \
-filter_complex "[0:v]scale=1294:723[cam];[1:v][cam]overlay=90:255" \
-vcodec libx264 -b:v 4500k -maxrate 4500k -bufsize 9000k -preset veryfast -g 60 -keyint_min 60 \
-f flv rtmp://live.twitch.tv/app/Your_Twitch_Key
Webcam Stream + Overlay + Microphone Sound
To add the microphone sound, use this command:
ffmpeg -f v4l2 -thread_queue_size 1024 -input_format mjpeg -framerate 30 -video_size 1920x1080 -i /dev/video0 \
-f alsa -thread_queue_size 1024 -i hw:2,0 \
-i /PATH_TO_OVERLAY.png \
-filter_complex "[0:v]scale=1294:723[cam];[2:v][cam]overlay=90:255" \
-vcodec libx264 -b:v 4500k -maxrate 4500k -bufsize 9000k -preset veryfast -g 60 -keyint_min 60 \
-filter:a "volume=0.5" \
-acodec aac -b:a 160k -ar 44100 \
-f flv rtmp://live.twitch.tv/app/Your_Twitch_Key
Webcam Stream + Overlay + Microphone Sound + Music Radio Stream
To add a radio stream, use this command:
ffmpeg -f v4l2 -thread_queue_size 1024 -input_format mjpeg -framerate 30 -video_size 1920x1080 -i /dev/video0 \
-f alsa -thread_queue_size 1024 -i hw:2,0 \
-i /PATH_TO_OVERLAY.png \
-i YOUR_STREAM_URL_HERE \
-filter_complex "[0:v]scale=1294:723[cam];[2:v][cam]overlay=90:255[final];[1:a]volume=0.4[a1];[a1][3:a]amix=inputs=2:duration=first:dropout_transition=3[aout]" \
-map "[final]" -map "[aout]" \
-vcodec libx264 -b:v 4500k -maxrate 4500k -bufsize 9000k -preset veryfast -g 60 -keyint_min 60 \
-acodec aac -b:a 160k -ar 44100 \
-f flv rtmp://live.twitch.tv/app/Your_Twitch_Key
Webcam Stream + Overlay + Microphone Sound + Music Radio Stream
To optimize the stream, use this command:
ffmpeg -f v4l2 -thread_queue_size 1024 -input_format mjpeg -framerate 24 -video_size 1280x720 -i /dev/video0 \
-f alsa -thread_queue_size 1024 -i hw:2,0 \
-i /PATH_TO_OVERLAY.png \
-i YOUR_STREAM_URL_HERE \
-filter_complex "[0:v]scale=1294:723[cam];[2:v][cam]overlay=90:255[final];[1:a]volume=0.4[a1];[a1][3:a]amix=inputs=2:duration=first:dropout_transition=3[aout]" \
-map "[final]" -map "[aout]" \
-vcodec libx264 -b:v 2500k -maxrate 2500k -bufsize 5000k -preset superfast -g 60 -keyint_min 60 \
-acodec aac -b:a 128k -ar 44100 \
-f flv rtmp://live.twitch.tv/app/Your_Twitch_Key
Run FFmpeg in a Screen or Tmux Session
Use screen
: Run ffmpeg
in a screen
session so it continues running even if your SSH connection is interrupted.
Install screen
:
sudo apt install screen
Start a new screen session
screen -S stream
Run your ffmpeg
command inside the screen
session. To detach from the screen
session, press Ctrl+A
, then D
. To reattach, use:
screen -r stream