The following will explain the steps necessary to stream twitch.tv (as well as other media sites like YouTube) on the Raspbian (wheezy) distribution for the Raspberry Pi 2. While you should be able to accomplish the following instructions with Python 2.6.x, I recommend using 2.7.10+.
As with any Linux-based distribution its usually a good idea to make sure your system packages are updated:
sudo apt-get update && sudo apt-get upgrade -y
After doing the upgrade, especially if you do a dist-upgrade
, you may
encounter an oddity wherein startx
will load the desktop session,
however, the taskbar will be empty and flash in and out of existence
constantly. After trying a myriad of fixes, I was finally able to resolve this
issue by adding the line dtparam=audio=on
to the file
/boot/config.txt
as found towards the end of
this thread.
While not strictly necessary, if your RPi distribution came with a version of Python prior to 2.7.x, I recommend upgrading your Python installation for the greatest compatibility and making sure it is built with proper SSL support.
You can check which version of Python is installed by doing:
python --version
There is a great guide for building Python 2.7.10 on the RPi that can be found here.
If you do not already have pip
installed:
sudo apt-get install python-pip
Even if you do, make sure it is up to date:
sudo pip install -U pip
Make sure rtmpdump
is available:
sudo apt-get install rtmpdump
Optionally setup a virtualenv for the streaming packages: (see the virtualenv setup section for more information)
Install the livestreamer utility utilized for the actual streaming:
# sudo is not required when using a virtualenv
sudo pip install livestreamer
At this point you should be able to begin watching twitch.tv via:
livestreamer -np "omxplayer --adev hdmi" twitch.tv/cohhcarnage source
You can then end the stream with CTRL+C
.
Note: if you have problems with the video spilling out of the display
area and getting cut off, you can add the following to your
/boot/config.txt
file:
overscan_scale=1
This should force omxplayer to conform to your overscan settings which you can adjust to have the output fit nicely inside the display bounds.
Normally I would recommend VLC for almost any media, however, at the time
of writing, VLC is not properly able to support video on the RPi. That is
why the -p
option is used to tell livestreamer to utilize omxplayer
to display the stream. The quotes are necessary since arguments are being
passed to omxplayer which should ensure that the audio is also passed to
the HDMI output of the RPi. The source
argument can be substituted
with any other available stream quality (typically high
, medium
,
or low
) depending on your preference and bandwidth.
You can also optionally add a shortcut function to your .bash_profile
that will make opening streams more convenient:
function twitchtv() {
livestreamer -np "omxplayer --adev hdmi" twitch.tv/$1 source
}
This will allow you to open streams by using twitchtv
like a command:
twitchtv cohhcarnage
Install virtualenvwrapper:
sudo pip install virtualenvwrapper
Open your bash profile for editing:
cd ~ && nano .bash_profile
Add the lines below to file:
export WORKON_HOME="$HOME/.envs"
export PROJECT_HOME="$HOME/.projects"
source /usr/local/bin/virtualenvwrapper.sh
Optionally add the following alias shortcuts as well:
alias mkvenv="mkvirtualenv"
alias mkvenv3="mkvirtualenv -p python3"
alias rmvenv="rmvirtualenv"
alias lsvenv="lsvirtualenv"
Save the changes to .bash_profile
:
- CTRL+X
- y
- ENTER
If you do not already have a .bash_profile
configured, then you may need
to add the following to your .bashrc
file to make sure it is included:
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile
fi
Finally, make sure that your changes are made available to your current terminal session:
. ~/.bashrc
Hello! new here. I was wondering why did you install virtualenvwrapper. Is it neccesary? I read that it is to manage virtual environments. I haven't implemented this because I don't have a RPi yet.
Thanks!!