ffmpeg
ffmpeg 是強大的跨平台多媒體處理工具,可進行影音轉檔、剪輯、串流等操作。
基本語法格式
ffmpeg [global options] [[infile options] -i infile]... {[outfile options] outfile}...| 區塊 | 說明 |
|---|---|
global options | 全域選項,影響整個轉檔流程,不針對特定檔案(例如:-y 自動覆蓋、-loglevel 設定輸出詳細程度)。 |
infile options | 針對單一輸入檔案或串流的參數設定(如:剪輯開始時間 -ss、指定格式 -f、指定解碼器等)。 |
-i infile | 指定輸入來源,可為本地影音檔、遠端串流(HTTP/HTTPS)、HLS 清單、RTSP/RTMP 串流、攝影機裝置等。 |
outfile options | 針對每個輸出檔案的設定選項(如:轉檔格式、指定編碼器 -c:v、bitrate、濾鏡等)。 |
outfile | 輸出檔案名稱與格式,副檔名可決定輸出格式,也可指定串流端點如 rtmp://、udp://。 |
輸入來源範例
| 輸入來源類型 | 範例 | 描述 |
|---|---|---|
| 本地檔案 | -i video.mp4 | 一般轉檔用 |
| HTTP 串流檔 | -i https://example.com/clip.mp4 | 可直接讀取遠端影片(需支援 HTTP/HTTPS) |
| HLS 清單 | -i https://example.com/stream.m3u8 | HLS 串流常見格式,支援直播或 VOD |
| RTMP 串流 | -i rtmp://a.rtmp.server/live/123 | 常用於直播串流平台的來源 |
| 攝影機設備 | -i /dev/video0 | 讀取 webcam 輸入(Linux) |
| 桌面螢幕錄製 | -f x11grab -i :0.0 | 錄製桌面畫面(Linux,需搭配 -f) |
輸出目標範例
| 輸出目標類型 | 範例 | 描述 |
|---|---|---|
| 輸出為 AAC 音訊 | output.aac | 萃取音訊或轉檔為 .aac 格式 |
| 輸出為 MP4 | output.mp4 | 最常見格式,支援廣泛 |
| 輸出為 HLS | output.m3u8 + -f hls | 將影片轉成 HLS 清單 |
| 推流到 RTMP | rtmp://live.server/app/stream | 輸出為直播串流 |
| 輸出為音訊裝置 | -f alsa default (Linux) | 播放至音效卡 |
求助指令
| 指令 | 說明 |
|---|---|
-h | 顯示基本選項 |
-h long | 顯示較多選項 |
-h full | 顯示所有選項(包含 codec 與格式細節) |
-h type=name | 顯示指定元件(解碼器/編碼器等)的選項 |
man ffmpeg | 查看完整使用手冊(Linux/Unix 系統) |
顯示功能 / 能力選項
| 指令 | 說明 |
|---|---|
-L | 顯示授權條款 |
-version | 顯示 ffmpeg 版本 |
-muxers | 顯示可用的 muxers(封裝器) |
-demuxers | 顯示可用的 demuxers(解封裝器) |
-devices | 顯示可用的設備列表 |
-decoders | 顯示可用的解碼器 |
-encoders | 顯示可用的編碼器 |
-filters | 顯示可用的濾鏡 |
-pix_fmts | 顯示像素格式 |
-layouts | 顯示標準聲道配置 |
-sample_fmts | 顯示音訊取樣格式 |
全域選項
| 選項 | 說明 |
|---|---|
-v <loglevel> | 設定日誌等級(quiet, panic, fatal, error, warning, info, verbose, debug) |
-y | 自動覆蓋已存在的輸出檔案 |
-n | 遇到同名檔案時不覆蓋 |
-stats | 顯示處理進度 |
-print_graphs | 將執行圖表資料印出到 stderr |
-print_graphs_file <filename> | 將圖表輸出到指定檔案 |
-print_graphs_format <format> | 指定圖表輸出格式(default, csv, json, xml, mermaid 等) |
輸入/輸出 檔案選項
通用(輸入和輸出都適用)
| 選項 | 說明 |
|---|---|
-f <fmt> | 強制指定容器格式(否則自動判斷) |
-t <duration> | 設定持續時間(秒) |
-to <time_stop> | 設定停止時間點(HH:MM:SS) |
-ss <time_off> | 設定開始時間點 |
僅輸出
| 選項 | 說明 |
|---|---|
-metadata[:<spec>] key=value | 添加 metadata 到輸出檔案中 |
每個 stream 的選項
| 選項 | 說明 |
|---|---|
-c[:<stream_spec>] <codec> | 指定使用的編碼器/解碼器(可用 copy 代表不轉碼) |
-filter[:<stream_spec>] <filter_graph> | 對指定的影音流套用濾鏡處理 |
stream_spec 可為:
- 整數編號(0, 1, 2…)
v(視訊)a(音訊)s(字幕)
視訊選項
| 選項 | 說明 |
|---|---|
-r[:<stream_spec>] | 設定影格率(frame rate) |
-aspect[:<stream_spec>] | 設定畫面比例(如 16:9、4:3) |
-vn | 移除視訊(僅輸出音訊) |
-vcodec <codec> | -c:v 的別名,用來指定視訊編碼器 |
-vf <filter_graph> | -filter:v 的別名,用來套用視訊濾鏡 |
-b | 設定位元率(請使用 -b:v 以避免混淆) |
音訊選項
| 選項 | 說明 |
|---|---|
-aq | 設定音訊品質(視 codec 而定) |
-ar[:<stream_spec>] | 設定取樣率(Hz) |
-ac[:<stream_spec>] | 設定聲道數(如單聲道 1、立體聲 2) |
-an | 移除音訊(僅輸出視訊) |
-acodec <codec> | -c:a 的別名,設定音訊的編碼器 |
-ab <bitrate> | -b:a 的別名,設定音訊位元率 |
-af <filter_graph> | -filter:a 的別名,套用音訊濾鏡 |
字幕選項
| 選項 | 說明 |
|---|---|
-sn | 移除字幕 |
-scodec <codec> | -c:s 的別名,指定字幕編碼器 |
輸入處理
| 指令範例 | 說明 |
|---|---|
ffmpeg -i input.mp4 output.mp4 | 讀取本地檔案 |
ffmpeg -i https://xxx.m3u8 -c copy output.ts | 下載並儲存 HLS 串流 |
ffmpeg -i rtmp://live.server/app/stream -c copy out.flv | 擷取 RTMP 串流 |
ffmpeg -protocol_whitelist "file,http,https,tcp,tls,crypto" -i "https://xxx.m3u8" -c:a copy -vn output.aac | 下載 HLS 音訊 |
剪輯影片(無轉檔)
| 指令範例 | 說明 |
|---|---|
ffmpeg -ss 00:00:30 -t 00:00:10 -i input.mp4 -c copy cut.mp4 | 從 30 秒處剪出 10 秒影片(快速) |
ffmpeg -i input.mp4 -ss 30 -t 10 -c:v copy -c:a copy out.mp4 | 同上,但更清楚指定音/視訊複製 |
音訊處理
| 指令範例 | 說明 |
|---|---|
ffmpeg -i input.mp4 -vn -acodec copy output.aac | 擷取 AAC 音訊 |
ffmpeg -i input.mp4 -vn -ar 44100 -ac 2 -b:a 192k out.mp3 | 轉檔為 MP3(設定採樣率與位元率) |
ffmpeg -i input.wav -filter:a "volume=1.5" output.wav | 提高音量(乘以 1.5 倍) |
視訊處理
| 指令範例 | 說明 |
|---|---|
ffmpeg -i input.mp4 -an -vcodec copy output.mp4 | 移除音訊,只保留視訊 |
ffmpeg -i input.mp4 -vf scale=1280:720 output.mp4 | 調整影片解析度為 1280x720 |
ffmpeg -i input.mp4 -r 30 output.mp4 | 設定輸出影片為 30 FPS |
ffmpeg -i input.mp4 -vf "transpose=1" rotated.mp4 | 順時針旋轉影片 90 度 |
擷取影格 / 螢幕截圖
| 指令範例 | 說明 |
|---|---|
ffmpeg -i input.mp4 -ss 00:00:05 -vframes 1 shot.png | 擷取第 5 秒的畫面為圖片 |
ffmpeg -i input.mp4 -vf fps=1 frames/out%d.png | 每秒擷取 1 張畫面 |
轉檔格式
| 指令範例 | 說明 |
|---|---|
ffmpeg -i input.mov -c:v libx264 -c:a aac output.mp4 | MOV → MP4(x264 + AAC) |
ffmpeg -i input.mp4 -c:v libvpx -c:a libvorbis output.webm | MP4 → WebM |
ffmpeg -i input.mp4 -c:v copy -c:a copy output.mkv | 無損快速轉封裝(MP4 → MKV) |
串流推播
| 指令範例 | 說明 |
|---|---|
ffmpeg -re -i input.mp4 -c copy -f flv rtmp://server/app/key | 推送本地影片至 RTMP 串流平台 |
ffmpeg -i input.mp4 -f hls out/index.m3u8 | 將影片切割成 HLS 格式 |
輸出設定與其他技巧
| 指令範例 | 說明 |
|---|---|
ffmpeg -i input.mp4 -metadata title="My Video" out.mp4 | 設定影片標題 metadata |
ffmpeg -i input.mp4 -movflags faststart output.mp4 | 加速 MP4 開頭播放(適合網路串流) |
ffmpeg -i input.mp4 -map 0 -c copy -f segment -segment_time 10 out%03d.mp4 | 將影片每 10 秒切割成片段 |
加入浮水印
| 指令範例 | 說明 |
|---|---|
ffmpeg -i input.mp4 -vf "drawtext=text='Hello':fontcolor=white:x=10:y=10" -c:a copy out.mp4 | 加入左上角白色文字浮水印 |
ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=10:10" -c:a copy out.mp4 | 加入圖片浮水印(左上角位置) |
ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=W-w-10:H-h-10" out.mp4 | 浮水印放右下角(使用寬高變數) |
音訊壓縮與處理
| 指令範例 | 說明 |
|---|---|
ffmpeg -i input.wav -c:a aac -b:a 96k output.m4a | 將音訊壓縮為 AAC 96kbps |
ffmpeg -i input.mp4 -filter:a "volume=0.5" output.mp4 | 降低音量為原本一半 |
ffmpeg -i input.mp4 -af "highpass=f=200, lowpass=f=3000" out.mp4 | 加入高/低通濾波器處理音訊 |
壓縮影片
| 指令範例 | 說明 |
|---|---|
ffmpeg -i input.mp4 -vcodec libx264 -crf 28 -preset slow -acodec aac -b:a 96k out.mp4 | 壓縮影片檔案(畫質適中、音訊壓縮) |
ffmpeg -i input.mp4 -vf scale=1280:-2 -c:v libx264 -crf 30 out.mp4 | 縮小解析度並壓縮 |
ffmpeg -i input.mp4 -c:v libx265 -crf 28 -c:a aac out.mp4 | 使用 H.265 壓縮(效能更高) |
-crf 數值越高壓縮越大(畫質越差),一般建議範圍:23(高畫質)~30(小檔案)裁切影片
| 指令範例 | 說明 |
|---|---|
ffmpeg -i input.mp4 -filter:v "crop=640:480:0:0" out.mp4 | 裁出 640x480 區域(從左上角開始) |
ffmpeg -i input.mp4 -filter:v "crop=in_w-100:in_h:50:0" out.mp4 | 左右裁邊,保留寬度減去 100,x 起點為 50 |
合併影片 / 聲音
無轉碼合併 MP4(需格式相同)
# 先建立 input.txt:
# file 'part1.mp4'
# file 'part2.mp4'
ffmpeg -f concat -safe 0 -i input.txt -c copy output.mp4合併聲音與影片
ffmpeg -i video.mp4 -i audio.m4a -c:v copy -c:a aac -strict experimental out.mp4影片加速 / 慢動作
| 指令範例 | 說明 |
|---|---|
ffmpeg -i input.mp4 -filter:v "setpts=0.5*PTS" output.mp4 | 快轉 2 倍速 |
ffmpeg -i input.mp4 -filter:v "setpts=2.0*PTS" output.mp4 | 慢動作 0.5 倍速 |
ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" out.mp4 | 快轉影片 + 聲音同步處理 |
CRF 與 Preset 對壓縮的影響
| 參數 | 說明 |
|---|---|
-crf N | Constant Rate Factor:數值越大壓縮越大(範圍 18~30,預設 23) |
-preset | 編碼速度選項:ultrafast、superfast、fast、medium、slow、veryslow。越慢壓縮效率越高。 |
產生 GIF
ffmpeg -i input.mp4 -ss 00:00:05 -t 3 -vf "fps=10,scale=320:-1:flags=lanczos" out.gif擷取第 5 秒開始的 3 秒影片轉為 GIF。
RTP / SDP 串流
讀取 MP4 並推送 RTP
ffmpeg -re -i "input.mp4" -protocol_whitelist file,rtp,udp -map 0:v -c:v libx265 -c:a libmp3lame -f rtp rtp://127.0.0.1:20003讀取 SDP 檔案
ffmpeg -analyzeduration 5000000 -probesize 5000000 -protocol_whitelist file,crypto,udp,rtp -i test.sdp -vcodec libx264 -y out.h264錄製 SDP 串流
ffmpeg -i a.sdp -protocol_whitelist "file,http,https,rtp,udp,tcp,tls" -vcodec copy -acodec aac -y output.mp4ffplay 播放
# 播放 MP4
ffplay video.mp4 -vf scale=800:600 -af volume=0.2
# 播放 SDP 串流
ffplay -i test.sdp -protocol_whitelist "file,udp,rtp" -vf scale=800:600 -af volume=0.2常用 Flag 說明
| Flag | 說明 |
|---|---|
-c:v | 設定視訊編碼器 |
-c:a | 設定音訊編碼器 |
-an | 移除音訊 |
-vn | 移除視訊 |
-re | 以即時速度讀取輸入,適合串流 |
-map 0 | 使用來源檔案的所有 stream |
-b:v | 設定視訊位元率 |
-crf | 設定 Constant Rate Factor |
-pix_fmt | 指定像素格式(如 yuv420p, rgb24) |
-protocol_whitelist | 指定允許的協定 |
-f rtp | 設定輸出格式為 RTP |