FFmpeg 快速上手:命令行詳解、工具、教程、電子書

FFmpeg 簡介

FFmpeg 是一個開源的音視頻處理工具,誕生已 22 年。它可以用來處理音視頻的編解碼、格式轉換、剪輯、合併、抽取、壓縮、解壓縮、濾鏡、字幕等等。它可以在 Windows、Linux、Mac 等多種平臺上使用。

FFmpeg 由法國天才程序員 Fabrice Bellard 在 2000 年時開發出初版。不過後來 FFmpeg 社區出現分裂,包括 Fabrice Bellard 在內的部分 FFmpeg 開發者決定脫離原組織重新創建新項目,稱作 libav。後來 FFmpeg 項目負責人 Michael Niedermayer 將 libav 的代碼合併到了 FFmpeg。

除了 FFmpeg,其他許多知名開源項目,諸如 TinyCC、QEMU、JSLinux 以及比較新的 QuickJS 均出自 Fabrice Bellard 之手,詳情查看 https://bellard.org/。

FFmpeg 單詞中的 “FF” 指的是 “Fast Forward(快速前進)”,MPEG 是制定國際標準的組織,負責制定影音壓縮及傳輸的規格標準。

FFmpeg 恥辱柱

FFmpeg 最被人熟知的還有它創建的 “恥辱柱”(已於 2014 年下線,不過其 網頁源碼 [1] 仍保留這些名單)。因爲 FFmpeg 屬於自由軟件,採用了 LGPL 和 GPL 許可證(具體依據所選的組件),所以任何人都可以在遵守協議的情況下自由使用。不過很多播放軟件使用了 FFmpeg 的代碼後並沒有遵守開源協議公開任何源代碼。FFmpeg 社區便將違反開源協議的公司、組織和個人網站貼在 “恥辱柱”。韓國播放軟件 KMPlayer 以及國產播放器暴風影音、QQ 影音都曾上過榜。

FFmpeg 安裝

Windows

Windows 平臺上 FFmpeg 安裝比較簡單,只需要下載 FFmpeg 的壓縮包,解壓後將 bin 目錄添加到環境變量即可。

下載地址:Download FFmpeg[2]

Linux

Linux 平臺上 FFmpeg 安裝比較簡單,只需要使用包管理器安裝即可。

Ubuntu/Debian:

sudo apt install ffmpeg

CentOS/RHEL:

sudo yum install ffmpeg

Arch Linux:

sudo pacman -S ffmpeg

MacOS

brew install ffmpeg

FFmpeg 工作流程

ffmpeg 的主要工作流程相對比較簡單,具體如下。

  1. 1. 解封裝 (Demuxing)

  2. 2. 解碼 (Decoding)

  3. 3. 編碼 (Encoding)

  4. 4. 封裝 (Muxing)

具體爲 6 個步驟:

  1. 1. 讀取輸入源

  2. 2. 進行音視頻的解封裝

  3. 3. 解碼每一幀音視頻數據

  4. 4. 編碼每一幀音視頻數據

  5. 5. 進行音視頻的重新封裝

  6. 6. 輸出到目標

FFmpeg 工作流程

FFmpeg 命令

FFmpeg 命令格式

ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

舉個例子,以下命令將 input.mp4 通過 H.264 編碼器轉碼爲 output.mp4:

ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset veryfast output.mp4

其中 -i 表示輸入文件,-c:v 用於指定視頻編碼器, libx264 是 H.264 編碼的一個實現,-crf 表示視頻質量,-preset veryfast 表示預設的編碼速度,output.mp4 表示輸出文件。

FFmpeg 命令參數

通過 ffmpeg -h 可以看到 ffmpeg 常見的命令大概分爲 6 個部分,具體如下:

常用參數如下:

FFmpeg 常用命令

視頻轉碼(指定分辨率)

ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset veryfast -s 1280x720 output.mp4

提取音頻

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

截取視頻

ffmpeg -i input.mp4 -ss 00:00:10 -t 00:00:10 -c copy output.mp4

裁剪畫面

ffmpeg -i input.mp4 -filter:v "crop=1280:720:0:0" output.mp4

旋轉畫面

ffmpeg -i input.mp4 -vf "transpose=1" output.mp4

添加水印

ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=10:10" output.mp4

添加字幕

ffmpeg -i input.mp4 -vf "subtitles=subtitle.srt" output.mp4

合併多個視頻到一個視頻

list.txt 即爲視頻列表,每行一個視頻文件名

ffmpeg -f concat -i list.txt -c copy output.mp4

合併視頻音頻

ffmpeg -i input.mp4 -i input.aac -c copy output.mp4

FFmpeg 命令行生成器

FFmpeg 涉及到很多參數,如果每次都要記住這些參數,那就太麻煩了,所以我們可以使用在線的命令行生成器,這樣就可以很方便的生成命令行了。

FFmpeg Commander[3]

FFmpeg Commander

FFmpeg Command Generator[4]

FFmpeg Command Generator

ffmpeg - CMD Generator[5]

ffmpeg string creator[6]

FFmpeg GUI

命令行寫起來很麻煩,所以我們可以使用 GUI 工具來代替命令行。這裏介紹幾個非常好用的 GUI 工具。

handbrake

HandBrake 是一個由志願者構建的開源工具,用於將視頻從幾乎任何格式轉換爲精選的現代、廣泛支持的編解碼器。

HandBrake: Open Source Video Transcoder[7]

HandBrake

HandBrake Documentation — Supported source formats[8]

One of HandBrake’s strengths is its ability to open a wide variety of video formats. HandBrake uses FFmpeg under the hood and generally can open whatever FFmpeg will, in addition to disc-based formats like DVD and Blu-ray.

QuickCut

Quick Cut 是一款輕量、強大、好用的視頻處理軟件。它是一個輕量的工具,而不是像 Davinci Resolve、Adobe Premiere 那樣專業的、複雜的龐然大物。Quick Cut 可以滿足普通人一般的視頻處理需求:壓縮視頻、轉碼視頻、倒放視頻、合併片段、根據字幕裁切片段、自動配字幕、自動剪輯……

Quick Cut 發行版 - Gitee.com[9]

Quick Cut

Shutter Encoder

我個比較喜歡的一個 GUI 工具,批量轉換很方便。

Shutter Encoder encoding|converting video FREE PC|Mac[10]

Shutter Encoder

ShanaEncoder

ShanaEncoder Download - Shana[11]

QWinFF

Home | QWinFF[12]

ciano

robertsanseries/ciano: A multimedia file converter focused on simplicity.[13]

App Ciano - Robert San[14]

FFaudioConverter

Release FFaudioConverter 0.31.0 · Bleuzen/FFaudioConverter[15]

FFmpeg 教程

FFmpeg 視頻處理入門教程 - 阮一峯的網絡日誌 [16]

FFmpeg 系列教程 視頻教程 [17]

FFmpeg 系列教程

FFmpeg 官方文檔翻譯 - 鏈滴 [18]

FFmpeg 電子書

FFmpeg 從入門到精通

FFmpeg 從入門到精通

本書是一本介紹 FFmpeg 的實戰技術指南,全書共 10 章,分爲兩個部分。部分(第 1~7 章)爲 FFmpeg 的命令行使用篇,介紹了 FFmpeg 的基礎組成部分、FFmpeg 工具使用、FFmpeg 的封裝操作、FFmpeg 的轉碼操作、FFmpeg 的流媒體操作、FFmpeg 的濾鏡操作、FFmpeg 的設備操作。第二部分(第 8~10 章)爲 FFmpeg 的 API 使用篇,介紹了 FFmpeg 封裝部分的 API 使用操作、FFmpeg 編解碼部分的 API 使用操作,FFmpeg 濾鏡部分的 API 使用操作,相關操作均以實例方式進行說明,包括新 API 及舊 API 的操作。

Understanding FFmpeg with source code FFMPEG Fundementals

Understanding FFmpeg with source code FFMPEG Fundementals

In this book, we will review the ffmpeg source code and learn about ffmpeg's capabilities. In the first part, we will consider ffmpeg in general terms. In the second part, we will discuss the ffmpeg methods related to video. In this section, we will also examine the ffmpeg filters closely.

In the third part, we will learn the details and methods of audio. In the fourth and fifth part, we will learn about Interlaced video and I-B-P frames. In the sixth part, we will learn what is codec and how does ffmpeg handle it. In the seventh part, we will try to explain how video encoding and audio encoding take place in ffmpeg, with sample codes. In the eighth part, we will try to explain how decoding works in fmmpeg and how to make a sample player with the help of SDL. In parts 9, 10 and 11, we will show the video and audio multiplexing methods. In the twelfth part, how to convert a video file into another format. We will learn all the transcoding steps. In the thirteenth part, we will learn all streaming protocols. In the last part, we will learn how to manage video and audio devices with ffmpeg.

FFMPEG - From Zero to Hero

FFMPEG - From Zero to Hero

If you ever wondered how the developers of YouTube or Vimeo cope with billions of video uploads or how Netflix processes its catalogue at scale or, again, if you want to discover how to create and develop your own video platform, you may want to know more about FFMPEG.

FFMPEG Quick Hacks

FFMPEG Quick Hacks

歡迎關注我的公衆號 “碼中人”,原創技術文章第一時間推送。

引用鏈接

[1] 網頁源碼: https://github.com/FFmpeg/web/blob/master/src/shame
[2] Download FFmpeg: https://ffmpeg.org/download.html
[3] FFmpeg Commander: https://alfg.dev/ffmpeg-commander/
[4] FFmpeg Command Generator: https://www.mrfdev.com/ffmpeg-command-generator
[5] ffmpeg - CMD Generator: https://cmdgenerator.phphe.com/cmd-generators/ffmpeg/
[6] ffmpeg string creator: http://www.mackinger.at/ffmpeg/
[7] HandBrake: Open Source Video Transcoder: https://handbrake.fr/
[8] HandBrake Documentation — Supported source formats: https://handbrake.fr/docs/en/1.3.0/technical/source-formats.html
[9] Quick Cut 發行版 - Gitee.com: https://gitee.com/haujet/QuickCut/releases
[10] Shutter Encoder encoding|converting video FREE PC|Mac: https://www.shutterencoder.com/en/
[11] ShanaEncoder Download - Shana: https://shana.pe.kr/shanaencoder_download
[12] Home | QWinFF: https://qwinff.github.io/
[13] robertsanseries/ciano: A multimedia file converter focused on simplicity.: https://github.com/robertsanseries/ciano
[14] App Ciano - Robert San: https://robertsanseries.github.io/ciano/
[15] Release FFaudioConverter 0.31.0 · Bleuzen/FFaudioConverter: https://github.com/Bleuzen/FFaudioConverter/releases/tag/v0.31.0
[16] FFmpeg 視頻處理入門教程 - 阮一峯的網絡日誌: https://www.ruanyifeng.com/blog/2020/01/ffmpeg.html
[17] FFmpeg 系列教程 視頻教程: https://space.bilibili.com/396616671/channel/collectiondetail?sid=628274&ctype=0
[18] FFmpeg 官方文檔翻譯 - 鏈滴: https://ld246.com/article/1595480295489

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/S1G_m6Y9UtnFdIGUV_OxKA