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 a license. Here is a simple note on several common FFmpeg commands to help novice readers easily convert their video encoding! Most of the content here is referenced from existing online sources with marked citations.
Let's have some fun together ฅ՞•ﻌ•՞ฅ
Installing FFmpeg#
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 images, etc. Common video container formats include:
- MP4 (MPEG 4)
- MKV (Matroska)
- WebM
- AVI (ASF)
- MOV (QuickTime)
TOP: WebM is a popular container for AV1, VP9, and VP8, which require specific hardware acceleration for encoding and decoding.
For example, AIN's 2022 new products 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 encoderNVENC
: H.264 encoder based on NVIDIA GPUlibx265
: Open-source HEVC encoderlibvpx
: Google's VP8 and VP9 encoderslibaom
: 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 qualityultrafast
,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
Only display 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
Clip#
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 we only demonstrate the simplest encoding methods, 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 at 2M bitrate
ffmpeg -i input.mp4 -b:v 2M -bufsize 2M output.mp4
Stabilize at 2M bitrate allowing a maximum of 2.5M bitrate
ffmpeg -i input.mp4 -b:v 2M -bufsize 2M -maxrate 2.5M output.mp4
Using 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 Video bitrate
-minrate Minimum allowed bitrate
-maxrate Maximum allowed 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 with -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 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 Sample 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 updated synchronously by Mix Space to xLog. The original link is https://www.miaoer.net/posts/blog/ffmpeg