Labels

Monday, September 9, 2013

Record or Stream Desktop & Games (Ubuntu 13.04)


In my adventure to get desktop recording and live streaming working under Linux I have come across some great resources and information around the interwebs. This is an attempt to compile and present what I think is the best and easiest methods of doing these tasks at the time of this post. The instructions I give are specifically for Ubuntu but could work if you know how to follow these steps in your distro.


** Update: Video can be found here -> Tutorial Video

Download Software

Before you get started lets download the software.

PulseAudio Volume Control


1. Open up the Ubuntu Software Center and search for "PulseAudio Volume Control".

2. Install it.

Download and install PulseAudio Volume Control.

SimpleScreenRecorder

1. Open up terminal.

2. Add the SimpleScreenRecorder PPA by typing the following:
sudo add-apt-repository ppa:maarten-baert/simplescreenrecorder
3. Update  package lists and repositories by typing the following:
sudo apt-get update
4. Install SimpleScreen recorder by typing the following:
sudo apt-get install simplescreenrecorder
5. It should be installed now.   Mine automatically added it to the launcher.  If yours isn't on the launcher just push the super key (windows key) and type pulseaudio and you should see it then.

Sound Settings

Now we have to change your recording device so that you can record system and microphone audio.  By default only the microphone will be recorded.

1. Open up PulseAudio Volume Control and click the "Recording" tab.  They're probably isn't anything there.

2. To see a recording device we will have to open a program that is actively recording.  Open up SimpleScreenRecorder.

3. Click Continue

4. Leave everything default for now and click continue again.

5. Continue again.

6. At the top there's a button that says "Start Recording".  Click it.

7. Now go back to PulseAudio Volume Control and you should now see a device recording.  Next to "ALSA Capture from" change the drop box to "Monitor of xxxxxxx".  The x's being whatever your default sound device is.  As you can see in the picture below my default audio is the "Built-in Audio Analog Stereo".

Choose to monitor your default sound device.

8. Now go back to SimpleScreenRecorder and click the button at the very top that says "Pause Recording".

9.  Now close SimpleScreenRecorder.  You will get a dialogue box saying that you haven't saved your current recording.  We don't want to keep it right now so just click the yes button.

10. Now go to PulseAudio Volume Control and the Input tab.  Be sure that your primary or default microphone is set to default by clicking the green button next to it.  You should be able to get mic levels by talking into your mic now.

Set default mic by clicking green check mark.

11. Now you'll need to loopback your mic into your speakers so that you can record both at once.  Open up terminal.

12. Type in the command to load loopback.  When you're done to turn the mic back off type in the command to unload loopback.

Load loopback:
pactl load-module module-loopback

Unload loopback: 
pactl unload-module module-loopback 
Note1: Once you load loopback you'll hear your microphone through your speakers.  You might want to use headphones so that you don't get that annoyingly a loud screeching feedback.

Note2: Works best with the default onboard audio. Do not have it working with my audio interface (Mbox Mini)  yet.  It may work with direct USB devices like USB mics or headphones.

Record Desktop

(If you're reading this just to stream skip down to "Live Stream To Twitch" section.)

1. Open up SimpleScreenRecorder

2. Click Continue.


3. Select the area of the screen you want to record.  I haven't had the chance to use the OpenGL option for games so I'll leave that alone for now.  Everything on this screen should be self explanatory.  Ideal frame rate should be 30 fps.  This will look fine on 60 hz monitors and YouTube.  Everything else is up to you.  When done click continue.


4. Set the location you want to save your output file.  This is where the video will be when your'e done recording.  I would leave the audio and video settings on this screen at default unless you know what your doing.  Click continue.



5. To start recording, click the "Start Recording" button at the top.


6. When your done recording then click the "Pause Recording" button.


7. Now save your recording by clicking "Save recording".


8. To see your recording open up the file browser and navigate to where you saved your files in step 4.

Live Stream To Twitch

1. Open up the text editor, then copy and paste the script below into it.  Do not copy the dashes at the beginning and end.

----------------------Twitch_Script--------------------------------------------

#! /bin/bash

# originaly from http://tinyurl.com/twitch-linux from taladan
# www.youtube.com/user/taladan

# gist created by brodul

INRES="1280x800"                                            # input resolution
#OUTRES="1024x640"                                           # Output resolution
OUTRES="800x500"                                           # Output resolution
FPS="30"                                                    # target FPS
QUAL="medium"                                               # one of the many FFMPEG preset on (k)ubuntu found in /usr/share/ffmpeg
# If you have low bandwidth, put the qual preset on 'fast' (upload bandwidth)
# If you have medium bandwitch put it on normal to medium

# Write your key in a file named .twitch_key in your home directory
STREAM_KEY=$(cat ~/.twitch_key)   # This is your streamkey generated by jtv/twitch found at: http://www.justin.tv/broadcast/adv_other

avconv \
    -f x11grab -s $INRES  -r "$FPS" -i :0.0 \
    -f alsa -ac 2 -i pulse  \
    -vcodec libx264 -s $OUTRES -preset $QUAL \
    -acodec libmp3lame -ar 44100 -threads 6 -qscale 3 -b 712000  -bufsize 512k \
    -f flv "rtmp://live.justin.tv/app/$STREAM_KEY"

---------------------------------------------------------------------------------------------------------------------------------

Note1: Change your INRES= value to whatever your screen resolution is.  Change the OUTRES= value to what you want to stream to.  I use 1280x720 because its still HD but easier to stream then 1080p.

Note2: Where it says "-threads" change this number to the number of cores you have. I have a i5 which is quad core.  So I changed my value to 4.

Note3: Where it says 712000, that's your upload rate. If you have a stable 1 Mb up then you would want to change this to 1000000.  You can play  with this to find your best stream quality.  Just leave it to default if your not sure.

2. Save the file as twitch.sh on your desktop.

3. Right click on the icon for twitch.sh that's now on your desktop.  Go to properties.  Select the "Permissions" tab.  Tick the box at the bottom that says "Execute:".

4. Go to www.twitch.tv/broadcast.  Click the "show key" button at the upper right area.


5. A alpha numeric string will appear.  You need to copy all of it.

6. Now open a new text file. Paste the string you just copied into it.

7. Save the file into your home folder as ".twitch_key".

8. Close all the text files being sure that they're saved correctly.

9. Be sure you have the right system files to stream with.  Go to terminal and type:

sudo apt-get install libav-tools

9. Go to terminal and type:
cd Desktop
./twitch.sh
10. You should now be able to check your Twitch channel and see that your streaming.

11. To stop streaming press Ctrl-C or close the terminal window.

Finish

Keep in mind that streaming and even recording your desktop requires a good amount of overhead from your computer.  On my Intel core i5 while streaming, I see a CPU usage of around 50% per core.  This is before I launch the game.  So you want to keep in mind that you will want a more recent PC and decent cooling because long periods of high CPU usage will run it on the hot side.  For the screen recording you will want a 7,200 rpm drive and preferably record to a completely different one if possible.

That's it.  It may seem complicated at a glance but once you've done it once and wrapped your head around it, you'll see that's it not.  If you have any questions or thoughts, leave them in the comments section.

Video Coming Soon . . .
--------------------------------------------------------------------------------------------------------------------------
Sources:


Subreddit Linux_Gaming

Linux GameCast

Andraz Brodnik (script)

Maarten Baert (screenrecorder)

Twitch TV (streaming)
--------------------------------------------------------------------------------------------------------------------------

14 comments:

  1. Do you know what I'm looking at here? It's the output from twitch.sh but I cannot figure out what it's trying to tell me...

    ---

    avconv version 0.8.10-6:0.8.10-0ubuntu0.13.10.1, Copyright (c) 2000-2013 the Libav developers
    built on Feb 6 2014 20:53:28 with gcc 4.8.1
    [x11grab @ 0x1225980] device: :0.0 -> display: :0.0 x: 0 y: 0 width: 1366 height: 768
    [x11grab @ 0x1225980] shared memory extension found
    [x11grab @ 0x1225980] Estimating duration from bitrate, this may be inaccurate
    Input #0, x11grab, from ':0.0':
    Duration: N/A, start: 1392591108.631253, bitrate: 1007124 kb/s
    Stream #0.0: Video: rawvideo, bgra, 1366x768, 1007124 kb/s, 30 tbr, 1000k tbn, 30 tbc
    [alsa @ 0x1231300] capture with some ALSA plugins, especially dsnoop, may hang.
    [alsa @ 0x1231300] Estimating duration from bitrate, this may be inaccurate
    Input #1, alsa, from 'pulse':
    Duration: N/A, start: 1392591108.650075, bitrate: N/A
    Stream #1.0: Audio: pcm_s16le, 48000 Hz, 2 channels, s16, 1536 kb/s
    Unknown encoder 'libx264'

    ReplyDelete
    Replies
    1. You are missing the x264 encoder. Install it and it should be okay.

      Delete
    2. Yeppers -- all set. Two ways to do this. Make sure you apt-get the -dev version x264 if you want it to be linked to ffmpeg/avconv.

      But this is much better:

      https://trac.ffmpeg.org/wiki/UbuntuCompilationGuide

      Delete
  2. im getting this error [rtmp @ 0x218b740] Server error: Authentication Failed.
    rtmp://live.justin.tv/app/: Input/output error

    ReplyDelete
    Replies
    1. Sounds like the wrong twitch key. Double check that.

      Delete
  3. I'm getting this error. I'm not sure if it's the same error as Christopher's because it's a little different at the end.
    avconv version 0.8.10-6:0.8.10-0ubuntu0.13.10.1, Copyright (c) 2000-2013 the Libav developers
    built on Feb 6 2014 20:53:28 with gcc 4.8.1
    [x11grab @ 0xbca980] device: :0.0 -> display: :0.0 x: 0 y: 0 width: 1080 height: 1920
    [x11grab @ 0xbca980] shared memory extension found
    X Error of failed request: BadMatch (invalid parameter attributes)
    Major opcode of failed request: 130 (MIT-SHM)
    Minor opcode of failed request: 4 (X_ShmGetImage)
    Serial number of failed request: 11
    Current serial number in output stream: 11

    ReplyDelete
  4. I have this error can you help me pleaze ?

    [rtmp @ 0x96f0fc0] Server error: Connection failed: Application rejected connection.
    rtmp://live.justin.tv/app/PK
    �pD^�2
    'mimetypeapplication/vnd.oasis.opendocument.textPK
    �pD�>��� � Thumbnails/thumbnail.png�PNG

    IHDR� zA�� �IDATx���}L�u����p< p�% ��<8MA��@Ň&ji�r�\m��* k�֣[�?����a+]��
    3 ҙ�O(�S E Q�A�� � ���l�Oik�x����������w���zM�}���7��4��B P�
    }@�(� �>�� ��B P�
    Ϙ�j7�=a] �� ~0 {��� � ��n?��S�� �G̣]�|Ј d�m�m
    }��� �:.W]\^�+��se� ����{�{ C� �����O ����y��i!��[��
    S�\O^������+�r� i߹tc��9�I�b� c��^���� a��ʽ߬��J~� �)r��a]�Iz�wm� X��vl?���{H��UU�H���
    �s��<ȳ����� O^i޺tC�g���c'�
    뫓{�Z�?๨� _~�6o��Z� �:GL��=����ڼM�
    �6�=�#<�Ѿ�PU����a
    ����98f������������ �[�]�C< m��]M�A)c�F9 �3��K����< X��Ȝ�^Sk�cm��xT��.�Y]\ :���r�9��� � ã}Z
    gj�|Z ��JLj U'z���x<')�2�D��̅ �32�]w�� ����D@Ku}�-,��d� - �?&��v��·1!%�b�֢���9>� ��e�CS����S<��c'�X

    ReplyDelete
  5. i get...
    Unrecognized option 'preset'
    Failed to set value 'medium'
    what do i do?

    ReplyDelete
    Replies
    1. I did a new video that makes it much easier to stream using software instead of this script. Check it out here. https://www.youtube.com/watch?v=OyCDzN_AZGw&list=UUgA40p8UnIA2-jjFHyicK5A

      Delete
    2. This comment has been removed by the author.

      Delete
  6. i keep getting this error someone plz help
    [x11grab @ 0x15c4cc0] device: :0.0 -> display: :0.0 x: 0 y: 0 width: 1280 height: 800
    No protocol specified
    [x11grab @ 0x15c4cc0] Could not open X display.
    :0.0: Input/output error

    ReplyDelete
    Replies
    1. I did a new video that makes it much easier to stream using software instead of this script. Check it out here. https://www.youtube.com/watch?v=OyCDzN_AZGw&list=UUgA40p8UnIA2-jjFHyicK5A

      Delete
  7. mine seems to streaming but there isn't any video and I get
    Incompatible pixel format 'bgra' for codec 'libx264', auto-selecting format 'yuv420p'

    ReplyDelete