Skip to content

Instantly share code, notes, and snippets.

@wilt00
Last active August 29, 2021 20:16
Show Gist options
  • Save wilt00/148fe774360601d5070336d472b9c54d to your computer and use it in GitHub Desktop.
Save wilt00/148fe774360601d5070336d472b9c54d to your computer and use it in GitHub Desktop.
Find gifs in your Discord cache - WIP
# Tested with Powershell v7.1
# StackOverflow says the behavior of reading bytes from files changed sometime around v5, so
# this script might not work with the version of Powershell that comes with your computer
# https://stackoverflow.com/a/34559554
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
$cacheDir = "C:\Users\$env:USERNAME\AppData\Roaming\discord\Cache"
$gifDir = New-TemporaryDirectory
Copy-Item -Path "$cacheDir\*" -Destination $gifDir -Recurse
# Find .mp4 videos - requires ffmpeg to be on your PATH
Get-ChildItem $gifDir | Where-Object { !(ffmpeg -loglevel error -i $_ -map 0:1 -f null - 2>&1) } | Rename-Item -NewName { $_.Name + ".mp4" }
# https://www.garykessler.net/library/file_sigs.html
$magicGifBytes1 = 71,73,70,56,57,97
$magicGifBytes2 = 71,73,70,56,55,97
# Doesn't work yet
$magicWebmBytes = 26,69,123,163
function Is-Gif {
param ([string]$Path)
$fileBytes = Get-Content $Path -AsByteStream -TotalCount 7
!(Compare-Object $fileBytes $magicGifBytes1) -or !(Compare-Object $fileBytes $magicGifBytes2)
}
Get-ChildItem $gifDir | Where-Object { Is-Gif $_ } | Rename-Item -NewName { $_.Name + ".gif" }
Get-ChildItem $gifDir | Where-Object { !$_.Extension } | Remove-Item
explorer.exe $gifDir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment