Last active
March 10, 2023 18:51
-
-
Save mavaddat/1e75ab2df980aceac75d7d065f3210a1 to your computer and use it in GitHub Desktop.
Windows ffmpeg updater in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Updates FFMPEG to the latest version | |
$ffmpegGH = @{OWNER = 'GyanD'; REPO = 'codexffmpeg' } | |
$extractTo = Join-Path -Path $env:USERPROFILE -ChildPath 'ffmpeg' | |
if (-not ([System.Management.Automation.PSTypeName]'SevenZipExtractor.ArchiveFile').Type) | |
{ | |
if (-not (Get-Package -Name SevenZipExtractor -ErrorAction Ignore)) | |
{ | |
Install-Package SevenZipExtractor -Source nuget.org -Scope CurrentUser | |
} | |
Get-Package -Name SevenZipExtractor | ForEach-Object { Add-Type -LiteralPath ($_.Source | Split-Path | Get-ChildItem -Filter 'netstandard*' -Recurse -Directory | Get-ChildItem -Filter *.dll -Recurse -File ).FullName } | |
} | |
$latestRelease = Invoke-RestMethod -Uri "https://api.github.com/repos/$($ffmpegGH.OWNER)/$($ffmpegGH.REPO)/releases/latest" -Method Get -ContentType 'application/vnd.github+json' | |
$fullBuildAsset = $latestRelease.assets | Where-Object -FilterScript { $_.name -like '*full_build.7z' } | |
if ((Test-Path -Path ($ffmpegPath = Join-Path -Path $extractTo -ChildPath 'bin' -AdditionalChildPath 'ffmpeg.exe') -PathType Leaf) -and ($fullBuildAsset.name | Select-String -Pattern 'ffmpeg-([\d\.\w_-]+?)\.7z' | ForEach-Object { $_.Matches.Groups[1].Value }) -eq (&$ffmpegPath -h 2>&1 | Select-Object -First 1 | Select-String -Pattern 'ffmpeg version ([\d\.\w_-]+?)-www\.gyan\.dev Copyright \(c\) \d+-\d+ the FFmpeg developers' | ForEach-Object { $_.Matches.Groups[1].Value })) | |
{ | |
'Already installed' | Write-Verbose | |
return 1 | |
} | |
$hashExpected = Invoke-RestMethod -Uri "https://www.gyan.dev/ffmpeg/builds/packages/$($fullBuildAsset.name).sha256" | |
$downloaded = Invoke-WebRequest -Uri ($fullBuildAsset.browser_download_url) -ContentType 'application/octet-stream' | |
if ($downloaded.Content -isnot [byte[]]) | |
{ | |
$encoding = $downloaded.Encoding | |
$bytes = $encoding.GetBytes($response.Content) | |
$sevenZipStream = [System.IO.MemoryStream]::new($bytes) | |
} | |
else | |
{ | |
$sevenZipStream = [System.IO.MemoryStream]::new($downloaded.Content) | |
} | |
$szExtractor = New-Object -TypeName SevenZipExtractor.ArchiveFile -ArgumentList @($sevenZipStream) | |
$szExtractor.Entries | Out-Null | |
$sevenZipStream.Position = 0 # Reset the stream position to the beginning | |
$hashCalculated = Get-FileHash -Algorithm SHA256 -InputStream $sevenZipStream | Select-Object -ExpandProperty Hash | |
if ($hashCalculated -eq $hashExpected -and $szExtractor.Entries.Count -gt 0) | |
{ | |
foreach ($entry in $szExtractor.Entries[1..($szExtractor.Entries.Count)]) # start at 1 so as to not duplicate root dir | |
{ | |
$filename = $entry.FileName -split [regex]::Escape([System.IO.Path]::DirectorySeparatorChar) | Select-Object -Skip 1 # Skip the first element which is the root dir | |
$extractPath = $filename.Count -gt 1 ? ( Join-Path -Path $extractTo -ChildPath $(Join-Path @filename)) : (Join-Path -Path $extractTo -ChildPath $filename) | |
$entry.Extract($extractPath) | |
} | |
} | |
else | |
{ | |
'Hash mismatch. Aborting.' | Write-Error | |
return 2 | |
} | |
# Copy entire script | |
# [System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Clipboard -Raw))) | Set-Clipboard |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment