Skip to content

Instantly share code, notes, and snippets.

@book000
Created January 2, 2025 09:32
Show Gist options
  • Save book000/2c87637df45463a11816b81ead357e37 to your computer and use it in GitHub Desktop.
Save book000/2c87637df45463a11816b81ead357e37 to your computer and use it in GitHub Desktop.
param(
[string]$Year, # yyyy部分を引数として指定
[int]$MaxParallel = 10 # 並列実行の最大数を指定(デフォルト10)
)
$sourceFolder = "N:\Users\tomachi\Pictures\VRChat"
$destinationFolder = "N:\Users\tomachi\Pictures\VRChat\webp"
$ffmpegPath = "ffmpeg"
function Wait-JobsAndUpdateProgress {
param([array]$jobs, [int]$totalFiles)
$completedInBatch = 0
while ($jobs.Count -gt 0) {
$readyJobs = $jobs | Where-Object { $_.State -eq 'Completed' }
foreach ($job in $readyJobs) {
$result = Receive-Job $job
Write-Host $result
Remove-Job $job
$jobs = $jobs | Where-Object { $_.Id -ne $job.Id }
$completedInBatch++
$completedCount = $completedFiles + $completedInBatch
Write-Progress -Activity "Converting PNG to WebP" -Status "Processing... ($completedCount/$totalFiles)" `
-PercentComplete ($completedCount / $totalFiles * 100)
}
Start-Sleep -Milliseconds 500
}
return $completedInBatch
}
$folders = Get-ChildItem -Path $sourceFolder -Directory | Where-Object {
$_.Name -match "^\d{4}-\d{2}$" -and $_.Name.StartsWith($Year)
}
$files = foreach ($folder in $folders) {
Get-ChildItem -Path $folder.FullName -Filter "*.png"
}
$totalFiles = $files.Count
$completedFiles = 0
$jobs = @()
foreach ($file in $files) {
$job = Start-Job -ScriptBlock {
param($file, $sourceFolder, $destinationFolder, $ffmpegPath)
$sourceFile = $file.FullName
$relativePath = $file.DirectoryName.Substring($sourceFolder.Length).TrimStart('\')
$destinationPath = Join-Path $destinationFolder $relativePath
$destinationFile = Join-Path $destinationPath ([System.IO.Path]::ChangeExtension($file.Name, ".webp"))
if (Test-Path $destinationFile) {
return "Skipped: $destinationFile"
}
if (-not (Test-Path $destinationPath)) {
New-Item -ItemType Directory -Path $destinationPath -Force | Out-Null
}
& $ffmpegPath -i $sourceFile -qscale:v 75 $destinationFile *>$null 2>&1
return "Converted: $sourceFile -> $destinationFile"
} -ArgumentList $file, $sourceFolder, $destinationFolder, $ffmpegPath
$jobs += $job
# 並列実行を制限
if ($jobs.Count -ge $MaxParallel) {
$completedFiles += Wait-JobsAndUpdateProgress $jobs $totalFiles
$jobs = @()
}
}
$completedFiles += Wait-JobsAndUpdateProgress $jobs $totalFiles
$jobs | Remove-Job
Write-Progress -Activity "Converting PNG to WebP" -Status "Done" -Completed
Write-Host "Conversion completed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment