Streaming to Twitch.tv from Ubuntu

Tested:

Recently 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:

  1. 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.
  2. 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.
  3. 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
  4. Make sure twitch_stream.sh has the right permissions. From within your home (~) directory run the following command:
    chmod 755 twitch_stream.sh
  5. 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.

Share on Google+2Share on Reddit0Share on StumbleUpon0Tweet about this on TwitterShare on Facebook2Digg this

    Thanks for this script! This looks like something I could use.

    However, when I try your example, I get the following error:
    [X11grab indev @ 0x1d05440] Unable to parse option value “1920×1080+0+0” as image size [X11grab indev @ 0x1d05440]
    Error setting option video_size to value 1920×1080+0+0.
    What can I do?

    full log here:
    http://paste.ubuntu.com/6514519/

    ricky v
    January 9, 2014 at 4:17 pm

    I Got this Part it Streams It says im Live but The Screen Is Gray ‘Loading Video ‘
    i Need to Get Something To Record onto twitch !

      Hmm, that sounds like it might be a network problem or twitch problem rather than an issue with the uploader. Check this reddit thread for more info on the problem as it does seem to happen to others and not just uploaders:
      http://www.reddit.com/r/Twitch/comments/1rlgeu/streams_get_stuck_on_loading_video/

      Edit: As discussed in the comments, this issue is also commonly caused by having the -pix_fmt yuv420p command or trying to use a ‘lossless’ -vpre. Try removing “-pix_fmt yuv420p” and switching your -vpre to “medium” and try again.

    NicktheGamer
    January 23, 2014 at 3:05 pm

    I have this same issue with loading my stream. I open it up on my computer, then I go to my channel, and it acknowledges that I’m live, yet there is no feed, I just get “Loading Video” thing for five minutes. I can go to other channels and view those, but mine is the only one that doesn’t show up. After a five minute stream test run, I could not get it to go on the channel, so I closed the terminal and went back to my Channel Page. I was able to watch the video of the stream in “Past Broadcasts” but really couldn’t get it to go live. I checked the page that was posted above but I believe those people are having a different issue. Any help would be appreciated, thanks!

      After some testing I managed to run into the same issue. Try to remove the following option:
      -pix_fmt yuv420p
      and run the script again again. Make sure to wait at least a few seconds for the stream to appear.

        NicktheGamer
        January 26, 2014 at 8:44 am

        The line “-pix_fmt yuv420p” you suggested to remove is not in my current script, or in the script that is posted on this page, though when I run the script terminal comes up with a message saying..
        “Incompatible pixel format ‘bgra’ for codec ‘libx264’, auto-selecting format ‘yuv420p'”
        I’m not sure if I am doing something wrong here, or if the posted script has been outdated or what, but despite trying a couple times my situation hasn’t changed. I am also running Ubuntu 13.10 on my quad-core 64bit AMD A8, if that helps, and I am not sure if there were some settings I need to have adjusted for that. I really have no experience with scripts, and I am new to Linux, so any more help would be greatly appreciated. Also, I have no idea if this is causing the issue, but when I start the script it says
        “*** THIS PROGRAM IS DEPRECATED ***
        This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
        [alsa @ 0x18f59a0] capture with some ALSA plugins, especially dsnoop, may hang.
        [alsa @ 0x18f59a0] Estimating duration from bitrate, this may be inaccurate”

        I have tried using avconv instead but am not really sure how that works or if the script needs to be adjusted for this. I have tried to include all the info involved with the problem, but if there is anything else I you need to know I’ll be glad to share that too. Thank you for all the help

          I just updated both the gist and this post to remove that line. If you don’t have it in your script it’s possible you got the script from somewhere else. There are a few fairly similar ones floating around. To answer your other questions questions:

          1. avconv is the successor to ffmpeg. I had issues using it which is why I chose ffmpeg instead. ffmpeg may still work though.
          2. The posts that I could find that had the line “Incompatible pixel format ‘bgra’ for codec ‘libx264′, auto-selecting format ‘yuv420p’” were suggesting that the issue was caused by trying to use a lossless preset for the -vpre option ( e.g. -vpre lossless_ultrafast). If you have a lossless preset try something like “fast” or “medium” instead

          If this doesn’t work, can you please send me both the script you are using (remove your stream key if you included it), and the full output of running it?

            NicktheGamer
            January 26, 2014 at 9:29 am

            I copied the new script again, and then changed the quality to “medium” and now the stream works! Thank you so much for the help!

            I’m glad to hear it worked!

    Sil
    February 2, 2014 at 4:01 am

    I’ve used a script much like this before, but it only seemed to stream the top-left of my screen. How can I set it to record where a window or game will be?

      I may be wrong, but I don’t think ffmpeg can natively record a process window like a game. You can change the input resolution to fully match your screen (so you don’t just record top left) either by doing:
      INRES=$(xwininfo -root | awk '/geometry/ {print $2}'i)
      or simply setting INRES manually to your screen resolution.

    I had the same problem : for me the solution was to change the QUAL variable to “ultrafast”. Hope that’ll help someone.

    June 26, 2014 at 12:20 pm

    Alex, thanks again for this article. I would like to include both microphone and applications sound in the stream, do you know how can I figure this out ?

Leave a Reply to Nickolai Leschov Cancel reply

Search
Questions!?
Play nice
>> All posts, unless specified otherwise, are original and created solely for The Game Engine. Please credit anything you use or reference from this site.
Was this site helpful?
>> The Game Engine is a non-profit personal project. Please consider a small donation so we can keep helping you with your games.