UP | HOME
Ravi Sagar | Home | Blog

ffmpeg tutorials

Table of Contents

1 Introduction

ffmpeg is a great piece of software. It is a cross platform utility to modify audio and video. It can do lot of wonderful things like editing videos, encoding them and live streaming as well. In this post I want to share whatever little I know and have been exploring.

Installing it is easy. Just check your package manager but on Arch linux you can do sudo pacman -S ffmpeg and it should install it.

2 Installation

2.1 CentOS 8

sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
sudo yum repolist
sudo dnf install ffmpeg
sudo yum-config-manager --enable powertools

3 Live streaming with ffmpeg

I recently made a video explain how to use ffmpeg to live stream from local computer to YouTube using nothing but shell scripts. Check the video here.

4 Record audio with ffmpeg

Check this link.

First check your audio devices.

arecord -l

5 Screen recording with ffmpeg

ffmpeg   -video_size 1368x768   -framerate 25   -f x11grab   -i :0.0+0,30   -f pulse   -i default   -ac 2   rec-01.mp4

6 Concatenate multiple files

6.1 Create file.txt for the concatenate to work

for f in *.mp3 ; do echo "file '${f}'" >> file.txt; done;

6.2 While concatenating multiple videos remove audio using

ffmpeg -safe 0 -f concat -i file.txt -vcodec copy -an output.MOV

6.3 Concatenate audio files

ffmpeg -safe 0 -f concat -i file.txt -acodec copy all.mp3

7 Merge video with audio and keeping the shortest duration (from video)

ffmpeg -i output.MOV -i ~/Documents/sounds_youtube/tmp/all.mp3 -map 0:v -map 1:a -filter:a "volume=0.2" -vcodec copy -shortest final.MOV

8 You can also split video.

ffmpeg -i output.MOV -ss 00:09:10 -to 00:10:00 -c copy final.MOV

9 Extract single frame from a video

Somtimes you may want to extract a frame for your thumnail.

ffmpeg -ss 00:02:13 -i input.mp4 -frames:v 1 frame.png

Source: https://stackoverflow.com/questions/27568254/how-to-extract-1-screenshot-for-a-video-with-ffmpeg-at-a-given-time

10 You can also reduce file size drastically using this command.

ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4

Reduce size of a single video and also increase the volume.

ffmpeg -i input.mp4 -map 0:v -map 0:a -filter:a "volume=4" -vcodec libx265 -crf 28 output.mp4

11 Reduce the size of all the videos in a directory.

for f in *.MP4;do ffmpeg -i "${f}" -vcodec libx265 -crf 28 "small_${f}";done;

12 Recursively go inside directories and execute a command on them

find . -type f -exec echo "'{}' and '{}_small'" \;

13 Find duration of videos in the directory

 for f in *.mp4; do ffprobe -v quiet -of csv=p=0 -show_entries format=duration "$f"; done | awk '{sum += $1}; END{print
sum/60/60}'

This will print time in hours.

14 Blur

ffmpeg -i 7.2_searching-sharing-and-exporting-issues_sharing-and-exporting.mp4 -filter_complex \ "[0:v]crop=1920:1080:0:0,boxblur=10:enable='between(t,321,323)'[blur1]; \
[0:v]crop=1920:1080:0:0,boxblur=10:enable='between(t,333,336)'[blur2]; \
[0:v][blur1]overlay=0:0:enable='between(t,321,323)'[v0];
[v0][blur2]overlay=0:0:enable='between(t,333,336)'[v]" \
-map "[v]" -map 0:a -c:v libx264 -b:v 3000k -bufsize 6000k -c:a copy blur.mp4

I had to provide bitrate because it was reduced significantly.