5

I've been trying for the last hour or so to stream to Twitch.tv with desktop audio in the stream. Currently, I'm able to use this to get a video to display on Twitch:

#! /bin/bash

INRES="1280x800"             # input resolution
OUTRES="640x480"             # Output resolution
FPS="30"                     # target FPS
QUAL="medium"                # one of the many FFMPEG presets
STREAM_KEY=$(cat ~/.twitch_key)

ffmpeg \
    -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"

What I want this to do is along with my microphone, I need the audio that my computer is playing (e.g. the audio from a YouTube video) to upload to the stream as well.

I'm also open to other options, but I haven't been able to find any as of yet.

2 Answers 2

2

You will first need to setup ALSA correctly by loading the snd_aloop module and using its named device in an mdev plugin. You will simultaneously output an application's audio through the loop back device and another device of your choice.

# ~/.asoundrc

pcm.!default {
  type plug
  slave.pcm mdev
  route_policy duplicate
}

pcm.mdev {
  type multi

  slaves.a.pcm "hw:Loopback,0,0"
  slaves.a.channels 2
  slaves.b.pcm "hw:0,0"
  slaves.b.channels 2

  bindings.0.slave a
  bindings.0.channel 0
  bindings.1.slave a
  bindings.1.channel 1
  bindings.2.slave b
  bindings.2.channel 0
  bindings.3.slave b
  bindings.3.channel 1
}

pcm.loopback {
  type hw
  card Loopback
  device 1
  subdevice 0
}

pcm.mic {
  type hw
  card 1
  device 0 
} 

FFMPEG can merge multiple audio streams using the amerge filter. You will provide it with the loop back source and you may provide additional inputs. This example includes a microphone. This script is setup for streaming to TwitchTV.

#!/bin/bash

FPS="30"
QUAL="faster"
GOP="60"
GOPMIN="30"
CBR="1500k"
ABR="96k"
SIZE="960x540"
OUTPUT=""rtmp://live-jfk.twitch.tv/app/${STREAM_KEY}"

function GetWindowPos() {
  echo $(xwininfo | grep 'Absolute' | awk '{ORS=" "; print $4;}' | awk '{print ":0.0+"$1","$2"+draw_mouse=0";}')
}

function GetWindowSize() {
  echo $(xwininfo | grep 'Width\|Height' | awk '{ORS=" "; print $2;}' | awk '{print $1"x"$2;}')
}

ffmpeg \
  -f alsa -i mic \
  -f alsa -i loopback \
  -f x11grab -s $(GetWindowSize) -i $(GetWindowPos) \
  -acodec aac -ab "$ABR" -strict -2 \
  -vcodec libx264 -preset "$QUAL" \
  -g "$GOP" -keyint_min "$GOPMIN" \
  -b:v "$CBR" -minrate "$CBR" -maxrate "$CBR" -bufsize "$CBR" \
  -filter_complex "[2:0]format=pix_fmts=yuv420p[vstream];[0:0][1:0]amerge=inputs=2[astream]" \
  -flags:v +global_header -flags:a +global_header \
  -r "$FPS" -s "$SIZE" \
  -map "[vstream]" -map "[astream]" \
  -f flv "$OUTPUT"

This would be an order of magnitude easier to use PulseAudio, but I often experience latency using the monitor sources, which is terrible for live streaming. If you are latency free on PulseAudio you could simply switch out the ALSA devices for two pulse inputs.

-f alsa -i mic \
-f alsa -i loopback \

These would change to:

-f alsa -i pulse \
-f alsa -i pulse \

Then you would set the inputs in pavucontrol.

1

Attempt #2

(ALSA method worked, but audio is quiet!)

Found these 2 methods in a blog post titled: Broadcasting to twitch.tv using ffmpeg.

First set these variables like so:

SIZE=”1600×1200″
BITRATE=”128k”
FRAMERATE=”20″
KEY=”your key here”

Then use this for ALSA:

$ ffmpeg \
       -f x11grab -s $SIZE -r $FRAMERATE -i :0.0 \
       -f alsa -ac 2 -i hw:0,0 -vb $BITRATE -vcodec libx264 \
       -threads 0 -acodec libmp3lame -ab 128k -ar 22050 \
       -f flv “rtmp://live.justin.tv/app/$KEY”

Or if using OSS:

$ ffmpeg \
       -f x11grab -s $SIZE -r $FRAMERATE -i :0.0 \
       -f oss -i /dev/dsp -vb $BITRATE -vcodec libx264 \
       -threads 0 -acodec libmp3lame -ab 128k -ar 22050 \
       -f flv “rtmp://live.justin.tv/app/$KEY”

I also found additional information on how to set this up on the following pages:

Attempt #1 - (didn't work)

Got this error: Unknown input format: 'pulse'

I found this on on the ffmpeg wiki. It was on the page titled Streaming Guide.

$ ffmpeg \
       -f x11grab -s $INRES -r $FPS -i :0.0 \
       -f pulse -ac 2 -i default -c:v libx264 -preset $QUAL \
       -pix_fmt yuv420p -s $OUTRES -c:a libmp3lame -ab 96k -ar 22050 \
       -threads 0 -f flv "rtmp://live.justin.tv/app/$STREAM_KEY"
7
  • @sim Unrecognized option 'af'
    – jackcook
    Aug 17, 2013 at 18:05
  • @javadog36 - There was a typo with previous comment: I think you can use an audio filter to do this: -af volume=2.0 should double it. If the filter doesn't work try the switch -vol 384.
    – slm
    Aug 17, 2013 at 18:13
  • @sim -af still isn't a property, and -vol 384 completely removes sound from the video
    – jackcook
    Aug 17, 2013 at 18:56
  • @sim I just found this. I'm assuming that I have an older version of ffmpeg. I tried the second option, and it says volume=2.0: No such file or directory.
    – jackcook
    Aug 17, 2013 at 19:01
  • @javadog36 - that's the same page that I got the info from 8-). I'm guessing it's an older version issue as well. Let me think about it a bit more. Let me know if you make any breakthroughs. Also can you delete some of the comments above that aren't relevant? Thanks.
    – slm
    Aug 17, 2013 at 19:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .