Skip to content

Instantly share code, notes, and snippets.

@itsuki-hayashi
Last active January 1, 2024 12:48
Show Gist options
  • Save itsuki-hayashi/66ae8c73e1cc7aa7743db6f79e1f9cc6 to your computer and use it in GitHub Desktop.
Save itsuki-hayashi/66ae8c73e1cc7aa7743db6f79e1f9cc6 to your computer and use it in GitHub Desktop.
Convert AVIF to JPG in PowerShell with ImageMagick
# Single thread
Get-ChildItem -LiteralPath . -Filter *.avif | ForEach-Object {
$jpgFileName = [System.IO.Path]::ChangeExtension($_.Name, '.jpg')
magick convert $_.FullName $jpgFileName
}
# Or parallel
$scriptBlock = {
param($inputFile)
$outputFile = [System.IO.Path]::ChangeExtension($inputFile, '.jpg')
magick convert $inputFile $outputFile
}
$maxConcurrentJobs = [Environment]::ProcessorCount
$jobs = @()
Get-ChildItem -LiteralPath . -Filter *.avif | ForEach-Object {
if ($jobs.Count -ge $maxConcurrentJobs) {
$finishedJob = Wait-Job -Job $jobs -Any
Receive-Job -Job $finishedJob
$jobs = $jobs | Where-Object { $_.State -ne 'Completed' }
}
$job = Start-Job -ScriptBlock $scriptBlock -ArgumentList $_.FullName
$jobs += $job
}
# Wait for all jobs to complete and receive their output
Get-Job | Wait-Job
Get-Job | Receive-Job
# Clean up the job objects
Get-Job | Remove-Job
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment