This content on this page was written a long time ago. It might not be accurate or has opinions which have changed/evolved over time.

Streaming to twitch.tv from Linux

I was looking to stream to twitch.tv from Linux, but none of the existing solutions were embedding the webcam over the captured desktop. Since twitch.tv works with rtmp, and existing scripts were using ffmpeg, it was easy to modify the script to add the camera as an overlay. Here’s the source for anyone interested:

#!/bin/bash

API_KEY="live_..."
FPS="30"
INRES="1920x1200"
OUTRES="640x400"

ffmpeg -f x11grab -s "$INRES" -r "$FPS" -i :0.0 \
-f alsa -ac 2 -i default -vcodec libx264 -s "$OUTRES" \
-acodec libmp3lame -ab 128k -ar 44100 -threads 0 \
-vf "movie=/dev/video0:f=video4linux2, scale=120:-1, setpts=PTS-STARTPTS [movie];[in] setpts=PTS-STARTPTS, [movie] overlay=main_w-overlay_w:main_h-overlay_h [out]" \
-f flv  "rtmp://live.twitch.tv/app/$API_KEY"

How to use:

Copy the code and paste it into a file “stream.sh”. Edit in your API_KEY, then run it as “bash stream.sh”.

A few notes:

  1. Change fps, input resolution, output resolution to match your setup. If you have slow upload, keep FPS and OUTRES low.
  2. Change the last part of the command line (rtmp://…) to a file name to record screencasts.