ffmpeg Tutorial

Ffmpeg

What is ffmpeg and basic usage

https://www.ruanyifeng.com/blog/2020/01/ffmpeg.html

How to install

# macOS
brew install ffmpeg
# centos
sudo yum install epel-release -y
sudo yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
sudo yum install ffmpeg ffmpeg-devel -y
ffmpeg -version
# source code
# https://ffmpeg.org/download.html#releases
# https://gist.githubusercontent.com/hyer/5a63543966dd2642989a/raw/8fc5b0993b4d0dcbf60cb48e1d0a098b38822669/install-ffmpeg.sh
wget https://ffmpeg.org/releases/ffmpeg-5.1.2.tar.xz && tar -xvf ffmpeg-5.1.2.tar.xz && cd ffmpeg-5.1.2
./configure --prefix=/usr/local/ffmpeg && make && make install
echo "export PATH=/usr/local/ffmpeg/bin:$PATH" >> ~/.bashrc 
echo "export LD_LIBRARY_PATH=/usr/local/ffmpeg/lib:$LD_LIBRARY_PATH" >> ~/.bashrc && source ~/.bashrc
ffmpeg -version

How to use ffmpeg to make live Broadcast

#/!bin/bash
# author=Jas0n0ss
# date=2023-01-17
#
# define color
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
font="\033[0m"
##

##
ffmpeg_install() {
	echo "ffmpeg checking" | lolcat
	ffmpeg -version | lolcat
	if [ $? == 0 ]; then
		echo -e "${yellow}ffmpeg$font ${green}Installed$font"
	else
		echo "wegt installing..." | lolcat
		yum -y install wget
		echo "ffmpeg installing" | lolcat
		wget --no-check-certificate https://ffmpeg.org/releases/ffmpeg-5.1.2.tar.xz
		tar -xJf ffmpeg-5.1.2.tar.xz && cd ffmpeg-5.1.2
		./configure --prefix=/usr/local/ffmpeg && make && make install
		echo "export PATH=/usr/local/ffmpeg/bin:$PATH"
		echo "export LD_LIBRARY_PATH=/usr/local/ffmpeg/lib:$LD_LIBRARY_PATH"
	fi
}

live_start() {
	echo "rtmp URL please:" | lolcat
	echo -e "$red Leave it blank will lead to default URL $font"
	read -r rtmp
	url=${rtmp:-"rtmp://live-push.bilivideo.com/live-bvc/?streamname=live_431290720_11083557&key=2c62b2a3a826c488d49663d6d1e1e3db&schedule=rtmp&pflag=1"}
	echo -e "$green The rtmp URL is: $font"
	echo $rmtp | lolcat
	echo "########################" | lolcat
	echo "The video path you want to do cycle live:" | lolcat
	echo -e "$red Leave it blank will load current path by default !!$font"
	read -r v_path
	V_PATH=${v_path:-"."}
	echo -e "$green video PATH is: $V_PATH $font"
	echo "########################" lolcat
	echo "Need logo right corner of your screen live(May need better CPU to supprt)?(yes/no):" | lolcat
	echo -e "$red Leave it blank will not have logo by default !!$font"
	read -r YN
	mark=${YN:-"no"}
	if [ $mark = "yes" ]; then
		echo "The logo file path (v.jpg/v.png/v.bmp):" | lolcat
		read -r image
		while true; do
			for video in $(find $V_PATH -type f -name "*.mp4" | shuf -n1); do
				ffmpeg -re -i "$video" -i "$image" -filter_complex overlay=W-w-5:5 -c:v libx264 -c:a aac -b:a 192k -strict -2 -f flv $rtmp
			done
		done
	else
		echo "Starting live with no logo..." | lolcat
		while true; do
			for video in $(find $V_PATH -type f -name "*.mp4" | shuf -n1); do
				ffmpeg -re -i "$video" -c:v copy -c:a aac -b:a 192k -strict -2 -f flv $rtmp
			done
		done
	fi
}

live_stop() {
	screen -S live -X quit
	killall ffmpeg
}

start() {
	echo -e "$red Please enter$font $green ffmpeg|start|stop$font"
	read -r $1
	if [ $1 == "start" ]; then
		live_start
	elif [ $1 == "stop" ];then
		live_stop
	elif [ $1 == "ffmpeg"];then
		ffmpeg_install
	else
		exit 0
	fi
}
#
echo "Please make sure you have lolcat installed !!!" | lolcat
start