Skip to content

Instantly share code, notes, and snippets.

@ToshY
Created August 31, 2019 10:10
Show Gist options
  • Save ToshY/aab281f7bbfb968adac912f0ba9a26ef to your computer and use it in GitHub Desktop.
Save ToshY/aab281f7bbfb968adac912f0ba9a26ef to your computer and use it in GitHub Desktop.
Clean ASS files by removing unused styles
# The directory of the ASS files that need to be cleaned
$dir = "C:\Users\Kintoki\Desktop\ASS_files"
$files = Get-ChildItem "$dir" | Where-Object {$_.Extension -eq ".ass"}
#Start batch loop
for ($i=0; $i -lt $files.Count; $i++) {
$subs = $files[$i].FullName
$ASS = Get-Content -LiteralPath $subs
Write-Host ('>> File: ' + $files[$i].BaseName)
#Remove non-used styles
foreach($elem in ($ASS -match "Style:\s(\D.*)").Replace("Style: ","")){
$tmp = $elem.split(",") | Select-Object -Index 0
if($tmp){
$str = ',' + $tmp + ','
[int]$occ = (Select-String -InputObject $ASS -Pattern $str -AllMatches).Matches.Count
#check if the first assigned actor/name is actually used, else remove
if($occ -le 0){
$ASS = $ASS -ne "Style: $elem"
Write-Host "* Removed stylename '$tmp' because it does not appear in ASS"
}
}
}
Set-Content -Encoding UTF8 -LiteralPath $subs -Value $ASS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment