使用PowerShell和ffmpeg編寫一個自動壓縮視頻腳本
引言
視頻文件往往體積龐大,尤其是高分辨率或高碼率的視頻。如果你經(jīng)常需要壓縮視頻以便存儲、傳輸或分享,那么手動輸入 ffmpeg 命令會很繁瑣。本文將教你如何編寫一個 PowerShell 自動化腳本,智能選擇壓縮參數(shù),支持以下模式:
- 默認有損壓縮(自動推薦 CRF)
- 手動指定 CRF 值
- 無損壓縮
- 目標大小模式(指定目標文件大小,自動計算碼率)
背景知識
什么是 CRF?
- CRF (Constant Rate Factor) 是 ffmpeg 在使用
libx264或libx265編碼時的一種恒定質量模式。 - 范圍:0–51
- 0 → 無損壓縮(文件極大)
- 18–23 → 高質量(接近無損)
- 24–28 → 中等質量(常用壓縮)
- 29+ → 低質量(極限壓縮)
無損壓縮
- 使用
-crf 0(H.264)或-x265-params lossless=1(H.265)。 - 文件體積通常不會比原始小很多,但保證質量不丟失。
目標大小模式
- 根據(jù)視頻時長和目標大小計算碼率:
目標碼率 (kbps) = 目標大小 (MB) x 8 x 1024 / 時長 (秒) - 使用
-b:v設置視頻碼率,音頻固定為128k。
腳本源碼:auto_compress.ps1
param(
[Parameter(Mandatory=$true)]
[string]$InputFile,
[Parameter(Mandatory=$true)]
[string]$OutputFile,
[Parameter(Mandatory=$false)]
[switch]$Lossless, # 是否啟用無損壓縮
[Parameter(Mandatory=$false)]
[int]$CRF, # 用戶可手動指定 CRF 值
[Parameter(Mandatory=$false)]
[int]$TargetSizeMB # 目標大小 (MB)
)
# 檢查 ffmpeg 是否可用
if (-not (Get-Command ffmpeg -ErrorAction SilentlyContinue)) {
Write-Host "錯誤: 未檢測到 ffmpeg,請先安裝并配置到 PATH。" -ForegroundColor Red
exit 1
}
# 獲取視頻信息
$info = & ffprobe -v error -select_streams v:0 -show_entries stream=width,height,r_frame_rate,bit_rate,duration -of default=noprint_wrappers=1 "$InputFile"
$width = ($info | Select-String "width=" | ForEach-Object { $_.ToString().Split("=")[1] })
$height = ($info | Select-String "height=" | ForEach-Object { $_.ToString().Split("=")[1] })
$fpsRaw = ($info | Select-String "r_frame_rate=" | ForEach-Object { $_.ToString().Split("=")[1] })
$bitrate = ($info | Select-String "bit_rate=" | ForEach-Object { $_.ToString().Split("=")[1] })
$duration = ($info | Select-String "duration=" | ForEach-Object { $_.ToString().Split("=")[1] })
# 計算幀率
$fpsParts = $fpsRaw -split "/"
if ($fpsParts.Count -eq 2) {
$fps = [math]::Round([double]$fpsParts[0] / [double]$fpsParts[1], 2)
} else {
$fps = [double]$fpsRaw
}
Write-Host "檢測到視頻: ${width}x${height}, ${fps}fps, 碼率=${bitrate}bps, 時長=${duration}s"
# 判斷壓縮模式
if ($Lossless) {
# 無損壓縮
$codec = "libx264"
$preset = "slow"
$extraParams = "-crf 0"
$audioCodec = "copy"
Write-Host "選擇無損壓縮: $codec $preset $extraParams, 音頻=$audioCodec"
} elseif ($TargetSizeMB) {
# 目標大小模式
$targetBitrate = [math]::Round(($TargetSizeMB * 8 * 1024) / [double]$duration)
Write-Host "目標大小模式: 目標=$TargetSizeMB MB, 計算碼率=$targetBitrate kbps"
$codec = "libx264"
$preset = "slow"
$extraParams = "-b:v ${targetBitrate}k"
$audioCodec = "aac -b:a 128k"
Write-Host "選擇目標大小壓縮: $codec $preset $extraParams, 音頻=$audioCodec"
} else {
# 有損壓縮,自動推薦 CRF
if ($CRF) {
$chosenCRF = $CRF
Write-Host "用戶指定 CRF=$chosenCRF"
} else {
if ([int]$width -ge 1920 -or [int]$bitrate -ge 4000000) {
$chosenCRF = 23
} elseif ([int]$width -ge 1280 -or [int]$bitrate -ge 2000000) {
$chosenCRF = 26
} else {
$chosenCRF = 28
}
Write-Host "自動推薦 CRF=$chosenCRF"
}
$codec = "libx264"
$preset = "slow"
$extraParams = "-crf $chosenCRF"
$audioCodec = "aac -b:a 128k"
Write-Host "選擇有損壓縮: $codec $preset $extraParams, 音頻=$audioCodec"
}
# 執(zhí)行壓縮
$ffmpegCmd = "ffmpeg -i `"$InputFile`" -c:v $codec -preset $preset $extraParams -c:a $audioCodec `"$OutputFile`""
Write-Host "執(zhí)行命令: $ffmpegCmd"
& cmd /c $ffmpegCmd
# 顯示文件大小對比
$origSize = (Get-Item $InputFile).Length
$newSize = (Get-Item $OutputFile).Length
Write-Host "原始大小: $([math]::Round($origSize/1MB,2)) MB"
Write-Host "壓縮后大小: $([math]::Round($newSize/1MB,2)) MB" -ForegroundColor Green
使用方法
默認有損壓縮(自動推薦 CRF)
./auto_compress.ps1 -InputFile "input.mp4" -OutputFile "output.mp4"
指定 CRF 值(例如 CRF=24,高質量)
./auto_compress.ps1 -InputFile "input.mp4" -OutputFile "output.mp4" -CRF 24
無損壓縮
./auto_compress.ps1 -InputFile "input.mp4" -OutputFile "output.mp4" -Lossless
目標大小模式(例如目標 100MB)
./auto_compress.ps1 -InputFile "input.mp4" -OutputFile "output.mp4" -TargetSizeMB 100
總結
這個腳本讓你在 Windows 下用 PowerShell 一鍵壓縮視頻,支持:
- 自動推薦 CRF 值(智能選擇質量與體積平衡)
- 手動指定 CRF 值
- 無損壓縮(保留原始質量)
- 目標大小模式(精確控制輸出文件大?。?/li>
到此這篇關于使用PowerShell和ffmpeg編寫一個自動壓縮視頻腳本的文章就介紹到這了,更多相關PowerShell ffmpeg自動壓縮視頻內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
PowerShell中查找字符串位置的IndexOf函數(shù)使用實例
這篇文章主要介紹了PowerShell中查找字符串位置的IndexOf函數(shù)使用實例,例子簡單明了,容易看懂,需要的朋友可以參考下2014-08-08
Powershell在一個會話中只允許執(zhí)行指定命令的方法
這篇文章主要介紹了Powershell在一個會話中只允許執(zhí)行指定命令的方法,使用本文的技巧可以達到控制權限的功能,需要的朋友可以參考下2014-11-11
PowerShell入門教程之遠程操作運行PowerShell的方法
這篇文章主要介紹了PowerShell入門教程之遠程操作運行PowerShell的方法,本文講解了配置遠程基礎結構、執(zhí)行遠程操作等內容,需要的朋友可以參考下2014-10-10
windows Powershell 快速編輯模式和標準模式
powershell控制臺有兩種模式,一個是快速編輯模式,一個是標準模式。2014-08-08
Powershell中請求WebServices并以JSON格式輸出結果
這篇文章主要介紹了Powershell中請求WebServices并以JSON格式輸出結果,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-03-03
PowerShell正則表達式(Regex)從右往左進行匹配方法代碼實例
這篇文章主要介紹了PowerShell正則表達式(Regex)從右往左進行匹配方法代碼實例,最重要的就是一個RightToLeft參數(shù)的運用,本文直接給出代碼實例,需要的朋友可以參考下2015-05-05

