使用PowerShell和ffmpeg編寫一個(gè)自動(dòng)壓縮視頻腳本
引言
視頻文件往往體積龐大,尤其是高分辨率或高碼率的視頻。如果你經(jīng)常需要壓縮視頻以便存儲(chǔ)、傳輸或分享,那么手動(dòng)輸入 ffmpeg 命令會(huì)很繁瑣。本文將教你如何編寫一個(gè) PowerShell 自動(dòng)化腳本,智能選擇壓縮參數(shù),支持以下模式:
- 默認(rèn)有損壓縮(自動(dòng)推薦 CRF)
- 手動(dòng)指定 CRF 值
- 無損壓縮
- 目標(biāo)大小模式(指定目標(biāo)文件大小,自動(dòng)計(jì)算碼率)
背景知識
什么是 CRF?
- CRF (Constant Rate Factor) 是 ffmpeg 在使用
libx264或libx265編碼時(shí)的一種恒定質(zhì)量模式。 - 范圍:0–51
- 0 → 無損壓縮(文件極大)
- 18–23 → 高質(zhì)量(接近無損)
- 24–28 → 中等質(zhì)量(常用壓縮)
- 29+ → 低質(zhì)量(極限壓縮)
無損壓縮
- 使用
-crf 0(H.264)或-x265-params lossless=1(H.265)。 - 文件體積通常不會(huì)比原始小很多,但保證質(zhì)量不丟失。
目標(biāo)大小模式
- 根據(jù)視頻時(shí)長和目標(biāo)大小計(jì)算碼率:
目標(biāo)碼率 (kbps) = 目標(biāo)大小 (MB) x 8 x 1024 / 時(shí)長 (秒) - 使用
-b:v設(shè)置視頻碼率,音頻固定為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, # 用戶可手動(dòng)指定 CRF 值
[Parameter(Mandatory=$false)]
[int]$TargetSizeMB # 目標(biāo)大小 (MB)
)
# 檢查 ffmpeg 是否可用
if (-not (Get-Command ffmpeg -ErrorAction SilentlyContinue)) {
Write-Host "錯(cuò)誤: 未檢測到 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] })
# 計(jì)算幀率
$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, 時(shí)長=${duration}s"
# 判斷壓縮模式
if ($Lossless) {
# 無損壓縮
$codec = "libx264"
$preset = "slow"
$extraParams = "-crf 0"
$audioCodec = "copy"
Write-Host "選擇無損壓縮: $codec $preset $extraParams, 音頻=$audioCodec"
} elseif ($TargetSizeMB) {
# 目標(biāo)大小模式
$targetBitrate = [math]::Round(($TargetSizeMB * 8 * 1024) / [double]$duration)
Write-Host "目標(biāo)大小模式: 目標(biāo)=$TargetSizeMB MB, 計(jì)算碼率=$targetBitrate kbps"
$codec = "libx264"
$preset = "slow"
$extraParams = "-b:v ${targetBitrate}k"
$audioCodec = "aac -b:a 128k"
Write-Host "選擇目標(biāo)大小壓縮: $codec $preset $extraParams, 音頻=$audioCodec"
} else {
# 有損壓縮,自動(dòng)推薦 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 "自動(dòng)推薦 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
使用方法
默認(rèn)有損壓縮(自動(dòng)推薦 CRF)
./auto_compress.ps1 -InputFile "input.mp4" -OutputFile "output.mp4"
指定 CRF 值(例如 CRF=24,高質(zhì)量)
./auto_compress.ps1 -InputFile "input.mp4" -OutputFile "output.mp4" -CRF 24
無損壓縮
./auto_compress.ps1 -InputFile "input.mp4" -OutputFile "output.mp4" -Lossless
目標(biāo)大小模式(例如目標(biāo) 100MB)
./auto_compress.ps1 -InputFile "input.mp4" -OutputFile "output.mp4" -TargetSizeMB 100
總結(jié)
這個(gè)腳本讓你在 Windows 下用 PowerShell 一鍵壓縮視頻,支持:
- 自動(dòng)推薦 CRF 值(智能選擇質(zhì)量與體積平衡)
- 手動(dòng)指定 CRF 值
- 無損壓縮(保留原始質(zhì)量)
- 目標(biāo)大小模式(精確控制輸出文件大?。?/li>
到此這篇關(guān)于使用PowerShell和ffmpeg編寫一個(gè)自動(dòng)壓縮視頻腳本的文章就介紹到這了,更多相關(guān)PowerShell ffmpeg自動(dòng)壓縮視頻內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PowerShell中查找字符串位置的IndexOf函數(shù)使用實(shí)例
這篇文章主要介紹了PowerShell中查找字符串位置的IndexOf函數(shù)使用實(shí)例,例子簡單明了,容易看懂,需要的朋友可以參考下2014-08-08
Powershell在一個(gè)會(huì)話中只允許執(zhí)行指定命令的方法
這篇文章主要介紹了Powershell在一個(gè)會(huì)話中只允許執(zhí)行指定命令的方法,使用本文的技巧可以達(dá)到控制權(quán)限的功能,需要的朋友可以參考下2014-11-11
PowerShell入門教程之遠(yuǎn)程操作運(yùn)行PowerShell的方法
這篇文章主要介紹了PowerShell入門教程之遠(yuǎn)程操作運(yùn)行PowerShell的方法,本文講解了配置遠(yuǎn)程基礎(chǔ)結(jié)構(gòu)、執(zhí)行遠(yuǎn)程操作等內(nèi)容,需要的朋友可以參考下2014-10-10
windows Powershell 快速編輯模式和標(biāo)準(zhǔn)模式
powershell控制臺有兩種模式,一個(gè)是快速編輯模式,一個(gè)是標(biāo)準(zhǔn)模式。2014-08-08
Powershell中請求WebServices并以JSON格式輸出結(jié)果
這篇文章主要介紹了Powershell中請求WebServices并以JSON格式輸出結(jié)果,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-03-03
PowerShell正則表達(dá)式(Regex)從右往左進(jìn)行匹配方法代碼實(shí)例
這篇文章主要介紹了PowerShell正則表達(dá)式(Regex)從右往左進(jìn)行匹配方法代碼實(shí)例,最重要的就是一個(gè)RightToLeft參數(shù)的運(yùn)用,本文直接給出代碼實(shí)例,需要的朋友可以參考下2015-05-05
PowerShell 遠(yuǎn)程執(zhí)行任務(wù)的方法步驟
這篇文章主要介紹了PowerShell 遠(yuǎn)程執(zhí)行任務(wù)的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12

