Useful ffmpeg Commands by Examples
Tags: webdev, gamedev, linux, productivity
Published: 2021-10-12 07:28:28


This article presents a number of different ffmpeg use cases assuming the reader has basic knowledge of the software. You will see various practical examples in this article. For more details, please refer to the [official documentation pages](https://ffmpeg.org/ffmpeg.html).

All below examples are **one liners** although they are presented in multiple lines. Simply copy and paste them in your terminal and hit enter.

<center>⋅&nbsp;⋅&nbsp;⋅</center>
<center>◆&nbsp;◆&nbsp;◆</center>
<center>⋅&nbsp;⋅&nbsp;⋅</center>

## 1. Codec Options

Setting up codecs is the crucial part of encoding media assets. You will usually want to either keep source file’s encoding or apply a new encoder.

There are so many available codecs that ffmpeg supports. Below examples are some of the most popular combos that have been used in a number of different commercial projects.

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 1.1 Convert to MP4
```sh
ffmpeg -i source.mov -c:v libx264 -c:a aac output.mp4
```
_libx264_ is one of the most popular H.264 encoders. `libx264` + `aac` combo supports IE11. Use `-crf` option to control the output quality.

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 1.2 Convert to WebM
```sh
ffmpeg -i source.mov \
  -c:v libvpx-vp9 -crf 31 -b:v 1M \
  -c:a libvorbis \
output.webm
```
`-crf` stands for Constant Rate Factor. _libvpx_ doesn’t have default CRF value. `-crf 31` for 1080p is usually considered good enough while it accepts any number between 0 (lossless) and 63 (worst quality). For your information, _libx264_ has default value of 28 with range from 0 to 51.

`-b:v` is an option for bitrate. The value `1M` is identical to `1000k`. 

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 1.3 Convert to Ogg
```sh
ffmpeg -i source.mov \
  -c:v libtheora -q:v 5 \
  -c:a libvorbis -q:a 5 \
output.ogg
```
`-q:v` (video) and `-q:a` (audio) are options for Variable Bit Rate (VBR) quality level. The value range is 1-31 where 1 is the highest quality and 31 is the lowest quality. Alternatively, they are identical to `-qscale:v` and `-qscale:a` respectively.

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 1.4 Convert to GIF
```sh
ffmpeg -i source.mov \
  -vf " \
    scale=960:-1, \
    fps=16, \
    split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse
  " \
  -loop 0 \
output.gif
```

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 1.5 Keep original encoding
```sh
ffmpeg -i source.mov -c copy output.mov
```
This creates a duplicate copy of the source file.

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 1.6 Remove audio track
```sh
ffmpeg -i source.mov -c:v copy -an output.mp4
```
Use `-an` to remove audio track while keeping video track with `-c:v copy` option.

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 1.7 Export the 99th frame to an image
```sh
ffmpeg -i source.mov \
  -vf "select=eq(n\, 98)" -vframes 1 \
output.png
```
Since the frame count starts from 0, the option should be `"select=eq(n\, 98)"`, not `"select=eq(n\, 99)"`.

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 1.8 Export a frame at 5 second to an image
```sh
ffmpeg -i source.mov -ss 00:05:00 -vframes 1 output.png
```


<center>⋅&nbsp;⋅&nbsp;⋅</center>
<center>◆&nbsp;◆&nbsp;◆</center>
<center>⋅&nbsp;⋅&nbsp;⋅</center>


## 2. Advanced Editing Features

You can combine below options with any of above codec settings.

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 2.1 Cut specific range from the source
```sh
ffmpeg -i source.mov \
  -ss 00:05:00 -t 12 \
output.mp4
```
Output file will start from 5 minutes of the source file with the max length of 12 seconds, meaning that the output video will end at 00:05:12 of the source video if the source is longer than that.

Alternatively, `-ss 00:05:00 -to 00:05:12` or `-ss 300 -t 12` can be used for the same result. `-ss` can be used without `-t` option.

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 2.2 Resize video

- **Resize to 1920x1080**
```sh
ffmpeg -i source.mov \
  -vf "scale=1920:1080" \
output.mp4
```

- **Resize width to 1920 keeping source ratio**
```sh
ffmpeg -i source.mov \
  -vf "scale=1920:-1" \
output.mp4
```

- **Resize width to 1920 but make height 2160 with black paddings**
```sh
ffmpeg -i source.mov \
  -vf "scale=1920:-1, pad=1920:2160:-1:-1:color=black" \
output.mp4
```

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 2.3 Change video playback speed

- **2x faster**
```sh
ffmpeg -i source.mov \
  -vf "setpts=PTS/2" \
  -af "asetpts=PTS/2" \
output.mp4
```

- **1/2x speed**
```sh
ffmpeg -i source.mov \
  -vf "setpts=PTS/.5" \
  -af "asetpts=PTS/.5" \
output.mp4
```

`PTS` stands for Presentation TimeStamp. You can apply different video and audio speeds by giving different numbers on video filter (`-vf`) and audio filter (`-af`) respectively.

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 2.4 Set frames per second (FPS)
```sh
ffmpeg -i source.mov -vf "fps=30" output.mp4
```

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 2.5 Combine multiple videos

```sh
ffmpeg -safe 0 -f concat \
  -i <( \
    for f in $PWD/*.@(mov|mp4); do \
      printf "file ${f}\n"; \
    done; \
  ) \
  -c copy \
output.mov
```
Above command combines all the .mov and .mp4 files in the current directory and create _output.mov_ file. All videos must have same dimension ratio to avoid stretch or distortion.

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 2.6 Set group of pictures (GOP)
```sh
ffmpeg -i source.mov -g 300 output.mp4
```
GOP means distance between two keyframes.

<center>⋅&nbsp;⋅&nbsp;⋅</center>

### 2.7 Copy source media’s metadata
```sh
ffmpeg -i source.mov \
  -map_metadata 0 -movflags use_metadata_tags \
output.mp4
```
As some videos store custom metadata, this command might not copy all the data correctly. 

If you want to check metadata, install [exiftool](https://exiftool.org/) and run: 
```sh
exiftool -g1 -a -s -gps* output.mp4`
```

Alternatively, you can copy metadata using exiftool as below:
```sh
exiftool -tagsFromFile source.mov -All:All output.mp4
```

<center>⋅&nbsp;⋅&nbsp;⋅</center>