FFmpeg-based Live stream via Python PyLivestream

Related: Live stream video data bandwidth consumption

YouTube Live streaming (optionally including other sites simultaneously) with screensharing is simple and stable with FFmpeg on Windows, Mac and Linux via my PyLivestream Python program.

python -m pip install PyLivestream

python Screenshare.py stream.ini youtube

Alternatively, below are commands to use FFmpeg directly from Terminal/Command Prompt, but this is what PyLivestream can do for you automatically.

Webcam FFmpeg YouTube Live

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

audio options

-f alsa -ac 2 -i hw:1,0
-ac 2for stereo to hardware address 1,0. For default audio device, use-f alsa -i pulse. Obtain hardware addresses viaarecord -l. Note that monoaural-ac 1` doesn’t work on some hardware.
-c:a aac -ar 44100
AAC encode audio at 44.1 kHz sampling frequency (passing audio up to about 22 kHz, CD-quality).

video options

-f v4l2 -r 10 -i /dev/video0
acquire video from webcam /dev/video0 at 10 fps, using the native resolution of the webcam. Force webcam resolution like -s 640x480.
-c:v 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.

FFmpeg config

-threads 0 -bufsize 512k
let FFmpeg choose the number of CPU threads, with a 512kB buffer. Adjust buffer size 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 YOURSTREAM. Don’t let others know the Stream ID!.

Screenshare FFmpeg YouTube Live

It’s better to run YouTube Live screenshare with PyLivestream Screenshare2YouTubeLive.py so that your password doesn’t get accidentally broadcast from your screen.

Otherwise, use the same command as above, swapping -f v4l2 -r 10 -i /dev/video0 with

-f x11grab -r 10 -s 1024x720 -i :0.0+0,24

video options

-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.

General FFmpeg stream source

Audio-only stream source

ffmpeg -f alsa -ac 2 -i pulse -c:a aac -ar 8000 -f rtp rtp://localhost:1234

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