Created
July 28, 2025 07:16
-
-
Save justintien/93742d9e6aaa3cde30d04b97f91c8af9 to your computer and use it in GitHub Desktop.
FFmpeg 常用轉檔指令整理與說明
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FFmpeg 常用轉檔指令整理與說明 | |
本文件整理了常見的 `ffmpeg` 用法,包括 H.264 Profile 轉換、影片解析度縮放、保留音訊等操作方式,並提供詳細參數說明。 | |
- 這裡有各種格式影片素材可以下載: https://test-videos.co.uk/ | |
## 1. 使用 FFmpeg 產生有畫面與聲音的測試影片 | |
```sh | |
resolution=640x360 # 畫面大小 | |
duration=5 # 時長 seconds | |
fps=30 | |
ffmpeg -f lavfi \ | |
-i testsrc="s=$resolution":r=$fps:d=$duration \ | |
-f lavfi \ | |
-i sine=frequency=440:duration=$duration \ | |
-vf " | |
drawtext=fontfile=:text='Frame\: %{n}':x=50:y=50:fontsize=32:fontcolor=white, | |
drawtext=fontfile=:text='Time\: %{pts\:hms}':x=50:y=100:fontsize=32:fontcolor=white | |
" \ | |
-profile:v high \ | |
-pix_fmt yuv420p \ | |
-c:v libx264 \ | |
-c:a aac \ | |
-b:a 192k \ | |
-crf 18 \ | |
-preset veryfast \ | |
${resolution}-${fps}fps-${duration}s.mp4 | |
``` | |
| 參數 | 類型 | 說明 | | |
| ------------------ | -------------------- | -------------------------------------------------------------------------------------------------------------------------------- | | |
| `-f lavfi` | 全域 | 使用 FFmpeg 的濾鏡虛擬裝置(lavfi)作為輸入 | | |
| `-i testsrc="s=1920x1080":r=30:d=10` | 視訊輸入 | 使用內建的測試視訊源產生測試畫面<br>`s=$resolution` 設定畫面大小<br>`r=$fps` 設定每秒幀數<br>`d=$duration` 設定時長 | | |
| `-i sine=frequency=440:duration=10` | 音訊輸入 | 使用內建的 sine 波產生器產生聲音<br>`frequency=440` 指定音高為 440Hz (A 音)<br>`duration=$duration` 設定音訊長度 | | |
| `-vf "drawtext=fontfile=:text='Frame\: %{n} Time\: %{pts\:hms}':x=100:y=100:fontsize=40:fontcolor=white"` | 視訊濾鏡 | 在畫面上加上文字<br>`text='Frame: %{n} Time: %{pts:hms}'` 顯示幀數與時間戳<br>`x=100:y=100` 文字位置<br>`fontsize=40` 字體大小<br>`fontcolor=white` 白色文字 | | |
| `-profile:v high` | 編碼參數 | 設定 H.264 的 Profile 為 `high`,增加壓縮效率與相容性 | | |
| `-pix_fmt yuv420p` | 視訊格式 | 設定像素格式為 `yuv420p`,確保相容性(特別是對某些播放器) | | |
| `-c:v libx264` | 視訊編碼器 | 使用 `libx264` 作為視訊編碼器 | | |
| `-c:a aac` | 音訊編碼器 | 使用 `aac` 作為音訊編碼器 | | |
| `-b:a 192k` | 音訊位元率 | 設定音訊比特率為 192kbps | | |
| `-crf 18` | Constant Rate Factor | 品質設定(數值越小品質越高,範圍 0\~51) | | |
| `-preset veryfast` | 編碼速度 | 編碼速度設定,`veryfast` 表示快速編碼(檔案會較大) | | |
| `輸出檔名` | 檔案輸出 | 使用變數命名方式,如 `640x360-30fps-5s.mp4` | | |
--- | |
## 2. 將 High 10 Profile 轉換為 High Profile(保留音訊與位元率) | |
```sh | |
ffmpeg -i "$file" \ | |
-c:v libx264 \ | |
-profile:v high \ | |
-level:v 4.2 \ | |
-pix_fmt yuv420p \ | |
-b:v $bitrate \ | |
-c:a copy \ | |
"$temp_path" | |
``` | |
| 參數 | 說明 | | |
| ------------------ | ----------------------- | | |
| `-i "$file"` | 輸入檔案 | | |
| `-c:v libx264` | 使用 H.264 編碼器 | | |
| `-profile:v high` | 轉為 High Profile(適合通用播放) | | |
| `-level:v 4.2` | 設定 Profile 等級為 4.2 | | |
| `-pix_fmt yuv420p` | 設定像素格式為相容性最佳的 YUV 4:2:0 | | |
| `-b:v $bitrate` | 保持與原始影片相同的位元率 | | |
| `-c:a copy` | 音訊不重新編碼,直接複製 | | |
| `"$temp_path"` | 輸出路徑 | | |
## 3. 自動將影片縮小至指定高度(例如 540p) | |
```sh | |
file="" | |
temp_path="" | |
TARGET_HEIGHT=540 | |
resolution=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 "$file") | |
if [ "$resolution" -gt "$TARGET_HEIGHT" ]; then | |
echo "Resizing $filename..." | |
# Resize video to 540p height, preserving aspect ratio and using H.265 codec with original bitrate | |
# 註: bitrate 會自動縮小 (若指定一樣的 bitrate 檔案大小會很大) | |
ffmpeg -i "$file" \ | |
-vf "scale=-2:$TARGET_HEIGHT" \ | |
-c:a copy \ | |
"$temp_path" | |
else | |
echo "Skipping $filename, resolution is $resolution" | |
fi | |
``` | |
| 步驟 | 說明 | | |
| ------------------------- | ------------- | | |
| `ffprobe` | 取得影片高度 | | |
| `scale=-2:$TARGET_HEIGHT` | 自動計算寬度、保留比例縮放 | | |
| `-c:a copy` | 音訊不重新編碼 | | |
## 4. 轉換為 Baseline 或 High Profile(不設定目標位元率) | |
- other | |
```sh | |
ffmpeg -i input.mp4 \ | |
-c:v libx264 \ | |
-profile:v baseline \ | |
-level:v 4.2 \ | |
-pix_fmt yuv420p \ | |
-crf 23 \ | |
-c:a copy \ | |
output.mp4 | |
# 或轉成 High Profile: | |
ffmpeg -i input.mp4 \ | |
-c:v libx264 \ | |
-profile:v high \ | |
-level:v 4.2 \ | |
-pix_fmt yuv420p \ | |
-crf 23 \ | |
-c:a copy \ | |
output.mp4 | |
# 也可指定更低的等級(適合低階設備) | |
ffmpeg -i input.mp4 \ | |
-c:v libx264 \ | |
-profile:v baseline \ | |
-level:v 3.0 \ | |
-pix_fmt yuv420p \ | |
-crf 23 \ | |
-c:a copy \ | |
output.mp4 | |
``` | |
| 參數 | 說明 | | |
| --------------------- | ------------------------- | | |
| `-profile:v baseline` | 使用 Baseline Profile,兼容性最好 | | |
| `-profile:v high` | 使用 High Profile,壓縮效率更佳 | | |
| `-level:v` | Profile 等級,選擇依裝置相容性 | | |
| `-pix_fmt yuv420p` | 保證播放器兼容 | | |
| `-crf 23` | 調整輸出品質(數值越小品質越高) | | |
| `-c:a copy` | 保留原始音訊流 | | |
## 補充說明 | |
- Profile 比較: | |
| Profile | 說明 | 使用情境 | | |
| -------- | -------- | ------- | | |
| Baseline | 支援最廣 | 舊設備、瀏覽器 | | |
| Main | 平衡壓縮與相容性 | 主流播放平台 | | |
| High | 高壓縮效率與畫質 | 高解析影片製作 | | |
- 像素格式建議:yuv420p 是播放相容性最好的選擇,特別是要支援 iOS / Safari 播放器。 | |
- CRF 推薦值: | |
- 18–20:高品質 | |
- 23:預設值,品質與大小平衡 | |
- 28:檔案小但品質差 | |
## 其它 | |
- 這裡有各種格式影片素材可以下載: https://test-videos.co.uk/ | |
- 可以線上查看media資訊的工具: https://mediainfo.js.org/demo/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment