ffmpeg command: basic and advanced usage and script examples
Category: Linux
Date: February 2023
Views: 596
Introduction to ffmpeg
ffmpeg is a powerful command-line tool for converting, editing, and processing audio and video files. It is available for Linux, as well as other operating systems, and supports a wide range of formats and codecs. As always in this article I will share with you a personal script of mine using ffmpeg
Installing ffmpeg on Linux
Installing ffmpeg on Linux is straightforward. On Debian based distros, you can use the following command:
sudo apt install ffmpeg
On Arch linux:
sudo pacman -S ffmpeg
Other Linux distributions may have different installation methods. You can check the documentation for your distribution to find the appropriate commands.
Basic Usage of ffmpeg
The basic syntax for using ffmpeg is as follows:
ffmpeg [input options] -i input_file [output options] output_file
For example, to convert a video file from one format to another, you can use the following command:
ffmpeg -i input.mp4 output.avi
This will convert the input file, `input.mp4`, to the output file, `output.avi`, using the default settings for the output format. You can also specify options such as the bitrate, resolution, and quality of the output file.
Advanced Usage of ffmpeg
ffmpeg can be used for more complex tasks, such as video and audio filtering, cropping and scaling, and adding subtitles or watermarks to a video. You can find more information and examples in the ffmpeg documentation and online tutorials.
Using ffmpeg for Live Streaming and Recording
ffmpeg can also be used for live streaming and recording. It supports a variety of streaming protocols, such as RTMP and HLS, and can encode and stream video and audio in real time.
To start a live stream, you can use a command like the following:
ffmpeg -f v4l2 -i /dev/video0 -f alsa -i hw:0 -vcodec libx264 -preset ultrafast -acodec aac -f flv rtmp://streaming_server/live/stream_key
This command uses the video and audio input devices, encodes the video using the `libx264` codec and the audio using the `aac` codec, and streams the output to an RTMP server.
My personal ffmpeg scripts
ffmpeg is really versatile, one cannot really say they know all about it. the following bash script is my go to whenever I need some quick conversion, screen recording or other things. it offers the following functions
$ ff_functions
0 ff_recscreen
1 ff_mergeVideoAudio
2 ff_convertToYoutube
3 ff_muteVideo
4 ff_convertToWhatsApp
5 ff_concatSubscribe
to use it just run it like this, to mute a video for example
$ ff_functions 3 video_file.mp4
#!/bin/bash
#screen recording
# $1 can be 2, indication recording the second monitor as per my settings
function ff_recscreen(){
outputfile="${HOME}/Videos/$(date +%G_%m_%d_at_%H-%M-%S).mkv"
audioInputSource=$(
pactl list sources short \
| awk '{print $2}'\
| grep input \
| tail -1
)
audioOutputSource=$(
pactl list sources short \
| awk '{print $2}' \
| grep output \
| tail -1
)
SCREEN_res=$(xrandr -q --current | grep '*' | awk '{print $1}' | head -1 )
if [[ "$1" == "2" ]]
then screen="1920+0"
else screen="0+0"
fi
printf '\033[1;32m audio1 : \033[1;0m %s\n' "$audioInputSource"
printf '\033[1;32m audio2 : \033[1;0m %s\n' "$audioOutputSource"
printf '\033[1;32m screen : \033[1;0m %s\n' "$SCREEN_res +0.0+$screen"
IFS= read -rN 1 -p "continue (Y/n) ? : " answer
echo
[[ "$answer" == "n" ]] && return
ffmpeg \
-f pulse -ac 2 -i $audioInputSource \
-f pulse -ac 2 -i $audioOutputSource \
-f x11grab -r 30 -s $SCREEN_res -i :0.0+$screen \
-af "highpass=f=200, lowpass=f=3000" \
-acodec pcm_s16le \
-vcodec libx264 \
-crf 0 \
-threads 0 \
$outputfile
}
# merge video and audio
# $1 : video file
# $2 : audio file
function ff_mergeVideoAudio(){
video="$1"
audio="$2"
ffmpeg \
-i "$video" \
-i "$audio" \
-filter_complex \
"[0:a]apad[main];[1:a]volume=1.2,apad[A];[main][A]amerge[out]" \
-c:v copy -c:a aac -map 0:v -map "[out]" \
-preset ultrafast -threads 0 \
-ac 2 -pix_fmt yuv420p \
-shortest \
"${video%.*}-${audio%.*}_mixed.mp4"
}
# convert large video file to a reasonable size, no loss of quality
# $1 : video file
function ff_convertToYoutube(){
video="$1"
ffmpeg \
-i "$video" \
-c:v libx264 \
-crf 24 \
-b:v 1M \
"${video%.*}_converted.mp4"
}
# remove audio from video file
# $1 : video file
function ff_muteVideo(){
video="$1"
ffmpeg \
-i "$video" \
-c:v copy \
-an \
"${video%.*}_muted.mp4"
}
# reduce video size to make it uploadable to whatsapp
# $1 : video file
function ff_convertToWhatsApp(){
video="$1"
ffmpeg \
-i "$video" \
-c:v libx264 \
-profile:v baseline \
-level 3.0 \
-pix_fmt yuv420p \
"${video%.*}_whatsapp.mp4"
}
# add add an image to the end of a video
function ff_concatSubscribe(){
video="$1"
image="$2"
#get the video duration:
dd=$(ffmpeg -i "$1" 2> &1 | grep Duration | awk '{print $2}' | tr -d , )
m=$(echo "$dd" | cut -d: -f2 )
s=$(echo "$dd" | cut -d: -f3 | cut -d. -f1)
duration=$((m*60+s+2))
#create a slightly longer video from the image:
ffmpeg \
-loop 1 \
-i "$image" \
-f lavfi -i anullsrc \
-c:v libx264 \
-c:a aac \
-t $duration \
-pix_fmt yuv420p \
-vf scale=1902:1080 \
-b:v 1M \
-y "${image%.*}.mp4"
#concat video and image:
ffmpeg \
-i "$video" \
-i "${image%.*}.mp4" \
-filter_complex "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" \
-shortest \
-y output.mkv
exit
}
readarray -t y <<< "$( cat "$0" |grep ^function |sed 's/function//g;s/[(){]//g' )"
if [[ -z "$1" ]]
then
for (( k=0;k<${#y[$@]}; k++ )) ; do
! [[ -z "${y[$k]}" ]] && printf "$k\t${y[$k]}\n"
done
else
${y[$1]} "$2" "$3" "$4"
fi
Conclusion and Further Resources
ffmpeg is a versatile and powerful tool for audio and video processing in Linux. With the right options and techniques, you can use it to perform a wide range of tasks, from simple conversions to complex video processing and live streaming. For more information, you can consult the ffmpeg documentation and online forums and communities.
0 Comments, latest
No comments.