Banner of From Script to Screen: How to Live Stream Using FFmpeg

Live Stream Your Linux Screen to YouTube From The Terminal


Category: Shell Scripting

Date: 2 months ago
Views: 515


Prerequisites

Before we get started, let's make sure we have everything we need:

  • FFmpeg installed on my system.

  • A YouTube stream key, which you can find in your YouTube Live Dashboard.

  • (Optional) Conky, a lightweight system monitor for Linux.

Script Overview

I've created a Bash script that incorporates several functions to handle different aspects of the streaming process:

  • notif: Sends desktop notifications using notify-send.

  • ff_recscreen: Configures FFmpeg for screen recording and streaming.

  • ff_kill: Terminates the FFmpeg process.

Script Walkthrough

    
#!/bin/bash
youtube_streaming_key="your youtube streaming key"
function notif(){
	local n_id=12347
	n_msg="$1"
	notify-send -a "ffmpeg rec" -r $n_id -u low "$n_msg" -t 1000 -i notification-message-im
}

function ff_recscreen(){
	ffmpeg -loglevel debug \
	       -threads:v 1 -threads:a 4 \
	       -filter_threads 2 -thread_queue_size 512 \
	       -f x11grab -video_size 1366x768 -framerate 30 -i :0.0+0,0 \
	       -f pulse -i alsa_input.pci-0000_00_1f.3.analog-stereo -pix_fmt yuv420p \
	       -c:v libx264 -qp:v 19 \
	       -profile:v high \
	       -af "volume=0.5" \
	       -c:a aac -b:a 128k \
	       -f flv rtmp://a.rtmp.youtube.com/live2/$youtube_streaming_key
}

function ff_kill(){
    rm /tmp/recording_in_progress 2> /dev/null
    pkill -2 ffmpeg
    kill -9 $(pgrep -f conky_recording)
    notif "streaming finished"
}

if [[ -f "/tmp/recording_in_progress" ]]
	then ff_kill
	else
	     notif "Starting streaming"
	     conky -c ~/.i3/conky/conky_recording &
	     touch /tmp/recording_in_progress
	     sleep 1.2
	     ff_recscreen &
fi
    

Usage

  1. Set Permissions: First, let's make the script executable by running chmod +x script_name.sh.

  2. Edit the Script: Now, replace $youtube_streaming_key with your actual YouTube stream key in the script.

  3. Run the Script: To start streaming, simply execute the script by running ./script_name.sh.

Script Overview

The Bash script automates the process of screen streaming using FFmpeg. When executed for the first time, it creates a temporary file to track the recording status. It then sends a notification, starts Conky for displaying this small icon in the top right corner of the screen, and initiates FFmpeg for screen recording and streaming. Subsequent executions of the script check for the existence of the temporary file. If the file exists, indicating an ongoing recording, the script terminates the FFmpeg and Conky processes, effectively stopping the streaming session.

Conclusion

With this script, you can easily initiate live streaming of your screen to platforms like YouTube using FFmpeg. Feel free to customize it further to suit your specific requirements or integrate additional features. Happy streaming!



515 views

Previous Article Next Article

0 Comments, latest

No comments.