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 belo…


This content originally appeared on DEV Community and was authored by C.Y. Park

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

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

-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

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

1.5 Keep original encoding

ffmpeg -i source.mov -c copy output.mov

This creates a duplicate copy of the source file.

⋅ ⋅ ⋅

1.6 Remove audio track

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.

⋅ ⋅ ⋅

◆ ◆ ◆

⋅ ⋅ ⋅

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

Output file will start from 5 minutes of the source file withe 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.

⋅ ⋅ ⋅

2.2 Resize video

  • Resize to 1920x1080
ffmpeg -i source.mov \
  -vf "scale=1920:1080" \
output.mp4
  • Resize width to 1920 keeping source ratio
ffmpeg -i source.mov \
  -vf "scale=1920:-1" \
output.mp4
  • 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
⋅ ⋅ ⋅

2.3 Change video playback speed

  • 2x faster
ffmpeg -i source.mov \
  -vf "setpts=PTS/2" \
  -af "asetpts=PTS/2" \
output.mp4
  • 1/2x speed
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.

⋅ ⋅ ⋅

2.4 Set frames per second (FPS)

ffmpeg -i source.mov -vf "fps=30" output.mp4
⋅ ⋅ ⋅

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

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

GOP means distance between two keyframes.


This content originally appeared on DEV Community and was authored by C.Y. Park


Print Share Comment Cite Upload Translate Updates
APA

C.Y. Park | Sciencx (2021-10-12T07:28:28+00:00) Useful ffmpeg Commands by Examples. Retrieved from https://www.scien.cx/2021/10/12/useful-ffmpeg-commands-by-examples/

MLA
" » Useful ffmpeg Commands by Examples." C.Y. Park | Sciencx - Tuesday October 12, 2021, https://www.scien.cx/2021/10/12/useful-ffmpeg-commands-by-examples/
HARVARD
C.Y. Park | Sciencx Tuesday October 12, 2021 » Useful ffmpeg Commands by Examples., viewed ,<https://www.scien.cx/2021/10/12/useful-ffmpeg-commands-by-examples/>
VANCOUVER
C.Y. Park | Sciencx - » Useful ffmpeg Commands by Examples. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/12/useful-ffmpeg-commands-by-examples/
CHICAGO
" » Useful ffmpeg Commands by Examples." C.Y. Park | Sciencx - Accessed . https://www.scien.cx/2021/10/12/useful-ffmpeg-commands-by-examples/
IEEE
" » Useful ffmpeg Commands by Examples." C.Y. Park | Sciencx [Online]. Available: https://www.scien.cx/2021/10/12/useful-ffmpeg-commands-by-examples/. [Accessed: ]
rf:citation
» Useful ffmpeg Commands by Examples | C.Y. Park | Sciencx | https://www.scien.cx/2021/10/12/useful-ffmpeg-commands-by-examples/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.