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

使用PowerShell和ffmpeg編寫一個(gè)自動(dòng)壓縮視頻腳本

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

引言

視頻文件往往體積龐大,尤其是高分辨率或高碼率的視頻。如果你經(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 在使用 libx264libx265 編碼時(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)文章

最新評論

府谷县| 沙雅县| 青铜峡市| 龙游县| 长丰县| 宜春市| 松阳县| 柳河县| 承德市| 旬邑县| 县级市| 舟曲县| 钟山县| 枞阳县| 阿克| 白银市| 同心县| 宁明县| 汶上县| 沽源县| 越西县| 大丰市| 油尖旺区| 新丰县| 门头沟区| 怀仁县| 舞阳县| 民县| 香港| 荆州市| 石林| 常山县| 佛坪县| 广丰县| 陆良县| 威海市| 盐亭县| 嘉义市| 泰来县| 泗水县| 神农架林区|