Skip to content

Instantly share code, notes, and snippets.

@neuroticnerd
Last active November 12, 2022 08:12
Show Gist options
  • Save neuroticnerd/151aa9725f2231f8efbc5a5df53a8700 to your computer and use it in GitHub Desktop.
Save neuroticnerd/151aa9725f2231f8efbc5a5df53a8700 to your computer and use it in GitHub Desktop.
Streaming twitch.tv on your Raspberry Pi 2

Streaming twitch.tv on your Raspberry Pi 2

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+.

System Updates

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.

Updating Python

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.

Installing Requirements

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

Begin Streaming twitch.tv

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

virtualenv setup

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
@erik339
Copy link

erik339 commented May 23, 2017

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!!

@begs
Copy link

begs commented Nov 22, 2017

Great guide.
I made a script that checks your followed channels if they are live or not. I found it very useful with my Pi.
https://github.com/begs/livestreamers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment