影片元資料分析指南:用 FFprobe 讀懂容器、編碼器、幀率、位元率
收到一個影片檔案,想知道是什麼編碼、解析度、位元率。FFprobe 是讀取影片資訊的必備工具。這篇解說 FFprobe 用法和影片元資料的各欄位意義。
FFprobe 基本用法
1. 基本資訊
bash
ffprobe video.mp4輸出範例:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.76.100
Duration: 00:01:23.45, start: 0.000000, bitrate: 2500 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 2350 kb/s, 30 fps, 30 tbr, 90k tbn, 60 tbc (default)
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)2. 詳細資訊
bash
ffprobe -show_format -show_streams video.mp43. JSON 輸出
bash
ffprobe -v error -show_format -show_streams -of json video.mp4輸出範例:
json
{
"streams": [
{
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "High",
"codec_type": "video",
"width": 1920,
"height": 1080,
"pix_fmt": "yuv420p",
"r_frame_rate": "30/1",
"avg_frame_rate": "30/1",
"bit_rate": "2350000",
"duration": "83.450000"
},
{
"index": 1,
"codec_name": "aac",
"codec_type": "audio",
"sample_rate": "44100",
"channels": 2,
"bit_rate": "128000",
"duration": "83.450000"
}
],
"format": {
"filename": "video.mp4",
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
"duration": "83.450000",
"size": "26078125",
"bit_rate": "2500000"
}
}重要元資料欄位
容器格式
bash
# 容器格式
ffprobe -v error -show_entries format=format_name -of default=noprint_wrappers=1:nokey=1 video.mp4
# 輸出: mov,mp4,m4a,3gp,3g2,mj2視訊串流
bash
# 視訊編碼器
ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 video.mp4
# 輸出: h264
# 解析度
ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 video.mp4
# 輸出: 1920,1080
# 幀率
ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate -of default=noprint_wrappers=1:nokey=1 video.mp4
# 輸出: 30/1
# 視訊位元率
ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 video.mp4
# 輸出: 2350000音訊串流
bash
# 音訊編碼器
ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 video.mp4
# 輸出: aac
# 取樣率
ffprobe -v error -select_streams a:0 -show_entries stream=sample_rate -of default=noprint_wrappers=1:nokey=1 video.mp4
# 輸出: 44100
# 聲道數
ffprobe -v error -select_streams a:0 -show_entries stream=channels -of default=noprint_wrappers=1:nokey=1 video.mp4
# 輸出: 2
# 音訊位元率
ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 video.mp4
# 輸出: 128000格式資訊
bash
# 時長
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 video.mp4
# 輸出: 83.450000
# 檔案大小
ffprobe -v error -show_entries format=size -of default=noprint_wrappers=1:nokey=1 video.mp4
# 輸出: 26078125
# 總位元率
ffprobe -v error -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1 video.mp4
# 輸出: 2500000進階用法
1. 取得所有元資料標籤
bash
ffprobe -v error -show_entries format_tags video.mp4輸出範例:
[TAG]major_brand=isom
[TAG]minor_version=512
[TAG]compatible_brands=isomiso2avc1mp41
[TAG]title=我的影片
[TAG]artist=作者名
[TAG]comment=影片說明
[TAG]encoder=Lavf58.76.1002. 取得關鍵幀位置
bash
ffprobe -v error -select_streams v:0 -show_entries packet=pts_time,flags -of csv video.mp4 | grep "K"3. 取得幀資訊
bash
# 所有幀的時間戳
ffprobe -v error -select_streams v:0 -show_entries frame=pts_time,pict_type -of csv video.mp4
# 統計 I/P/B 幀數量
ffprobe -v error -select_streams v:0 -show_entries frame=pict_type -of csv video.mp4 | sort | uniq -c4. 偵測場景切換
bash
ffprobe -v error -select_streams v:0 -show_entries frame=pts_time,pict_type -of csv video.mp4 | grep "I"元資料欄位意義
視訊串流主要欄位
| 欄位 | 意義 | 範例 |
|---|---|---|
codec_name | 視訊編碼器 | h264、hevc、vp9 |
profile | 編碼器設定檔 | High、Main、Baseline |
width | 寬度(像素) | 1920 |
height | 高度(像素) | 1080 |
pix_fmt | 像素格式 | yuv420p、yuv444p |
r_frame_rate | 基礎幀率 | 30/1 |
avg_frame_rate | 平均幀率 | 30/1 |
bit_rate | 位元率 | 2350000 |
duration | 時長(秒) | 83.450000 |
color_space | 色彩空間 | bt709 |
color_range | 色彩範圍 | tv、pc |
音訊串流主要欄位
| 欄位 | 意義 | 範例 |
|---|---|---|
codec_name | 音訊編碼器 | aac、mp3、opus |
profile | 編碼器設定檔 | LC、HE |
sample_rate | 取樣率 | 44100、48000 |
channels | 聲道數 | 2(立體聲)、6(5.1) |
channel_layout | 聲道配置 | stereo、5.1 |
bit_rate | 位元率 | 128000 |
duration | 時長(秒) | 83.450000 |
格式主要欄位
| 欄位 | 意義 | 範例 |
|---|---|---|
format_name | 容器格式 | mov,mp4,m4a |
duration | 總時長 | 83.450000 |
size | 檔案大小(位元組) | 26078125 |
bit_rate | 總位元率 | 2500000 |
實用範本
1. 快速影片資訊
bash
ffprobe -v error -show_entries format=duration,size,bit_rate:stream=codec_name,width,height,r_frame_rate -of default=noprint_wrappers=1 video.mp42. 視訊和音訊摘要
bash
ffprobe -v error -of csv=p=0 -show_entries stream=codec_type,codec_name,width,height,r_frame_rate,sample_rate,channels,bit_rate video.mp4輸出範例:
video,h264,1920,1080,30/1,N/A,N/A,2350000
audio,aac,N/A,N/A,N/A,44100,2,1280003. 批次分析
bash
# CSV 標題
echo "filename,format,duration,size,video_codec,width,height,fps,audio_codec,sample_rate,channels" > info.csv
# 批次處理
for f in *.mp4; do
info=$(ffprobe -v error -of csv=p=0 -show_entries "format=format_name,duration,size:stream=codec_name,width,height,r_frame_rate,sample_rate,channels" "$f")
echo "$f,$info" >> info.csv
done4. Python 指令碼
python
import subprocess
import json
def get_video_info(filepath):
cmd = [
'ffprobe',
'-v', 'error',
'-show_format', '-show_streams',
'-of', 'json',
filepath
]
result = subprocess.run(cmd, capture_output=True, text=True)
return json.loads(result.stdout)
info = get_video_info('video.mp4')
video_stream = next(s for s in info['streams'] if s['codec_type'] == 'video')
audio_stream = next(s for s in info['streams'] if s['codec_type'] == 'audio')
print(f"視訊編碼器: {video_stream['codec_name']}")
print(f"解析度: {video_stream['width']}x{video_stream['height']}")
print(f"幀率: {video_stream['r_frame_rate']}")
print(f"音訊編碼器: {audio_stream['codec_name']}")
print(f"取樣率: {audio_stream['sample_rate']} Hz")
print(f"時長: {float(info['format']['duration']):.2f} 秒")常見問題
1. 位元率顯示 N/A
原因: 部分容器不儲存位元率資訊
解決方案: 計算
bash
# 用檔案大小和時長計算
size=$(ffprobe -v error -show_entries format=size -of default=noprint_wrappers=1:nokey=1 video.mp4)
duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 video.mp4)
bitrate=$(echo "scale=0; $size * 8 / $duration" | bc)
echo "位元率: $bitrate bps"2. 幀率不正確
原因: r_frame_rate 是基礎幀率,avg_frame_rate 是平均幀率
解決方案: VBR 影片用 avg_frame_rate
3. 時長不正確
原因: 容器時長和實際時長不一致
解決方案: 用串流時長
bash
ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 video.mp4總結
FFprobe 是影片元資料分析的必備工具:
- 基本資訊:
ffprobe video.mp4 - 詳細資訊:
ffprobe -show_format -show_streams video.mp4 - JSON 輸出:
-of json - CSV 輸出:
-of csv=p=0
快速參考:
- 基本:
ffprobe video.mp4 - JSON:
ffprobe -v error -show_format -show_streams -of json video.mp4 - 指定串流:
-select_streams v:0(視訊)、a:0(音訊) - 特定欄位:
-show_entries stream=codec_name,width,height