Last active
October 15, 2026 22:08
-
-
Save lsmithmier/20b75e25cbed3971775c0112f7c16c17 to your computer and use it in GitHub Desktop.
PowerShell script that compares movie runtimes in a Plex library against IMDb runtimes (via OMDb)
This file contains hidden or 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
| #Requires -Version 7.0 | |
| <# | |
| .SYNOPSIS | |
| Compares movie runtimes in a Plex library against IMDb runtimes (via OMDb). | |
| .PARAMETER Limit | |
| Only process the first N movies. Use this while debugging so you don't burn | |
| through the OMDb free tier (1,000 requests/day). | |
| #> | |
| param( | |
| [int]$Limit = 0 | |
| ) | |
| # --- CONFIGURATION BLOCK --- | |
| $PlexUrl = "http://192.168.1.1:32400" | |
| $PlexToken = "" # Add in your Plex Token | |
| $LibraryId = "1" # Change to your specific Movie library section number | |
| $OmdbApiKey = "" # Add your OmdbApiKey | |
| $ThresholdMin = 3 # Flag movies that differ by more than this many minutes | |
| $CachePath = "./omdb_cache.json" | |
| # --------------------------- | |
| # 1. Fetch data from Plex API | |
| # NOTE: includeGuids=1 is REQUIRED. Without it, the library listing omits the | |
| # Guid array (imdb://, tmdb://, tvdb://), so every movie shows "Missing Match". | |
| $Headers = @{ | |
| "Accept" = "application/json" | |
| "X-Plex-Token" = $PlexToken | |
| } | |
| $PlexApiUrl = "$PlexUrl/library/sections/$LibraryId/all?includeGuids=1" | |
| Write-Host "Fetching movie library from Plex..." -ForegroundColor Cyan | |
| try { | |
| # Invoke-RestMethod can't parse this response: with includeGuids=1 each item has | |
| # both "guid" and "Guid" keys, which collide in a case-insensitive parser and the | |
| # call silently returns a raw string. -AsHashtable keeps them as separate keys. | |
| $Raw = Invoke-WebRequest -Uri $PlexApiUrl -Headers $Headers -Method Get | |
| $Json = if ($Raw.Content -is [byte[]]) { [System.Text.Encoding]::UTF8.GetString($Raw.Content) } else { $Raw.Content } | |
| $PlexResponse = $Json | ConvertFrom-Json -AsHashtable | |
| $Movies = @($PlexResponse['MediaContainer']['Metadata'] | Where-Object { $_ }) | |
| } catch { | |
| Write-Error "Failed to connect to Plex API: $($_.Exception.Message)" | |
| return | |
| } | |
| if ($Movies.Count -eq 0) { | |
| Write-Warning "Plex responded but returned no movies. Diagnostics:" | |
| Write-Host " URL requested : $PlexApiUrl" | |
| Write-Host " Response type : $($PlexResponse.GetType().FullName)" | |
| Write-Host " Token supplied: $([bool]$PlexToken)" | |
| Write-Host " Raw start : $($Json.Substring(0, [Math]::Min(300, $Json.Length)))" | |
| Write-Host " MediaContainer keys: $(($PlexResponse['MediaContainer'].Keys) -join ', ')" | |
| return | |
| } | |
| if ($Limit -gt 0) { $Movies = $Movies | Select-Object -First $Limit } | |
| # Load the OMDb cache so re-runs don't re-spend API calls | |
| $Cache = @{} | |
| if (Test-Path $CachePath) { | |
| (Get-Content $CachePath -Raw | ConvertFrom-Json -AsHashtable).GetEnumerator() | | |
| ForEach-Object { $Cache[$_.Key] = $_.Value } | |
| } | |
| $Report = [System.Collections.Generic.List[PSCustomObject]]::new() | |
| $TotalMovies = $Movies.Count | |
| $ApiErrors = 0 | |
| Write-Host "Found $TotalMovies movies. Comparing runtimes..." -ForegroundColor Cyan | |
| # 2. Loop through each movie and compare with IMDb/OMDb | |
| $Counter = 0 | |
| foreach ($Movie in $Movies) { | |
| $Counter++ | |
| Write-Progress -Activity "Auditing Runtimes" -Status "Processing: $($Movie['title'])" ` | |
| -PercentComplete (($Counter / $TotalMovies) * 100) | |
| # Plex duration is in milliseconds; it can be missing on unanalyzed items | |
| $PlexRuntimeMin = if ($Movie['duration']) { [Math]::Round(([double]$Movie['duration'] / 60000), 2) } else { $null } | |
| # Extract IMDb ID from the Guid array | |
| $ImdbId = $null | |
| foreach ($G in @($Movie['Guid'])) { | |
| if ($G -and $G['id'] -like "imdb://*") { | |
| $ImdbId = $G['id'] -replace "^imdb://", "" | |
| break | |
| } | |
| } | |
| $ImdbRuntimeMin = $null | |
| $Difference = $null | |
| $Status = "OK" | |
| if (-not $ImdbId) { | |
| $Status = "No IMDb ID in Plex" | |
| } | |
| elseif (-not $PlexRuntimeMin) { | |
| $Status = "No Plex duration" | |
| } | |
| else { | |
| if ($Cache.ContainsKey($ImdbId)) { | |
| $ImdbRuntimeMin = $Cache[$ImdbId] | |
| } | |
| else { | |
| try { | |
| # Query-string parameters 'i' (IMDb ID) and 'apikey' are required | |
| $OmdbUrl = "https://www.omdbapi.com/?i=$ImdbId&apikey=$OmdbApiKey" | |
| $OmdbData = Invoke-RestMethod -Uri $OmdbUrl -Method Get | |
| if ($OmdbData.Response -eq "True" -and $OmdbData.Runtime -match "(\d+)\s*min") { | |
| $ImdbRuntimeMin = [int]$Matches[1] | |
| $Cache[$ImdbId] = $ImdbRuntimeMin | |
| } | |
| else { | |
| # OMDb answered, but had no usable runtime (or an error such as a bad key) | |
| $Status = "OMDb: " + $(if ($OmdbData.Error) { $OmdbData.Error } else { "no runtime" }) | |
| if ($OmdbData.Error -match "limit|key") { | |
| Write-Warning "OMDb says: $($OmdbData.Error) - stopping." | |
| break | |
| } | |
| } | |
| } | |
| catch { | |
| $ApiErrors++ | |
| $Status = "OMDb request failed" | |
| Write-Warning "$($Movie['title']) [$ImdbId]: $($_.Exception.Message)" | |
| } | |
| Start-Sleep -Milliseconds 150 # be polite to OMDb | |
| } | |
| if ($null -ne $ImdbRuntimeMin) { | |
| $Difference = [Math]::Round(($PlexRuntimeMin - $ImdbRuntimeMin), 2) | |
| } | |
| } | |
| $Report.Add([PSCustomObject]@{ | |
| Title = $Movie['title'] | |
| Year = $Movie['year'] | |
| IMDb_ID = $ImdbId | |
| Plex_Runtime_Min = $PlexRuntimeMin | |
| IMDb_Runtime_Min = $ImdbRuntimeMin | |
| Difference_Min = $Difference | |
| Flag = if ($null -ne $Difference -and [Math]::Abs($Difference) -gt $ThresholdMin) { "CHECK" } else { "" } | |
| Status = $Status | |
| }) | |
| } | |
| # Persist the cache for next time | |
| $Cache | ConvertTo-Json | Set-Content $CachePath | |
| # 3. Export and display | |
| $OutputPath = "./Plex_vs_IMDb_Runtimes.csv" | |
| $Report | Export-Csv -Path $OutputPath -NoTypeInformation -Encoding UTF8 | |
| $Matched = @($Report | Where-Object { $null -ne $_.Difference_Min }) | |
| $Flagged = @($Report | Where-Object { $_.Flag -eq "CHECK" }) | |
| Write-Host "`nAudit complete! Report exported to: $OutputPath" -ForegroundColor Green | |
| Write-Host "$($Matched.Count) of $TotalMovies compared; $($Flagged.Count) differ by more than $ThresholdMin min; $ApiErrors API errors." -ForegroundColor Green | |
| # Show the biggest offenders first | |
| $View = $Report | Sort-Object { if ($null -eq $_.Difference_Min) { 0 } else { [Math]::Abs($_.Difference_Min) } } -Descending | |
| if ($IsWindows -and (Get-Command Out-GridView -ErrorAction SilentlyContinue)) { | |
| $View | Out-GridView -Title "Plex vs IMDb Runtime Comparison" | |
| } else { | |
| $View | Select-Object -First 25 | Format-Table -AutoSize | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment