banner
miaoer

miaoer

喵二の小博客 https://www.miaoer.net xLog 分站
tg_channel
telegram
bilibili

Common FFmpeg Commands

Preface#

Recently discovered and used FFmpeg and found it to be very powerful, capable of various audio and video operations. Some software also references FFmpeg mainly because it is a free and open-source tool that does not require licensing. Here, I will briefly make some notes on several common FFmpeg commands to help novice readers easily convert the encoding of their videos! Most of the content here is referenced from existing online sources with marked citations.

Let's have some fun together ฅ՞•ﻌ•՞ฅ

Installing FFmpeg#

image

Windows#

Since FFmpeg only provides source code that needs to be compiled, there are experts who have compiled FFmpeg that can be directly used from the repository.

https://github.com/BtbN/FFmpeg-Builds/releases

Ubuntu#

sudo add-apt-repository universe
sudo apt update
sudo apt install ffmpeg

Overview#

Container Formats#

The common video file formats are actually containers that include video tracks, audio tracks, metadata, subtitles, titles, cover art, etc. Common video container formats include:

  • MP4 (MPEG 4)
  • MKV (Matroska)
  • WebM
  • AVI (ASF)
  • MOV (QuickTime)

TOP: WebM is a recently popular container used for AV1, VP9, and VP8, and requires specific hardware acceleration for encoding and decoding.

For example, AIN's new products in 2022 all support AV1 encoding and decoding hardware acceleration.

Encoding#

Common types of encoding include licensed encoding, royalty-free encoding, and audio encoding formats.

Licensed Encoding

  • H.262
  • H.264
  • H.265

Royalty-Free Encoding

  • VP8
  • VP9
  • AV1

Audio Encoding Formats

  • MP3
  • AAC

Video Encoders

  • libx264: The most popular open-source H.264 encoder
  • NVENC: H.264 encoder based on NVIDIA GPU
  • libx265: Open-source HEVC encoder
  • libvpx: Google's VP8 and VP9 encoders
  • libaom: AV1 encoder

Audio Encoders

  • libfdk-aac
  • aac

View the installed encoders in FFmpeg

ffmpeg -encoders

Usage#

ffmpeg [1] [2] -i [3] [4] [5]

[1] Global parameters

[2] Input file parameters

[3] Input file

[4] Output file parameters

[5] Output file

Parameters#

  • -c: Specify the encoder
  • -c copy: Directly copy without re-encoding, faster
  • -c:v: Specify the video encoder
  • -c:a: Specify the audio encoder
  • -i: Specify the input file
  • -an: Remove the audio stream
  • -vn: Remove the video stream
  • -preset: Video encoding quality ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow
  • -y: Overwrite files without confirmation

More: https://zh.m.wikipedia.org/zh-hans/FFmpeg


File Information#

ffmpeg -i input.mp4

Show only metadata, clean
ffmpeg -i input.mp4 -hide_banner

Format Conversion#

ffmpeg -i input.avi output.mp4

Direct copy is the fastest
ffmpeg -i input.avi -c copy output.mp4

-i Input file

-c copy Direct copy without transcoding is faster

Transcoding#

ffmpeg -i input.mp4 -c:v libx264 output.mp4

Choose H.265 encoder
ffmpeg -i input.mp4 -c:v libx265 output.mp4

-i Input file

-c Convert encoder

Trimming#

ffmpeg -ss 00:00:00 -t 00:00:30 -i input.mp4 -vcodec copy -acodec copy output.mp4

-ss Start time

-t End time

-i Input file

Bitrate#

This section is too professional and requires a deeper understanding! Here, only the simplest encoding methods are demonstrated, excluding compression configurations.

ABR (VBR) Two-Pass Encoding#

ffmpeg -i input.mp4 -c:v libx264 -b:v 1M -pass 1 -f null /dev/null
ffmpeg -i input.mp4 -c:v libx264 -b:v 1M -pass 2 output.mp4

Of course, you can also do VBR in one pass
ffmpeg -i input.mp4 -c:v libx264 -b:v 1M -pass 1 output.mp4

CBR Constant Bitrate#

Compress to 2M bitrate
ffmpeg -i input.mp4 -b:v 2M output.mp4

Stabilize compression to 2M bitrate
ffmpeg -i input.mp4 -b:v 2M -bufsize 2M output.mp4

Stabilize compression at 2M bitrate with a maximum allowed bitrate of 2.5M
ffmpeg -i input.mp4 -b:v 2M -bufsize 2M -maxrate 2.5M output.mp4

Use libx264 encoder with a minimum bitrate of 1M, maximum bitrate of 2M, stabilized at 1M bitrate
ffmpeg -i input.mp4 -c:v libx264 -b:v 1M -minrate 1M -maxrate 2M -bufsize 1M output.mp4

-i Input file

-b Audio bitrate

-b Video bitrate

-minrate Allowed minimum bitrate

-maxrate Allowed maximum bitrate

-bufsize Bitrate control buffer

Size#

ffmpeg -i input.avi -fs 114M output.mp4

-i Input file

-fs Control file size

Resolution#

ffmpeg -i input.mp4 -vf scale=1920:1080 output.mp4

Maintain aspect ratio as -1
ffmpeg -i input.mp4 -vf scale=1920:-1 output.mp4

-i Input file
[Maintain aspect ratio -1]

FPS#

ffmpeg -i input.avi -r 30 output.mp4

-i Input file

-r Frame rate value

Extract Audio#

ffmpeg -i input.mp4 -aq 0 -f mp3 -vn output.mp3
ffmpeg -i input.mp4 -acodec aac -vn output.mp3

-i Input file

-f Output format

-vn Exclude video

-aq 0 Compression quality

-acodec Set audio codec

Separate Audio and Video#

Separate Video

ffmpeg -i input.mp4 -vcodec copy -an output.mp4

Separate acc Format

ffmpeg -i input.mp4 -acodec copy -vn output.m4a
ffmpeg -i m.m4a m.mp3

Directly Extract Audio

ffmpeg -i mavel4.mp4 m.mp3

Merge Audio and Video#

ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -strict experimental output.mp4

-i Input file

-c Audio encoding method

-c Video encoding method

Compress Audio#

ffmpeg -i input.wav -b:a 64k -acodec mp3 -ar 44100 -ac 1 output.mp3

-i Input file

-b Bitrate

-ar Sampling rate

Screenshot#

Screenshot at a specified time

ffmpeg -ss 00:11:45 -i input.mp4 -r 1 output.jpg 

Extract frames from the beginning

ffmpeg -i input.mp4 -r 1 -q:v 2 -f image2 output.jpg

-i Input file

-r Frames per second

-q Image quality

-f Output format

Cover#

Video Cover

ffmpeg -i input.mp4 -i input.jpg -map 0 -map 1:0 -c copy -disposition:v:1 output.mp4

Audio Cover

ffmpeg -i input.mp3 -i input.jpg -map 0:0 -map 1:0 -codec copy -id3v2_version 3 \ 
-metadata:s:v title="Album cover" -metadata:s:v comment="Cover (front)" output.mp3

Citations:
FFmpeg Video Processing Beginner's Tutorial

Using ffmpeg to Separate Video and Audio Streams

Common FFmpeg Parameters Explanation and Examples

Bitrate Control (1): Understanding Bitrate Control Modes (x264,x264,vpx)

This article is synchronized and updated to xLog by Mix Space The original link is https://www.miaoer.net/posts/blog/ffmpeg

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.