最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用PowerShell和ffmpeg編寫一個自動壓縮視頻腳本

 更新時間:2025年12月03日 09:23:40   作者:愛宇陽  
視頻文件往往體積龐大,尤其是高分辨率或高碼率的視頻,如果你經(jīng)常需要壓縮視頻以便存儲、傳輸或分享,那么手動輸入 ffmpeg 命令會很繁瑣,本文將教你如何編寫一個 PowerShell 自動化腳本,智能選擇壓縮參數(shù),需要的朋友可以參考下

引言

視頻文件往往體積龐大,尤其是高分辨率或高碼率的視頻。如果你經(jīng)常需要壓縮視頻以便存儲、傳輸或分享,那么手動輸入 ffmpeg 命令會很繁瑣。本文將教你如何編寫一個 PowerShell 自動化腳本,智能選擇壓縮參數(shù),支持以下模式:

  • 默認有損壓縮(自動推薦 CRF)
  • 手動指定 CRF 值
  • 無損壓縮
  • 目標大小模式(指定目標文件大小,自動計算碼率)

背景知識

什么是 CRF?

  • CRF (Constant Rate Factor) 是 ffmpeg 在使用 libx264libx265 編碼時的一種恒定質量模式。
  • 范圍: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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

仪征市| 桃园市| 鹤壁市| 青岛市| 酒泉市| 和静县| 温泉县| 铜梁县| 湖口县| 罗田县| 巴林左旗| 达孜县| 教育| 百色市| 商都县| 马尔康县| 监利县| 容城县| 齐河县| 慈溪市| 周至县| 新昌县| 大渡口区| 鄂托克前旗| 望江县| 浏阳市| 游戏| 嘉定区| 饶平县| 临夏市| 顺平县| 卓资县| 芮城县| 鄂托克前旗| 永州市| 惠水县| 泾源县| 叶城县| 藁城市| 区。| 南投市|