Tested:
ubuntu 13.04Recently I found myself needing to stream from my Ubuntu laptop to my twitch feed. Unfortunately almost every link/code snippet I could find online was either using unsupported/deprecated code or was full of syntax errors making it unusable. I’ve put together this simple tutorial for streaming to Twitch.tv with only single script to run.
The script (updated January 25 2013):
#! /bin/bash
# streaming on Ubuntu via ffmpeg.
# see http://ubuntuguide.org/wiki/Screencasts for full documentation
# input resolution, currently fullscreen.
# you can set it manually in the format "WIDTHxHEIGHT" instead.
INRES=$(xwininfo -root | awk '/geometry/ {print $2}'i)
# output resolution.
# keep the aspect ratio the same or your stream will not fill the display.
OUTRES="640x360"
# input audio. You can use "/dev/dsp" for your primary audio input.
INAUD="pulse"
# target fps
FPS="30"
# video preset quality level.
# more FFMPEG presets avaiable in /usr/share/ffmpeg
QUAL="fast"
# stream key. You can set this manually, or reference it from a hidden file like what is done here.
STREAM_KEY=$(cat ~/.twitch_key)
# stream url. Note the formats for twitch.tv and justin.tv
# twitch:"rtmp://live.twitch.tv/app/$STREAM_KEY"
# justin:"rtmp://live.justin.tv/app/$STREAM_KEY"
STREAM_URL="rtmp://live.twitch.tv/app/$STREAM_KEY"
ffmpeg \
-f alsa -ac 2 -i "$INAUD" \
-f x11grab -s "$INRES" -r "$FPS" -i :0.0 \
-vcodec libx264 -s "$OUTRES" -vpre "$QUAL" \
-acodec libmp3lame -threads 6 -qscale 5 -b 64KB \
-f flv -ar 44100 "$STREAM_URL"
What do I do with this? Follow these instructions:
- Create twitch_stream.sh. Download the script from here or copy the above raw code and paste it into a file called twitch_stream.sh in your home (~) directory. This will be something like: /home/YOU.
- Create .twitch_key. This script looks for the file ~/.twitch_key from which to read your twitch key. Go to http://www.twitch.tv/broadcast/dashboard/streamkey, copy your stream key and paste it into a file called .twitch_key in your home (~) directory.
- Make sure you have all the necessary packages installed. To install them run the following command:
sudo apt-get install ffmpeg libx264-dev libavcodec-extra-53
- Make sure twitch_stream.sh has the right permissions. From within your home (~) directory run the following command:
chmod 755 twitch_stream.sh
- Start streaming! From within your Home directory where you should have both the twitch_stream.sh and .twitch_key files run the following command in terminal:
sh twitch_stream.sh
If you don’t run into any issues this should start streaming right away. Check your twitch dashboard to see if this is working as expected.
NOTE – you will probably have to tweak the INRES and OUTRES to match your needs. By default the INRES is set to be your screens width. You probably want to make sure your INRES and OUTRES have the same aspect ratio.
I have also created a gist for this short script so it can be updated as needed that you can find here.
Possible Errors:
- stream.sh: 40: stream.sh: ffmpeg: not found
Solution – install ffmpeg using the following command:
sudo apt-get install ffmpeg
- Unrecognized option ‘preset’
Solution – install libx264-dev using the following command:
sudo apt-get install libx264-dev libavcodec-extra-53
- [rtmp @ 0xa39a20] Server error: Authentication Failed.
rtmp://live.twitch.tv/app/: Input/output error
Solution – get your twitch key again and copy paste it into the ~/.twitch_key file. You may have accidentally clicked reset stream key while copy pasting it.
If you run into any other error messages or dependency issues which streaming to Twitch.tv from Ubuntu please post them in the comments below. The more detail you can include the better! For reference I tested this on ubuntu 13.04.