FFmpeg Live stream via Python PyLivestream

YouTube Live streaming (optionally including other sites simultaneously) with screensharing is simple and stable with FFmpeg via my PyLivestream Python program. Alternatively, below are commands to use FFmpeg directly from Terminal/Command Prompt, but this is what PyLivestream can do for you automatically.

Webcam:

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

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

FFmpeg video option -f v4l2 -r 10 -i /dev/video0 say to 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 encodes 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 lets 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 uses the FLV container format, live stream to YouTube, with stream ID YOURSTREAM. Don’t let others know the Stream ID!.

Screenshare: 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

-f x11grab -r 10 -s 1024x720 -i :0.0+0,24 sets 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.

Audio-only:

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.