ffmpeg tutorials
Table of Contents
- 1. Introduction
- 2. Installation
- 3. Live streaming with ffmpeg
- 4. Record audio with ffmpeg
- 5. Screen recording with ffmpeg
- 6. Concatenate multiple files
- 7. Merge video with audio and keeping the shortest duration (from video)
- 8. You can also split video.
- 9. Extract single frame from a video
- 10. You can also reduce file size drastically using this command.
- 11. Reduce the size of all the videos in a directory.
- 12. Recursively go inside directories and execute a command on them
- 13. Find duration of videos in the directory
- 14. Burning subtitles
- 15. Blur
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
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 *.mp4 ; do echo "file '${f}'" >> file.txt; done;
6.2 While concatenating multiple videos
ffmpeg -safe 0 -f concat -i file.txt -vcodec copy output.mp4
6.3 While concatenating multiple videos remove audio using
ffmpeg -safe 0 -f concat -i file.txt -vcodec copy -an output.MOV
6.4 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 input.mp4 -ss 00:09:10 -to 00:10:00 -c copy output.mp4
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 -y screenshot.png
10 You can also reduce file size drastically using this command.
ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4
Concatenate videos and reduce file size at the same time.
ffmpeg -safe 0 -f concat -i file.txt -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.
Prefix small in the filename without changing the extension.
for f in *.mp4;do ffmpeg -i "${f}" -vcodec libx265 -crf 28 "small_${f}";done;
Suffix small in the filename and change the extension.
for f in *.mp4;do ffmpeg -i "${f}" -vcodec libx265 -crf 28 "${f%.*}_small.mp4";done;
Refer to Mastering Shell Scripting to learn more about extracting filename and extension.
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 Burning subtitles
14.1 Burn subtitles to your videos
Yes, you can burn your subtitles using filter_complex
, I made a video here: https://youtu.be/vCaxQ2pl5i0
I create shorts using ffmpeg.
14.2 TODO Change the position of the burned subtitles video
When using subtitles
you can specific the alignment
with different number values. You can check this page to learn about what those numbers will do: https://stackoverflow.com/questions/57869367/ffmpeg-subtitles-alignment-and-position
For example alignment:2
will burn the subtitles at the centre bottom. You can use alignment:10
to burn them in the centre of the video.
However I wanted to burn the subtitles slightly below the centre of the video. For that you need to first do alignment:0
and then use MarginL
for x position and MarginV
for y position.
For instance I used this in my script MarginL=10,MarginV=20
. Cool isn't it? but I don't know what these units are yet :) so I will still be using alignment:10
Video: https://youtu.be/NhL1IAoHPzY
15 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.