1. Home
  2. Linux

How To Stream To Twitch From The Command Line On Linux

Many users on Linux choose to stream to Twitch on Linux using the Open broadcaster tool. Not everyone likes this tool, and some wish for an alternative. You can watch Twitch streams without using a browser, and you can use a simple bash script stream to Twitch.

If you use a VPN with Twitch, you will have to enable it separately.

Install FFmpeg Encoding Tool

Streaming to Twitch from the Linux command line runs with the help of the FFmpeg encoding tool. Before we go any further in this tutorial, you’ll need to install it on your Linux PC. Open up a terminal window and enter the following. To install FFmpeg, you must be running Ubuntu, Debian, Arch Linux, Fedora or OpenSUSE. If you aren’t running one of these Linux distributions, you can build and install the FFmpeg encoding software directly from the website here.

Ubuntu

sudo apt install ffmpeg

Debian

sudo apt-get install ffmpeg

Arch Linux

sudo pacman -S ffmpeg

Fedora

sudo dnf install ffmpeg

OpenSUSE

sudo zypper install ffmpeg

Now that the encoding tool is installed, run it in the terminal to be sure that everything is working correctly. FFmpeg doesn’t need file encoding to run. In a terminal, try:

ffmpeg --help

Confirm that the help page launches correctly, and type clear to erase the terminal.

Modify Bashrc

The encoder tool is installed and working. The next step is to set up the Twitch streaming alias on the system. To create the streaming alias, you’ll need to modify the bashrc file. This file holds a lot of different command variables for each user. The bashrc file works on a per-user basis, so each user that wants to use this command to stream to Twitch must follow the process below.

Start off by backing up the bashrc file. This will ensure that any edits or mistakes can be undone if you want to delete the streaming command. Create a backup by making a copy of the file:

mkdir ~/bashrc-backup

cp ~/.bashrc ~/bashrc-backup/.bashrc-bak

With the backup taken care of, start the editing. Open up bashrc in the terminal. DO NOT USE ROOT, you could accidentally edit the root user’s bashrc file instead of your own, which would be a mistake.

nano ~/.bashrc

Paste the following code at the very end of the bashrc file. Understand you may need to go through this code and edit it to suit your needs. Specifically, the resolutions, threads, quality, CBR,  and etc.

Note: do not fill out your stream key in bashrc, as it is unsafe. This script asks for the key every time you stream for safety sake.

 streaming() {
     INRES="1920x1080" # input resolution
     OUTRES="1920x1080" # output resolution
     FPS="15" # target FPS
     GOP="30" # i-frame interval, should be double of FPS, 
     GOPMIN="15" # min i-frame interval, should be equal to fps, 
     THREADS="2" # max 6
     CBR="1000k" # constant bitrate (should be between 1000k - 3000k)
     QUALITY="ultrafast"  # one of the many FFMPEG preset
     AUDIO_RATE="44100"
     STREAM_KEY="$1" # use the terminal command Streaming streamkeyhere to stream your video to twitch or justin
     SERVER="live-sjc" # twitch server in California, see http://bashtech.net/twitch/ingest.php to change 
     
     ffmpeg -f x11grab -s "$INRES" -r "$FPS" -i :0.0 -f alsa -i pulse -f flv -ac 2 -ar $AUDIO_RATE \
       -vcodec libx264 -g $GOP -keyint_min $GOPMIN -b:v $CBR -minrate $CBR -maxrate $CBR -pix_fmt yuv420p\
       -s $OUTRES -preset $QUALITY -tune film -acodec libmp3lame -threads $THREADS -strict normal \
       -bufsize $CBR "rtmp://$SERVER.twitch.tv/app/$STREAM_KEY"
 }

When the code is in bashrc, save the Nano text editor with Ctrl + O, and exit it with Ctrl + X.

Streaming

To stream to Twitch directly from the command line, open up a terminal and use the new streaming command set up in bashrc. You must know your Twitch stream key. Log into Twitch, go to the dashboard and find your streaming key.

If the key is set up correctly, streaming from the command line should work like this:

streaming streamkey

To quit streaming, press “Q” and it should end, as the stream is using FFmpeg. If the button doesn’t work, force the script to quit with Ctrl + Z.

Stream Script

If entering a stream key over and over gets exhausting, consider creating a stream script. Keep in mind that doing this will expose your key to anyone else that has access to your PC. To create the script, open terminal and enter the following commands.

First, use echo to add the shebang. A shebang lets the Bash interpreter know what kind of script it is about to run, the environment, etc.

Note: don’t move the script from your home folder! It relies on the streaming script inside of bashrc. If you move it to other places in the file system that don’t have the correct permissions, it may not work correctly.

echo "#!/bin/bash" > ~/stream-script.sh

Next, copy your streaming command and use echo to write it into the script.

echo "streaming streamkey" >> ~/stream-script.sh

Lastly, update the permissions of the script, so that the system will allow it to run correctly. Without this, the script may fail.

sudo chmod +x stream-script.sh

Run the script with:

./stream-script.sh

or

sh stream-script.sh

Quit the script with or Ctrl + Z.

Leave a comment

  • developer201

    streaming: command not found

    • Derrik Diener

      You’ll have to make sure that your shell detects it as an alias. Restart the terminal, or your PC after setting it up if you have to.