Index Blogs

Two PC streaming setup FFmpeg

Published on 22 Sep 2019.

I wanted to stream to twitch.tv from my laptop (which is a MacBook Air) but it wasn't powerful enough to stream directly. The solution to this is a two PC streaming setup where your 'client' a.k.a. whichever device you want to stream from streams to an intemediary 'encoder' PC which then re-encodes the stream properly to the output

I want to setup my client device to share the desktop display. The setup is pretty simple:

ffmpeg -f avfoundation -capture_cursor 1 -s 2880x1800 -pix_fmt 0rgb -i 1 \
         -s 1400x900 -r 30 \
         -c:v h264_videotoolbox -profile:v main -b:v 40M \
         -sdp_file source.sdp -f rtp rtp://192.168.0.2:8080

This blasts RTP packets at the address above and saves an RTP Session Description Protocol file which stores information about the stream format. I chose a high rate and hardware encoding so I could avoid really bad quality while also keeping the bandwidth relatively low (if you have a gigabit network setup and both PCs are using ethernet you may be able to avoid encoding at all).

I then transfer the source.sdp file over to the encoder, read the RTP stream and send it off to twitch:

ffmpeg -i source.sdp \
  -vcodec libx264 -r 30 -g 60 -keyint_min 30 -b:v 1600k -minrate 1600k -maxrate 1600k -pix_fmt yuv420p \
  -s 1440x900 -preset fast -tune film -threads 0 -strict normal \
  -bufsize $CBR -f flv "rtmp://$SERVER.twitch.tv/app/$STREAM_KEY"

Replace $SERVER and $STREAM_KEY as needed. I think most people use OBS Studio for this instead of just FFmpeg directly since it allows various other handy things like stream overlays, scenes etc, but if you just want a super simple setup then give this a shot.