30 March, 2026

Long time... How to fix subtitles files for hebrew (Plex)

 So I upgraded my Plex and suddenly my Hebrew SRT files started showing as gibberish.

I looked for an answer and found a script (I cannot find the site I took it from so, my apologies for the original author of the script).

It was not exactly what I was looking for so I adjusted it and below is my final version.

Just save it as PS1 and put it in the root directory of your movies/TV directory, it will test the files to find the files it need to fix, create a new converted file and delete the old one.

All of the actions are written to a log file.

Here it is:

function Get-EncodingType {

    param ([string]$filePath)


    $bytes = [System.IO.File]::ReadAllBytes($filePath)


    if ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF) {

        return "utf8-bom"

    }


    try {

        $utf8NoBom = New-Object System.Text.UTF8Encoding($false)

        $text = $utf8NoBom.GetString($bytes)

        $reEncoded = $utf8NoBom.GetBytes($text)


        if ($reEncoded.Length -eq $bytes.Length -and ($reEncoded -join ',') -eq ($bytes -join ',')) {

            return "utf8"

        } else {

            return "other"

        }

    } catch {

        return "other"

    }

}


$scriptPath = $MyInvocation.MyCommand.Path

$rootPath = Split-Path -Path $scriptPath -Parent

$logFile = Join-Path -Path $rootPath -ChildPath "conversion_log.txt"

$sourceEncoding = [System.Text.Encoding]::GetEncoding(1255)

$targetEncoding = New-Object System.Text.UTF8Encoding($false)  # UTF-8 without BOM


# Clear old log and create a new one

if (Test-Path $logFile) { Remove-Item $logFile }

"Conversion started on $(Get-Date)" | Out-File -FilePath $logFile


# Get all .srt files except already processed ones

$files = Get-ChildItem -Path $rootPath -Recurse -Filter *.srt | Where-Object {

    $_.Name -notmatch '\.he\.utf\.srt$'

}


if ($files.Count -eq 0) {

    "No new .srt files found in $rootPath or its subfolders." | Add-Content -Path $logFile

    Write-Host "No files found. Log saved to $logFile"

    return

}


$total = $files.Count

$counter = 0


foreach ($file in $files) {

    $counter++

    $inputFile = $file.FullName


    $encoding = Get-EncodingType $inputFile

    if ($encoding -eq "utf8" -or $encoding -eq "utf8-bom") {

        #Add-Content -Path $logFile -Value "[$counter/$total] Skipped (already UTF-8): $inputFile"

        continue

    }


    try {

        $content = [System.IO.File]::ReadAllText($inputFile, $sourceEncoding)


        if ($content -match '[\u0590-\u05FF]') {

            $newName = $file.BaseName

            $outputFile = Join-Path -Path $file.DirectoryName -ChildPath "$newName.he.utf.srt"


            [System.IO.File]::WriteAllText($outputFile, $content, $targetEncoding)

Rename-Item -Path "$Inputfile" -NewName "$file.old"

Rename-Item -Path "$outputFile" -NewName "$file"

            Add-Content -Path $logFile -Value "[$counter/$total] Converted: $outputFile"

        } else {

            #Add-Content -Path $logFile -Value "[$counter/$total] Skipped (no Hebrew): $inputFile"

        }

    } catch {

        Add-Content -Path $logFile -Value "[$counter/$total] ERROR in file: $inputFile - $_"

    }


    Write-Progress -Activity "Converting SRT files" -Status "$counter of $total" -PercentComplete (($counter / $total) * 100)

}


Write-Host "Done. Log saved to $logFile"


Enjoy

No comments:

Post a Comment