---
title: "My Frequently Used FFmpeg Commands"
date: 2022-08-06T23:11:46.000Z
author: Z.SHINCHVEN
tags: [FFmpeg, bitrate]
canonical: https://atlassc.net/2022/08/06/ffmpeg-frequently-used-commands
---
## Compress Video with Given Encoding Parameters

```bash
ffmpeg -i input.mp4 -c:v libx264 -b:v 6000k -b:a 192k -vf scale=1920:1080 output.mp4
```

- `-i`: Input file
- `-c:v`: Video codec
- `-b:v`: Video bitrate, 50m for 4K and 6000k for 1080P are advised.
- `-b:a`: Audio bitrate
- `-vf scale=1920:1080`: Filter, scale video resolution
- `output.mp4`: Output file

## Hardware Acceleration

FFmpeg has a lot of [hardware acceleration supports](https://trac.ffmpeg.org/wiki/HWAccelIntro) for each platform, for example, `videotoolbox` is the hardware acceleration codec for macOS:

```bash
ffmpeg -i input.mp4 -c:v hevc_videotoolbox -tag:v hvc1 output.mp4
ffmpeg -i input.mp4 -c:v h264_videotoolbox output.mp4
```

- `-c:v hevc_videotoolbox`: Use `hevc_videotoolbox` as the video codec
- `-tag:v hvc1`: Set the video tag to `hvc1` for Final Cut Pro HEVC support.

## Filters

```bash
ffmpeg -i input.webm -c:v libx264 -vf scale=1920:1080,zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p output.mp4
```

- `-vf`: Filters
  - `scale=1920:1080`: Scale video resolution
  - `zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p`: SDR Color grading

## Copy Audio or Video Streams

```bash
ffmpeg -i input.mp4 -c:v copy -c:a copy output.mkv
```

- `-c:v copy`: Copy video codec
- `-c:a copy`: Copy audio codec

## Copy Audio out of a Video File

```bash
ffmpeg -i input.mp4 -vn -acodec copy output.aac
```

- `-vn`: Disable video
- `-acodec copy`: Copy audio codec

## Convert Video with Similar Bitrate

> Tips: Convert video with similar or higher bitrate preserves the quality of the original video well.

Firstly, we need to get the video and audio `bit_rate` of the input file:

```bash
ffprobe -v quiet -print_format json -show_entries stream=index,codec_name,codec_type,bit_rate input.mp4
```

Let's assume the video `bit_rate` is `6000k` and the audio `bitrate` is `192k`, then we can convert the video with similar bitrate:

```bash
ffmpeg -i input.mp4 -c:v libx264 -b:v 6000k -c:a aac -b:a 192k output.mp4
```

- `-c:v libx264`: Use `libx264` as the video codec
- `-b:v 6000k`: Set the video bit_rate to 6000k
- `-c:a aac`: Use `aac` as the audio codec
- `-b:a 192k`: Set the audio bit_rate to 192k

## Merge Audio and Video Files

```bash
ffmpeg -i input.mp4 -i input.aac -c copy output.mkv
```

- `-i input.mp4`: Input video file
- `-i input.aac`: Input audio file
- `-c`: Copy

## Cut Video

I wrote a nodejs wrapper of FFmpeg to [cut video](https://github.com/ShinChven/cut-video) into clips with given timestamps in a config file, which I think is a better way to cut video.

```bash
# Cut by timestamp
ffmpeg -ss 00:00:30.0 -i input.wmv -t 00:00:10.0 -c copy output.wmv
# Cut by frame number
ffmpeg -ss 30 -i input.wmv -t 10 -c copy output.wmv
```

- **Arguments' order is important, fail to keep them in the following order `-ss <START_TIME> -i <INPUT_FILE> -t <END_TIME> [other]` will result in getting a corrupted video. <a href='https://ffmpeg.org/ffmpeg.html#:~:text=position%20(input/output)-,When%20used%20as%20an%20input%20option%20(before,)%2C%20seeks%20in%20this%20input%20file%20to%20position.,-Note%20that%20in' target='_blank'>Official document</a> on this matter.**

- `-ss`: Start time
- `-i`: Input file
- `-t`: Duration
- `-c`: Copy content
- `output.mp4`: Output file

## Merge Files in Directory

```bash
ffmpeg -f concat -safe 0 -i <(for f in *.mp4; do echo "file '$f'"; done) -c copy output.mp4
```

- `-f`: File format
- `-safe`: Safe mode
- `-i`: Input file
- `-c`: Copy
- `output.mp4`: Output file
- `*.mp4`: Files in directory
- `-i <(for f in *.mp4; do echo "file '$f'"; done)`: Command to generate input file
- `-c copy`: Command to copy
- `output.mp4`: Output file

## Remove Tag or Chapters

```bash
ffmpeg -i input.mp4 -map_metadata -1 -map_chapters -1 -vcodec copy -acodec copy output.mp4
```

- `-map_metadata -1`: Remove metadata
- `-map_chapters -1`: Remove chapters
- `-vcodec copy`: Copy video codec
- `-acodec copy`: Copy audio codec
