How to use FFmpeg (with examples)

Have you ever had a video file that you needed to modify or optimise? You might have a video that is taking up too much space on your hard drive, or you just need to trim a small section from a long video or reduce the resolution. The go-to tool in these situations is FFmpeg, a software utility used by professionals and home users. In this article, we'll explain what FFmpeg is, how to install it, and look at some of the most common and useful commands using FFmpeg.

What is FFmpeg?

FFmpeg is a free and open-source video and audio processing tool that you run from the command-line.

FFmpeg is the tool of choice choice for multiple reasons:

  • Free: It's a completely free option.
  • Open-source: It has an active and dedicated open-source community continually deploying fixes, improvements, and new features.
  • Platform compatibility: FFmpeg is available for Windows, Mac, and Linux.
  • Command-line interface: It is a lightweight solution offering a vast array of options through a command-line interface.

How to install FFmpeg

Some operating systems, such as Ubuntu, install FFmpeg by default, so you might already have it on your computer.

Check if it's installed with the following command:

ffmpeg -version

If it gives you a version number and build information, you already have it installed.

If not, or you are using Windows or a Mac then you will need to download a static or compiled binary executable from a third party vendor. Unfortunately FFmpeg only provide the source code and not the ready to run software.

Here are the key steps you'll need to follow:

  1. Navigate to the FFmpeg download page.
  2. Under Get packages & executable files, select your operating system to display a list of vendors.
  3. Visit the most suitable vendor and follow the instructions on their web site. Typically you will either need to run a set of commands or you will need to download a zipped file (.zip, .7z, .tar.gz, etc.) containing the FFmpeg executable.
  4. If downloading, extract the contents of the zipped file to your chosen location. If you browse the extracted files you should find a file called ffmpeg or ffmpeg.exe in a bin folder.

To run FFmpeg you will need to use the command-line; open a new terminal and navigate to the directory where you extracted the ffmpeg file, then type and run the following command again:

ffmpeg -version

If installed correctly, you should see an output similar to below:

ffmpeg version 2024-02-04-git-7375a6ca7b-essentials_build-www.gyan.dev Copyright (c) 2000-2024 the FFmpeg developers
built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --pkg-config=pkgconf --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-dxva2 --enable-d3d11va --enable-libvpl --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband
libavutil 58. 36.101 / 58. 36.101
libavcodec 60. 38.100 / 60. 38.100
libavformat 60. 20.100 / 60. 20.100
libavdevice 60. 4.100 / 60. 4.100
libavfilter 9. 17.100 / 9. 17.100
libswscale 7. 6.100 / 7. 6.100
libswresample 4. 13.100 / 4. 13.100
libpostproc 57. 4.100 / 57. 4.100
Universal media converter
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

One last step to make FFMpeg more useful and available from any folder is to add it to your system PATH. This is different for each operating system, but typically involves adding the directory where the ffmpeg executable is located to the PATH environment variable.

Now that FFmpeg is successfully installed, let's look at how to use FFmpeg, with examples!

FFmpeg examples and common uses

Let's look at some of the most common and useful commands in FFmpeg.

You will need a sample video file to test the commands with. You can use any video file you have on your computer, or you can download this test file, which is names scott-ko.mp4.

Convert video formats using FFmpeg

One of the simplest and easiest commands to get started with is converting one video format to another. This is a common task when you need to make a video compatible with a specific device, player or platform.

A basic command to convert a video from one format to another, using our scott-ko.mp4 sample file is:

ffmpeg -i scott-ko.mp4 scott-ko.webm

This simple command will convert the video from the MP4 format to WEBM. FFmpeg is smart enough to know that the video and audio codec should be converted to be compatible with the new file type. For example, from h264 (MP4) to vp9 (WEBM) for video and aac (MP4) to opus (WEBM) for audio.

It is also possible to convert from one video format to another and have full control over the encoding options. The command to do that uses the following template:

ffmpeg -i input.mp4 -c:v video_codec -c:a audio_codec output.ext

Here are the options and the placeholders you can replace with your own values:

  • -i input.mp4: Replace input.mp4 with the path to your input video file.
  • -c:v video_codec: Specify the video codec for the output. Replace video_codec with the desired video codec (e.g., libx265 for H.265).
  • -c:a audio_codec: Specify the audio codec for the output. Replace audio_codec with the desired audio codec (e.g., aac for AAC audio).
  • output.ext: Replace this with the desired output file name and extension (e.g., output.mp4).

Here's an example of converting an MP4 video to an MKV video using H.264 codec for video and AAC codec for audio:

ffmpeg -i scott-ko.mp4 -c:v libx264 -c:a aac scott-ko.mkv

Trim a video using FFmpeg

If you have a long video and want to extract a small portion, you can trim the video using FFmpeg. You use the ss (start time) and t (duration) options.

Here's an example template command:

ffmpeg -i input.mp4 -ss start_time -t duration -c copy output.mp4

Use the following options and replace the placeholders with your specific values:

  • -ss start_time: Replace start_time with the start time to trim from. You can use various time formats like HH:MM:SS or seconds. For example, if you want to start trimming from 1 minute and 30 seconds, you can use -ss 00:01:30 or drop the hour and use -ss 01:30.
  • -t duration: Specify the duration of the trim. Again, you can use various time formats. For example, if you want to trim 20 seconds, you can use -t 20.
  • -c copy: This option copies the video and audio codecs without re-encoding, which is faster and preserves the original quality. If you need to re-encode, you can specify different codecs or omit this option.

Here's an example command trimming a video from 1 minute and 30 seconds to 20 seconds:

ffmpeg -i scott-ko.mp4 -ss 00:00:10 -t 5 -c copy trimmed.mp4

For more information and examples, see how to trim a video using FFmpeg.

Crop a video using FFmpeg

In the age of smartphones and social networks, cropping videos to different sizes and aspect rations has become an essential requirement when working with video. To crop a video using FFmpeg, use the crop filter.

Here's an example template:

ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4

The options and placeholders are described below:

  • input.mp4: Replace this with the name of your filename or the path to your input video.
  • -filter:v "crop=w:h:x:y" Use the crop video filter and specify the cropping parameters w (width), h (height), x (cropping x coordinate), and y (cropping y coordinate) according to your requirements.
  • output.mp4: Replace this with the desired filename or path for the output video.

Here's an example command cropping a video to a width of 640 pixels, a height of 640 pixels, and starting the crop from coordinates 900 pixels across and 50 pixels down:

ffmpeg -i scott-ko.mp4 -filter:v "crop=640:640:900:50" cropped.mp4

If you run this command using the provided test file, you'll see it is creates a square video cropped to the speakers face.

For more information and examples, see how to crop and resize videos using FFmpeg.

Extract or remove the audio from a video using FFmpeg

There are two common scenarios where you might want to work with a videos audio - extracting the audio so there is no video, or removing the audio from a video so it is silent, or muted.

To extract and save the audio from a video file using FFmpeg, use this command template:

ffmpeg -i input.mp4 -vn output.mp3

The following options are used and you can replace the following placeholders with your own preferences:

  • input.mp4: Replace this with the path to your input video file.
  • -vn: This option disables video processing.
  • output.mp3: Replace this with the desired output audio file name and extension. In this example, the output is saved as an MP3 file.

Here is an example command using our test file:

ffmpeg -i scott-ko.mp4 -vn scott-ko.mp3

To remove audio (or mute) a video file using FFmpeg, you can use the -an option, which disables audio processing. Here's an example command:

ffmpeg -i input.mp4 -an -c:v copy output.mp4

Here is an explanation of the options used:

  • -an: This option disables audio processing.
  • -c:v copy: This option copies the video stream without re-encoding, which is faster and preserves the original video quality. If you want to re-encode the video, you can specify a different video codec.

Here is an example using the test file:

ffmpeg -i scott-ko.mp4 -an -c:v copy scott-ko-muted.mp4

Concatenate videos using FFmpeg

Concatenating videos is the technical term FFmpeg uses to describe joining, merging or stitching multiple video clips together. To concatenate (or join) multiple video files together in FFmpeg, you can use the concat demuxer.

First, create a text file containing the list of video files you want to concatenate. Each line should contain the file path of a video file.

For example, create a file named filelist.txt and include a list of video files on your hard drive:

file 'video1.mp4'
file 'video2.mp4'
file 'video3.mp4'

Then, use the following FFmpeg command to concatenate the videos:

ffmpeg -f concat -safe 0 -i filelist.txt -c copy merged.mp4

Here is a summary of the options used:

  • -f concat: This specifies the format (concat) to be used.
  • -safe 0: This allows using absolute paths in the file list.
  • -i filelist.txt: This specifies the input file list.
  • -c copy: This copies the streams (video, audio) without re-encoding, preserving the original quality. If you need to re-encode, you can specify different codecs or omit this option.
  • merged.mp4: Replace this with the desired output file name and extension.

Adjust the file paths in filelist.txt according to your specific file names and paths. The order in which you list the files in the text file determines the order of concatenation.

For more information and examples, see merge videos using FFmpeg concat.

Resize a video using FFmpeg

You might need to resize a video if the resolution is very high, for example - you have a 4K video but you player only supports 1080p. To resize a video using FFmpeg, you can use the scale filter set using the -vf (video filter) option.

Here's an example template:

ffmpeg -i input.mp4 -vf "scale=w:h" resized.mp4

Replace the placeholders with your specific values:

  • input.mp4: Replace this with the path to your input video file.
  • -vf "scale=w:h": Replace w and h with the desired width and height of the output video. You can also set a single dimension, such as -vf "scale=-1:720" to maintain the original aspect ratio.
  • resized.mp4: Replace this with the desired output video file name and extension.

Here's an example command resizing our test video to 720p resolution and maintaining the aspect ratio:

ffmpeg -i scott-ko.mp4 -vf "scale=-1:720" resized.mp4

For more information and examples, see how to crop and resize videos using FFmpeg.

Compress a video using FFmpeg

Video files are typically large and can take up a lot of space on your hard drive, cloud storage or take a long time to download. To compress a video using FFmpeg, you typically need to re-encode it using a more efficient video codec or by adjusting other encoding parameters.

There are many different ways to do this but here's an example template to get you started:

ffmpeg -i input.mp4 -c:v libx264 -crf 25 -c:a aac -b:a 128k compressed.mp4

Here's the options and placeholders you can replace:

  • input.mp4: Replace this with the path to your input video file.
  • -c:v libx264: This option sets the video codec to H.264 (libx264). H.264 is a widely used and efficient video codec.
  • -crf 25: This controls the video quality. A lower CRF (constant rate factor) value results in higher quality but larger file size. Typical values range from 18 to 28, with 23 being a reasonable default.
  • -c:a aac -b:a 128k: These options set the audio codec to AAC with a bitrate of 128 kbps. Adjust the bitrate according to your preferences.
  • compressed.mp4: Replace this with the desired output file name and extension.

For more information and examples, see how to compress video using FFmpeg.

Using our test file, we can compress the video from 31.9MB to 6.99MB using this command:

ffmpeg -i scott-ko.mp4 -c:v libx264 -crf 25 -c:a aac -b:a 128k compressed.mp4

Convert a series of images to a video using FFmpeg

Who doesn't love a video montage? With FFmpeg it's easy to create a video from a series of images, simply use wildcard input glob pattern along with the -framerate option.

Here's an example command:

ffmpeg -framerate 1 -pattern_type glob -i 'path/to/images/*.jpg' -c:v libx264 -pix_fmt yuv420p montage.mp4

Replace the placeholders with your specific values:

  • -framerate 1: This sets the frame rate of the output video. Adjust the value according to your preference (e.g., 1 picture per second). Omitting the framerate will default to a framerate of 25.
  • -pattern_type glob -i 'path/to/images/*.jpg': This specifies the input images using a glob pattern. Adjust the pattern and path to the location of your image files.
  • -c:v libx264 -pix_fmt yuv420p: These options specify the video codec (libx264) and pixel format. Adjust these options based on your preferences and compatibility requirements.
  • montage.mp4: Replace this with the desired output file name and extension.

For more information and examples, see How to use FFmpeg to convert images to video.

Convert video to GIF using FFmpeg

GIFs are a popular animation format used for memes in messaging applications like WhatsApp or Facebook Messenger and a great way to send animations in emails among other use cases. There are a number of ways to convert and optimise a video to a GIF using FFmpeg, but here is a simple command template to get started with:

ffmpeg -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos" -c:v gif animation.gif

Here's a breakdown of the options used and what to replace:

  • input.mp4: Replace this with the path to your input video file.
  • -vf "fps=10,scale=320:-1:flags=lanczos": This sets the video filters for the GIF conversion. The fps option sets the frames per second (adjust the value as needed), and scale specifies the output dimensions. The flags=lanczos part is for quality optimization.
  • -c:v gif: This specifies the video codec for the output, in this case, GIF.
  • animation.gif: Replace this with the desired output file name and extension.

Here is an example using the test file:

ffmpeg -i scott-ko.mp4 -vf "fps=10,scale=320:-1:flags=lanczos" -c:v gif animation.gif

For more information and examples, see how to convert video to animated GIF using FFmpeg.

Speed up and slow down videos using FFmpeg

To speed up or slow down a video in FFmpeg, you can use the setpts filter. The setpts filter adjusts the presentation timestamp of video frames, effectively changing the speed of the video. Here are examples of both speeding up and slowing down a video.

Speed up a video

To double the speed of a video, use a setpts value of 0.5:

ffmpeg -i input.mp4 -filter:v "setpts=0.5*PTS" fast.mp4
Slow down a video

To slow down a video by a factor (e.g., 2x slower), you can use a setpts value greater than 1:

ffmpeg -i input.mp4 -filter:v "setpts=2.0*PTS" slow.mp4

These commands adjust the video speed by manipulating the presentation timestamps (PTS). The values 0.5 and 2.0 in the examples represent the speed factor. You can experiment with different speed factors to achieve the desired result.

Here is an example command that doubles the speed of our test file:

ffmpeg -i scott-ko.mp4 -filter:v "setpts=0.5*PTS" fast.mp4

Note that only the video is sped up, but not the audio.

Go forth and explore

This guide provides a quick primer on how to get started and use FFmpeg for various video processing tasks, along with some simple examples. The number of options and possibilities with FFmpeg is vast, and it's worth exploring the FFmpeg documentation and FFmpeg wiki to learn more about the tool and its capabilities.

FFmpeg's major strength is its versatility. However, it has a steep learning curve, with cryptic commands and an intimidating array of options. If you want to run FFmpeg commercially as part of a workflow, pipeline or application you'll also need to consider hosting the software, managing updates and security, and scaling the infrastructure to meet demand.

Shotstack was created to streamline automated video editing and video processing without having to learn complicated commands or worry about scaling infrastructure. Shotstack is an FFmpeg alternative offered as a collection of API's and SDK's that allow you to programmatically create, edit and render videos in the cloud. It's a great way to get started with video processing without having to worry about the complexities of FFmpeg.

Andrew Bone

BY ANDREW BONE
5th February, 2024

Become an Automated Video Editing Pro

Every month we share articles like this one to keep you up to speed with automated video editing.