FFmpeg is a free and open-source software project consisting of a large suite of libraries and programs for handling video, audio, and other multimedia files and streams. It is a cross-platform set of tools that work on Mac (OSX), Linux, and Windows. This article covers FFmpeg installation and most commonly used commands examples for getting video information, cutting and splitting video files, converting videos to various formats including images, managing audio, subtitles and so much more.
Table of contents
- Installing FFmpeg multimedia framework
- The most useful FFmpeg commands
- Get video file information
- Cut video into small clips
- Split video file into multiple parts
- Mute audio
- Extract audio from video
- Merge audio and video
- Convert video to images
- Convert images to video
- Converting MP4 to FLV
- Convert FLV to MPG
- Convert MPG into FLV
- Convert video into an animated GIF
- Convert to CD/DVD format
- Insert subtitles
- Extract subtitles
- Rotate video
- Summary
- Related articles
Installing FFmpeg multimedia framework
The installations of FFmpeg is easy and simple process for most common operating systems.
Ubuntu
To instal FFmpeg on the latest Ubuntu Linux, use the following commands:
sudo apt-get update
sudo apt install ffmpeg -y

OSX
If you’d like to install FFmpeg on your Mac, use Homebrew package manager:
brew update && brew upgrade ffmpeg

Windows
To install FFmpeg on Windows, use Chocolatey:
choco install ffmpeg

The most useful FFmpeg commands
Now, as soon as you’ve installed FFmpeg, we can jump to its most commonly used command examples.
For the purpose of this article we’ll use free sample video files.
Get video file information
To obtain detailed information about the video file, run the following command:
ffmpeg -i sample-mp4-file.mp4 -hide_banner

The -hide_banner
argument hides copyrights information.
Cut video into small clips
FFmpeg has a capability that allows you to trim video into short chunks.
We’re going to use the following arguments:
-ss
parameter specifies the start time stamp inHH:MM:SS.ms
format-t
argument specifies the final clip’s duration (time) in seconds-codec copy
– parameter would copy all streams without re-encoding
ffmpeg -i sample-mp4-file.mp4 -hide_banner \
-ss 00:01:00.0 \
-codec copy \
-t 20 \
OutputVideo.mp4

Split video file into multiple parts
Use the command shown below to divide a video into two or more parts.
In our example we’re splitting the file into two parts:
- The first part is 30 secs long from the beginning of the file (
-t
argument) - The second part will contain the video data from the 50th second till the end of the file (
-ss
argument)
ffmpeg -i sample-mp4-file.mp4 -hide_banner \
-t 00:00:30 \
-c copy OutputVideo-1.mp4 \
-ss 00:00:50 \
-codec copy \
OutputVideo-2.mp4

Mute audio
Use the following command to disable output video file audio:
ffmpeg -i sample-mp4-file.mp4 -hide_banner \
-an OutputVideo.mp4

Extract audio from video
You can extract the audio from the Video by executing the command:
ffmpeg -i sample-mp4-file.mp4 -hide_banner \
-vn \
-ac 2 \
-ab 187 \
-f mp3 \
Audio.mp3
Here we’re using the following options:
-vn
disables video recording to the output file-ac
sets the number of audio channels-ab
sets audio bitrate to 187kb/s- -f sets an output format to mp3

Merge audio and video
Use the following command to add audio track to video file:
ffmpeg -i Audio.mp3 -i sample-mp4-file.mp4 \
-hide_banner \
OutputVideo.mp4

Convert video to images
Use the below-stated command to convert Video into images:
ffmpeg -i sample-mp4-file.mp4 -hide_banner \
image%d.jpg

Convert images to video
All images can be converted the video:
ffmpeg -f image2 -i image%d.jpg \
-hide_banner \
OutputVideo.mp4

Try to add audio to the final clip yourself.
Converting MP4 to FLV
Run the command demonstrated below to convert MP4 format video file to FLV format:
ffmpeg -i sample-mp4-file.mp4 -hide_banner \
-vn \
-ac 2 \
-ab 187 \
-f flv \
OutputVideo.flv

Convert FLV to MPG
Run the command shown below to convert FLV format video file to MPG format:
ffmpeg -i OutputVideo.flv -hide_banner \
OutputVideo.mpg

Convert MPG into FLV
Copy the command demonstrated below to convert MPG format video file to FLV:
ffmpeg -i OutputVideo.mpg -hide_banner \
-ab 26k \
-f flv \
OutputVideo.flv

Convert video into an animated GIF
To convert a video file into an animated gif file, use the command:
ffmpeg -i sample-mp4-file.mp4 -hide_banner \
Output.gif

Convert to CD/DVD format
Use -target
type to create video CD or DVD formatted output file.
Target type supports vcd
, svcd
, dvd
, dv
and dv50
formats that may be prefixed with pal-
, ntsc-
or film-
to use the corresponding standard:
ffmpeg -i sample-mp4-file.mp4 -hide_banner \
-target film-vcd \
vcd_video.mpg

Insert subtitles
Let’s create a demo subtitles file (more information about subtitles):
cat << EOF > subtitles.srt
1
00:00:00,000 --> 00:00:20,000
This is demo subtitle 1
2
00:00:20,000 --> 00:00:40,000
This is demo subtitle 2
EOF
You can add subtitle file to video file by adding a separate subtitle file to the command:
ffmpeg -i sample-mp4-file.mp4 -hide_banner \
-i subtitles.srt \
-c copy \
-c:s mov_text \
OutputVideo.mp4
If you’d like to show subtitles in the video:
ffmpeg -i sample-mp4-file.mp4 -hide_banner \
-vf subtitles=subtitles.srt \
OutputVideo.mp4

Extract subtitles
To extract subtitles from the video file, check which stream you should use first:
ffmpeg -i OutputVideo.mp4

The output indicates, that subtitles are available in file at stream #0:2
.
Now, we can extract them using the following command:
ffmpeg -i OutputVideo.mp4 -hide_banner \
-map 0:2 subtitles.srt

Where -map 0:2
specifies the required stream.
Rotate video
And finally, here’s a command to rotate the video clip to the 90 degrease clockwise (use transpose=2
to rotate to anti clockwise direction):
ffmpeg -i sample-mp4-file.mp4 -hide_banner \
-filter:v 'transpose=1' \
OutputVideo.mp4

Summary
FFmpeg is a versatile multimedia framework that allows us to make various different operations with video files.
You can use FFmpeg to convert video and audio files into other formats, do image and audio extraction, rotating videos, adding subtitles, splitting video clips into multiple parts, and many more. In this guide, we illustrated how to apply the most commonly used commands to get the results you may need.
Related articles
- Building Thumbnails And GIFs Generator Using Lambda And FFmpeg
- The ultimate guide to image manipulation with ImageMagick
- How to convert PDF to PNG images and back in Linux
- How to extract .gz and .tar.gz files in Linux
How useful was this post?
Click on a star to rate it!
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?
I’m a passionate Cloud Infrastructure Architect with more than 15 years of experience in IT.
Any of my posts represent my personal experience and opinion about the topic.