27
Jun 11
21:16
Justin.tv desktop streaming in linux
There are really no good tutorials for this that I could find, so here’s a tutorial on how to get justin.tv up on ubuntu. This should work with other linux distros. Before you start calculating the millions you’ll be making from ad revenue, lets first get you working with a stream. Then you can broadcast your sick rts skills or live coding sessions, as you see fit and rake in the dough.
http://apiwiki.justin.tv/mediawiki/index.php/Linux_Broadcasting_API
is the main site for how to do this, but it isn’t very detailed and doesn’t go over desktop streaming.
We’re going to use ffmpeg to stream the desktop and capture our audio as well.
1. First you need a justin.tv account, so go create one.
2.Then go to this page (while logged in) and click the ‘show’ link to view your stream key. This is sort of like a password, so don’t give it out.
3. Install ffmpeg if you don’t have it (you probably do,) but if you don’t for debian/ubuntu it’s the following:
sudo apt-get install ffmpeg libavcodec-extra-52
4.Start the stream.
INRES="1920x1080" # input resolution OUTRES="1024x576" FPS="20" # target FPS QUAL="fast" # one of the many FFMPEG preset STREAM_KEY=live_231xxxxxxxxx #get your stream key as described above.
Then run ffmpeg with
ffmpeg -f x11grab -s "$INRES" -r "$FPS" -i :0.0 -f alsa -ac 2 -i hw:0,0 -vol 4096 -vcodec libx264 -vpre "$QUAL" -s "$OUTRES" -acodec libmp3lame -ab 128k -threads 0 -f flv "rtmp://live.justin.tv/app/$STREAM_KEY flashver=FMLE/3.0\20(compatible;\20FMSc/1.0)"
I modified this command from a forum post that used the one below. My changes were to not use pulseaudio and to boost the volume to twice as much (256 is default, so adjust accordingly)
some people had success with pulseaudio, but my ubuntu/wine config must not be setup to use it.
If audio fails, try this command instead.
ffmpeg -f x11grab -s "$INRES" -r "$FPS" -i :0.0 -f alsa -ac 2 -i hw:0,0 -vcodec libx264 -vpre "$QUAL" -s "$OUTRES" -acodec libmp3lame -ab 96k -vol 4096 -ar 22050 -threads 0 -f flv "rtmp://live.justin.tv/app/$STREAM_KEY flashver=FMLE/3.0\20(compatible;\20FMSc/1.0)"
The above command uses your audio output (what comes out of your speakers) as the audio to be streamed. To record from a mic, you can use pulse (I think it is installed by default):
ffmpeg -f x11grab -s "$INRES" -r "$FPS" -i :0.0 -f alsa -ac 2 -i pulse -vcodec libx264 -vpre "$QUAL" -s "$OUTRES" -acodec libmp3lame -ab 96k -vol 4096 -ar 22050 -threads 0 -f flv "rtmp://live.justin.tv/app/$STREAM_KEY flashver=FMLE/3.0\20(compatible;\20FMSc/1.0)"
This command should start your stream on justin.tv.
I am not sure why my audio is 1/16th what it should be and I need to scale the volume that much.
Next tutorial will be about how to integrate a webcam into a desktop streaming setup.
Update: I installed the pulse audio device/volume selector, and then ran the second command which uses pulse. While the stream is open I need to change from duplex stereo to output analog only, then change back. It’s weird that this fixes the filtering/low volume issue, but it does get around it. I only needed to make the swap once and it all works great.
sudo apt-get install padevchooser
I have recently switched to youtube because justin.tv/twitch tv doesn’t support saving or transfering videos to youtube if the content is not games. So while I may still use twitch for starcraft 2 and live streaming, I now use the following very similar command to record to mp4, then upload. I find I get much better framerates like this.
This command records the file to my videos folder with a timestamp attached. It wouldn’t take much more work to automate this upload process.
ffmpeg -f x11grab -s "$INRES" -r "$FPS" -i :0.0 -f alsa -ac 2 -i pulse -vcodec libx264 -vpre "$QUAL" -s "$OUTRES" -acodec libmp3lame -ab 96k -vol 4096 -ar 22050 -threads 0 "$HOME/Videos/JLPTN1/cast720-$(date +%Y_%m_%d_%k_%M).mp4"
For some reason I needed $HOME instead of ~ to get the filepath to work.
Update2: I recently installed FFmpeg 0.8.10 which uses libavcodec 53.8.0, and this works for the mp4 capture, but fails silently with the rtmp output to justin.tv not going anywhere (Even after I take out the no longer valid -vpre and flashver=FMLE/3.0\20(compatible;\20FMSc/1.0) – if you leave the flashver in you get an error that says [rtmp @ 0x9be5fc0] Server error: Authentication Failed.
. My solution was to have two versions of ffmpeg – one compiled from source in /usr/local/bin/ and one using the safe libavcodec 52.72.2 – this is the one that apt-get gave me in /usr/bin/, and whenever I need justin.tv I use the older one.
ChavoDelOcho
September 16, 2011
10:55 am
Hello brother, im using linux mint which is ubuntu based and i did exactly as you posted but no results. The terminal dumped me this…
FFmpeg version 0.6.2-4:0.6.2-1ubuntu1, Copyright (c) 2000-2010 the Libav developers
built on Mar 22 2011 15:55:04 with gcc 4.5.2
configuration: –extra-version=4:0.6.2-1ubuntu1 –prefix=/usr –enable-avfilter –enable-avfilter-lavf –enable-vdpau –enable-bzlib –enable-libgsm –enable-libschroedinger –enable-libspeex –enable-libtheora –enable-libvorbis –enable-pthreads –enable-zlib –enable-libvpx –disable-stripping –enable-runtime-cpudetect –enable-vaapi –enable-gpl –enable-postproc –enable-swscale –enable-x11grab –enable-libdc1394 –enable-shared –disable-static
libavutil 50.15. 1 / 50.15. 1
libavcodec 52.72. 2 / 52.72. 2
libavformat 52.64. 2 / 52.64. 2
libavdevice 52. 2. 0 / 52. 2. 0
libavfilter 1.19. 0 / 1.19. 0
libswscale 0.11. 0 / 0.11. 0
libpostproc 51. 2. 0 / 51. 2. 0
[x11grab @ 0xd4b420]device: :0.0 -> display: :0.0 x: 0 y: 0 width: 1366 height: 768
[x11grab @ 0xd4b420]shared memory extension found
[x11grab @ 0xd4b420]Estimating duration from bitrate, this may be inaccurate
Input #0, x11grab, from ‘:0.0’:
Duration: N/A, start: 1316163217.471091, bitrate: 503562 kb/s
Stream #0.0: Video: rawvideo, bgra, 1366×768, 503562 kb/s, 15 tbr, 1000k tbn, 15 tbc
[alsa @ 0xd59600]Estimating duration from bitrate, this may be inaccurate
Input #1, alsa, from ‘hw:0,0’:
Duration: N/A, start: 49743.018340, bitrate: N/A
Stream #1.0: Audio: pcm_s16le, 44100 Hz, 2 channels, s16, 1411 kb/s
Unknown encoder ‘libx264’
Any clue of whats wrong?
mike
September 16, 2011
11:19 am
I think it is that you need some additional libraries. FFmpeg doesn’t come with x264 out of the box because of licensing issues.
First try:
sudo apt-get install ffmpeg libavcodec-extra-52
if it doesn’t work, more advice here:
http://ubuntuforums.org/archive/index.php/t-1117283.html
If it works please let me know so I can update the page.
ChavoDelOcho
September 16, 2011
7:37 pm
Thank you for your time, its working now
I made some fixes in the script avoding the command
flashver=FMLE/3.0\20(compatible;\20FMSc/1.0) -> dumping INPUT/OUTPUT error
This allowed me to start the streaming with very high quality video and fluently
Now brother i ask you again for help because if the stream is already open, i can’t start any game under wine
And if i open StarCraft 2 first, it dumps me this:
[x11grab @ 0x16e3640]shared memory extension found
[x11grab @ 0x16e3640]Estimating duration from bitrate, this may be inaccurate
Input #0, x11grab, from ‘:0.0’:
Duration: N/A, start: 1316193945.106542, bitrate: 503562 kb/s
Stream #0.0: Video: rawvideo, bgra, 1366×768, 503562 kb/s, 15 tbr, 1000k tbn, 15 tbc
[alsa @ 0x16f1820]cannot open audio device hw:0,0 (Device or resource busy)
hw:0,0: Input/output error
I tried to resolve changing the hw:0,0 command into 1:0 0:1 1:1 but none of this worked; dumping me // No such file or directory (…) input/output error
I know im close so i would be glad if you help me with this <3 🙂
ChavoDelOcho
September 16, 2011
8:00 pm
Oh and i used this script not the one from the forum
ffmpeg -f x11grab -s “$INRES” -r “$FPS” -i :0.0 -f alsa -ac 2 -i hw:0,0 -vol 4096 -vcodec libx264 -vpre “$QUAL” -s “$OUTRES” -acodec libmp3lame -ab 128k -threads 0 -f flv “rtmp://live.justin.tv/app/$STREAM_KEY
btw sometimes it gives me black screen
mike
September 18, 2011
1:28 pm
Re: the audio error, it seems like your starcraft might be using an exclusive audio stream, but I’m really not sure, sorry. When setting up starcraft under wine via winecfg, I added some library overrides related to audio. This caused me to have some audio problems (even without streaming). I removed those and it made the audio work perfectly. I don’t know if this is related to your problem but worth a shot.
Re: the black screen, I believe it happens when you can’t upload or encode fast enough, so try to set $FPS to be a lower value.
shadesoflight
October 22, 2011
6:32 am
I get an authentication error until I delete the bit about flash version in the third command (using pulse)
I have it running (using medium preset instead of fast) I keep getting random glitchies in the stream where it blinks to a previous frame. Any thoughts why/
mike
November 4, 2011
9:28 pm
shadesoflight: thanks for posting your fix. I don’t know what the glitches are but try rendering to disk first using the last command to .mp4 and see if they still exist. If it does, the problem may be your upload speed capping the encoding and glitching.
benny
January 16, 2012
7:25 pm
nice post…thanks
one question… let say if I can broadcast just *PART* of my screen… (eg: only upper-right corner, 640×480 size and my current desktop is 1024×768)
thanks
mike
January 16, 2012
9:22 pm
Yes, you can. I do this when I cast programming sessions on: http://www.youtube.com/user/fstori.
In this case just set INRES and OUTRES to 640×480 (don’t forget FPS and the other variables) like this before you run ffmpeg:
INRES=640×480
OUTRES=640×480
Makotona
May 14, 2012
6:52 pm
This tutorial is so useful to me.
I am extremely thank to you.
This is a great article,in summary. 🙂
Naib
June 8, 2012
4:59 am
No good tutorials..
you have copied line for line and using the same variables as my tutorial, ie it was copied from me. Its one thing to utilise what I have detailed but to then palm it off as your own is just fucking low…
http://forums.heroesofnewerth.com/showthread.php?229469-HOWTO-Streaming-in-Linux
mike
June 8, 2012
10:33 am
Dear Naib,
Sorry to hear you in a bad mood over something on the internet, but I agree your point needs to be addressed.
Please see the line where I say “I modified this command from a forum post”. I’ve never claimed that I wrote that stuff from scratch.
Also, there were a number of sources that use the same variables, (attaching below so you can see,) so it wasn’t clear who to attribute to. I don’t think I saw your post, I just saw one of these forum links which are also copy paste with no attribution (but they mention that it’s from a forum too.)
http://ubuntuforums.org/showthread.php?t=1953527
http://community.justin.tv/forums/showthread.php?t=9290
Since you are the first to claim ownership of the code I’m linking to you.
polivka
July 7, 2012
7:23 am
Simple & great tutorial, thanks!
Xaero
October 18, 2012
1:20 pm
Hello, I was wondering if you are still able to use FFMpeg to stream to twitch.tv, every time I try streaming using any configuration using libx264, my stream remains black but I do get audio.