DEV Community

C.Y. Park
C.Y. Park

Posted on

Useful ffmpeg Commands by Examples

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.

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

⋅ ⋅ ⋅

◆ ◆ ◆

⋅ ⋅ ⋅

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.

⋅ ⋅ ⋅

1.1 Convert to MP4

ffmpeg -i source.mov -c:v libx264 -c:a aac output.mp4
Enter fullscreen mode Exit fullscreen mode

libx264 is one of the most popular H.264 encoders. libx264 + aac combo supports IE11. Use -crf option to control the output quality.

⋅ ⋅ ⋅

1.2 Convert to WebM

ffmpeg -i source.mov \
  -c:v libvpx-vp9 -crf 31 -b:v 1M \
  -c:a libvorbis \
output.webm
Enter fullscreen mode Exit fullscreen mode

-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.

⋅ ⋅ ⋅

1.3 Convert to Ogg

ffmpeg -i source.mov \
  -c:v libtheora -q:v 5 \
  -c:a libvorbis -q:a 5 \
output.ogg
Enter fullscreen mode Exit fullscreen mode

-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.

⋅ ⋅ ⋅

1.4 Convert to GIF

ffmpeg -i source.mov \
  -vf " \
    scale=960:-1, \
    fps=16, \
    split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse
  " \
  -loop 0 \
output.gif
Enter fullscreen mode Exit fullscreen mode
⋅ ⋅ ⋅

1.5 Keep original encoding

ffmpeg -i source.mov -c copy output.mov
Enter fullscreen mode Exit fullscreen mode

This creates a duplicate copy of the source file.

⋅ ⋅ ⋅

1.6 Remove audio track

ffmpeg -i source.mov -c:v copy -an output.mp4
Enter fullscreen mode Exit fullscreen mode

Use -an to remove audio track while keeping video track with -c:v copy option.

⋅ ⋅ ⋅

◆ ◆ ◆

⋅ ⋅ ⋅

2. Advanced Editing Features

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

⋅ ⋅ ⋅

2.1 Cut specific range from the source

ffmpeg -i source.mov \
  -ss 00:05:00 -t 12 \
output.mp4
Enter fullscreen mode Exit fullscreen mode

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.

⋅ ⋅ ⋅

2.2 Resize video

  • Resize to 1920x1080
ffmpeg -i source.mov \
  -vf "scale=1920:1080" \
output.mp4
Enter fullscreen mode Exit fullscreen mode
  • Resize width to 1920 keeping source ratio
ffmpeg -i source.mov \
  -vf "scale=1920:-1" \
output.mp4
Enter fullscreen mode Exit fullscreen mode
  • Resize width to 1920 but make height 2160 with black paddings
ffmpeg -i source.mov \
  -vf "scale=1920:-1, pad=1920:2160:-1:-1:color=black" \
output.mp4
Enter fullscreen mode Exit fullscreen mode
⋅ ⋅ ⋅

2.3 Change video playback speed

  • 2x faster
ffmpeg -i source.mov \
  -vf "setpts=PTS/2" \
  -af "asetpts=PTS/2" \
output.mp4
Enter fullscreen mode Exit fullscreen mode
  • 1/2x speed
ffmpeg -i source.mov \
  -vf "setpts=PTS/.5" \
  -af "asetpts=PTS/.5" \
output.mp4
Enter fullscreen mode Exit fullscreen mode

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.

⋅ ⋅ ⋅

2.4 Set frames per second (FPS)

ffmpeg -i source.mov -vf "fps=30" output.mp4
Enter fullscreen mode Exit fullscreen mode
⋅ ⋅ ⋅

2.5 Combine multiple videos

ffmpeg -safe 0 -f concat \
  -i <( \
    for f in $PWD/*.@(mov|mp4); do \
      printf "file ${f}\n"; \
    done; \
  ) \
  -c copy \
output.mov
Enter fullscreen mode Exit fullscreen mode

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.

⋅ ⋅ ⋅

2.6 Set group of pictures (GOP)

ffmpeg -i source.mov -g 300 output.mp4
Enter fullscreen mode Exit fullscreen mode

GOP means distance between two keyframes.

⋅ ⋅ ⋅

2.7 Copy source media’s metadata

ffmpeg -i source.mov \
  -map_metadata 0 -movflags use_metadata_tags \
output.mp4
Enter fullscreen mode Exit fullscreen mode

As some videos store custom metadata, this command might not copy all the data correctly.

If you want to check metadata, install exiftool and run:

exiftool -g1 -a -s -gps* output.mp4`
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can copy metadata using exiftool as below:

exiftool -tagsFromFile source.mov -All:All output.mp4
Enter fullscreen mode Exit fullscreen mode
⋅ ⋅ ⋅

Discussion (0)