1

I would like to stream from Ubuntu to Twitch in a lightweight manner from Terminal.

I have tried https://gist.github.com/brodul/3178114 however I get a green screen when I go to my Twitch stream. There is no error output; everything seems fine except for a few errors.

This is not a duplicate question because the newest version of this question is 3 years old and no longer applicable.

I've tried running the following script:

STREAM_KEY=$(cat ~/.twitch_key)   # This is your streamkey

ffmpeg -f x11grab -video_size 1280x720 -framerate 60 -i :0.0 -f pulse -i default
  -c:v libx264 -preset fast -b:v 4500k -bufsize 9000k -maxrate 5000k -g 120
  -pix_fmt yuv420p -c:a aac -b:a 128k -f flv "rtmp://live.twitch.tv/app/$STREAMKEY"

however I run into the following error:

nave01314@nave01314-All-Series:~$ ./start_stream.sh
ffmpeg version 2.8.11-0ubuntu0.16.04.1 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 20160609
  configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Input #0, x11grab, from ':0.0':
  Duration: N/A, start: 1516751285.842006, bitrate: N/A
    Stream #0:0: Video: rawvideo (BGR[0] / 0x524742), bgr0, 1280x720, 60 fps, 60 tbr, 1000k tbn, 60 tbc
Guessed Channel Layout for  Input Stream #1.0 : stereo
Input #1, pulse, from 'default':
  Duration: N/A, start: 1516751285.857784, bitrate: 1536 kb/s
    Stream #1:0: Audio: pcm_s16le, 48000 Hz, 2 channels, s16, 1536 kb/s
At least one output file must be specified
./start_stream.sh: line 4: -c:v: command not found
./start_stream.sh: line 5: -pix_fmt: command not found
  • Please don't edit questions so that existing answers to the old question become invalid answers for the new question. Instead, ask a new question. – Chai T. Rex Jan 24 '18 at 7:52
1

Try this:

ffmpeg -f x11grab -video_size 1280x720 -framerate 60 -i :0.0 -f pulse -i default \
  -c:v libx264 -preset fast -b:v 4500k -bufsize 9000k -maxrate 5000k -g 120 \
  -pix_fmt yuv420p -c:a aac -b:a 128k -f flv "rtmp://foo/your_stream_key"
  • View the console output during the encoding process. If the output fps is lower than -framerate then decrease -framerate and/or use a faster -preset. Use the slowest -preset that still provides the desired output frame rate.

  • -b:v is roughly determined by -video_size. See Figure 3 in Recommended Encoding Settings for HTTP Live Streaming Media for some suggested bitrates (but do experiment and see what works best for you). -bufsize is determined by -b:v. In this example a 2 second buffer is used so -bufsize is 2x -b:v.

  • -g should be 2x -framerate.

  • If you get "The encoder 'aac' is experimental but experimental codecs are not enabled, add '-strict -2' if you want to use it" then your ffmpeg is ancient and you should update instead of using -strict -2. The FFmpeg AAC encoder has been improved and is no longer considered experimental, but your version lacks these improvements. The easiest method to update is to download a recent ffmpeg binary and move it to /usr/local/bin (or ~/bin but you'll need to re-login or run source ~/.profile for it to work).

Also see:

1

You can't just put one command on multiple lines. You get errors like this if you do:

./start_stream.sh: line 4: -c:v: command not found
./start_stream.sh: line 5: -pix_fmt: command not found

You need to put \ on the end of each line where the next line is a continuation of this line, like this:

STREAM_KEY=$(cat ~/.twitch_key)   # This is your streamkey

ffmpeg -f x11grab -video_size 1280x720 -framerate 60 -i :0.0 -f pulse -i default \
  -c:v libx264 -preset fast -b:v 4500k -bufsize 9000k -maxrate 5000k -g 120 \
  -pix_fmt yuv420p -c:a aac -b:a 128k -f flv "rtmp://live.twitch.tv/app/$STREAMKEY"

You also need to change $STREAMKEY to match the underscore in the middle of STREAM_KEY at the beginning.

  • @EvanWeissburg See my updated answer regarding -strict -2. – llogan Jan 24 '18 at 1:38

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.