YouTube Live streaming using FFMPEG from Linux

YouTube Live streaming is simple and stable using nothing more than FFmpeg. This is also true on Linux, where FFmpeg works great for YouTube live streaming (and as a streaming source in general). OBS Studio is not needed for YouTube Live streams–YouTube should just recommend FFmpeg as below, particularly for Linux.

We give examples of FFmpeg for 3 scenarios:

  1. YouTube Live: Webcam streaming with audio
  2. YouTube Live: Screenshare streaming with audio
  3. Audio-only stream to almost any streaming player directly

Consider this easy-to-use Python script for YouTube FFmpeg streaming. This cross-platform Python script automatically computes good YouTube Live streaming parameters based on your inputs.

The following details are not normally needed–just use the easy Python script above

YouTube Live webcam stream with audio

Of course you can use more than a cheap webcam in this mode with an appropriate hardware video capture setup.

#!/bin/bash 

ffmpeg \
-f alsa -ac 1 -i hw:1,0 \
-f v4l2 -r 10 -i /dev/video0 \
-vcodec libx264 -pix_fmt yuv420p -preset ultrafast -g 20 -b:v 2500k \
-acodec libmp3lame -ar 44100 \
-threads 0 -bufsize 512k \
-f flv rtmp://a.rtmp.youtube.com/live2/YOURSTREAM &> stream.log

webcam audio config explained

-f alsa -ac 1 -i hw:1,0 sends a mono audio stream (-ac 2 for stereo) with the hardware address 1,0 obtained from arecord -l. If you just want the default audio device, you can instead say -f alsa -i pulse.

-acodec libmp3lame -ar 44100 encode audio as MP3 with a 44.1 kHz sampling frequency (passing audio up to about 22 kHz, which is CD-quality).

webcam video config explained

-f v4l2 -r 10 -i /dev/video0 acquire video from webcam at 10 fps, using the native resolution of the webcam–can force webcam resolution with option like -s 640x480.

-vcodec libx264 -pix_fmt yuv420p -preset ultrafast -g 20 -b:v 2500k encode video using the H.264 video compression algorithm, prioritizing reduced CPU utilization, in blocks of 20 frames with a target bitrate of 2.5 Mbps.

webcam FFmpeg config explained

-threads 0 -bufsize 512k let FFmpeg choose the number of CPU threads, and using a 512kB buffer (adjust as needed–tradeoff between latency and robustness).

-f flv rtmp://a.rtmp.youtube.com/live2/YOURSTREAM &> stream.log using the FLV container format, live stream to YouTube, with stream ID (don’t let others know it!) YOURSTREAM.

YouTube Live screenshare stream with audio

It’s better to run YouTube Live screenshare as a Python script so that your password doesn’t get accidentally broadcast from your screen

Here is a brief explanation of the parameters.

screenshare video config

-f x11grab -r 10 -s 1024x720 -i :0.0+0,24 screengrab at 10 fps, starting in the upper left hand corner, send a 1024x720 pixel video. If your screen resolution is more than 1024x720, there would be unsent regions at the bottom and/or right side.

Other FFmpeg streaming source applications

Audio-only streaming source FFmpeg

ffmpeg -f alsa -ac 1 -i pulse -acodec aac -ar 8000 -f rtp rtp://localhost:1234

Connect from the local or remote PC to rtp:://serverip:1234.