crosssubtitle-ai/scripts/prepare-ffmpeg-windows.ps1

84 lines
3.3 KiB
PowerShell

param(
[string]$FFmpegSource = ""
)
$ErrorActionPreference = "Stop"
$ROOT_DIR = Split-Path -Parent (Split-Path -Parent $PSCommandPath)
$VENDOR_DIR = Join-Path $ROOT_DIR "src-tauri\vendor\ffmpeg\windows-x86_64"
$BIN_DIR = Join-Path $VENDOR_DIR "bin"
$FFMPEG_URL = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.7z"
# Ensure target directory exists
if (-not (Test-Path $BIN_DIR)) {
New-Item -ItemType Directory -Path $BIN_DIR -Force | Out-Null
}
# Strategy 1: if -FFmpegSource explicitly provided, use it
if ($FFmpegSource) {
if (-not (Test-Path $FFmpegSource)) {
Write-Host "ffmpeg not found at: $FFmpegSource" -ForegroundColor Red
exit 1
}
Write-Host "Copying ffmpeg from: $FFmpegSource"
Copy-Item -Path $FFmpegSource -Destination (Join-Path $BIN_DIR "ffmpeg.exe") -Force
Write-Host "ffmpeg bundled at: $BIN_DIR\ffmpeg.exe" -ForegroundColor Green
exit 0
}
# Strategy 2: download Gyan Essentials build via 7-Zip
$sevenZipPaths = @(
"${env:ProgramFiles}\7-Zip\7z.exe",
"${env:ProgramFiles(x86)}\7-Zip\7z.exe",
"${env:LOCALAPPDATA}\Programs\7-Zip\7z.exe"
)
$sevenZip = $sevenZipPaths | Where-Object { Test-Path $_ } | Select-Object -First 1
if ($sevenZip) {
$tempDir = Join-Path $env:TEMP "ffmpeg-essentials"
$archivePath = Join-Path $tempDir "ffmpeg.7z"
try {
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
Write-Host "Downloading ffmpeg essentials from: $FFMPEG_URL" -ForegroundColor Cyan
Invoke-WebRequest -Uri $FFMPEG_URL -OutFile $archivePath -UseBasicParsing
Write-Host "Extracting..." -ForegroundColor Cyan
& $sevenZip x $archivePath -o"$tempDir\out" -y -bso0 | Out-Null
$extractedExe = Get-ChildItem -Path "$tempDir\out" -Recurse -Filter "ffmpeg.exe" | Select-Object -First 1 -ExpandProperty FullName
if (-not $extractedExe) {
throw "ffmpeg.exe not found in extracted archive"
}
Copy-Item -Path $extractedExe -Destination (Join-Path $BIN_DIR "ffmpeg.exe") -Force
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
$size = (Get-Item (Join-Path $BIN_DIR "ffmpeg.exe")).Length / 1MB
Write-Host "ffmpeg essentials bundled at: $BIN_DIR\ffmpeg.exe ($([math]::Round($size)) MB)" -ForegroundColor Green
exit 0
}
catch {
Write-Host "Download/extraction failed: $_" -ForegroundColor Yellow
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
else {
Write-Host "7-Zip not found, recommend installing it for automatic download." -ForegroundColor Yellow
Write-Host "Download manually from: $FFMPEG_URL and extract ffmpeg.exe to $BIN_DIR" -ForegroundColor Yellow
}
# Strategy 3: fall back to copying system ffmpeg
$systemFFmpeg = (Get-Command ffmpeg -ErrorAction SilentlyContinue).Source
if ($systemFFmpeg) {
Write-Host "Falling back to system ffmpeg: $systemFFmpeg" -ForegroundColor Cyan
Copy-Item -Path $systemFFmpeg -Destination (Join-Path $BIN_DIR "ffmpeg.exe") -Force
$size = (Get-Item (Join-Path $BIN_DIR "ffmpeg.exe")).Length / 1MB
Write-Host "ffmpeg bundled at: $BIN_DIR\ffmpeg.exe ($([math]::Round($size)) MB)" -ForegroundColor Green
exit 0
}
Write-Host "ERROR: No ffmpeg found. Install ffmpeg or specify path via -FFmpegSource." -ForegroundColor Red
exit 1