Choose the lighter copy of a film: 1080p before a 4K remux
release-gate / verify (push) Successful in 14m18s
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 4m23s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 1m47s

13 Nov: American Werewolf played its 54 Mbps 4K HDR copy with DTS-HD MA and drifted
out of sync, while a 10 Mbps 1080p AC3 copy was in the library (Night of the
Comet the same: 89 Mbps 4K DV vs 7 Mbps 1080p). resolveFilm now orders copies by
copyWeight (4K, high bitrate, lossless-only sound count against), then id.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 11:34:18 +00:00
co-authored by Claude Opus 5.5
parent e61f5edfdf
commit 0bee17ec42
2 changed files with 36 additions and 2 deletions
@@ -59,9 +59,11 @@ internal fun resolveFilm(identity: FilmIdentity, candidates: List<FilmCandidate>
else -> "film=\"${identity.label}\" missing: the library has that name only from $years"
})
if (filmCount(film) > 1) return FilmLookup(null, "film=\"${identity.label}\" ambiguous: two different films share that name and year, so the list needs an IMDb ID")
// Several copies of one film (another edition, a better file) are all fine. The first this box can play is used.
// Several copies of one film (another edition, a better file) are all fine. The lightest this box can play is used
// (1.0 RC1, 13 November 2026): a 1080p copy before a 4K remux, so a living-room box streaming from a server far away
// doesn't fall behind or drift out of sync (American Werewolf's 54 Mbps 4K copy did; its 10 Mbps 1080p copy was there).
val passed = mutableListOf<String>()
for (copy in film.sortedBy { it.id }) {
for (copy in film.sortedWith(compareBy<FilmCandidate> { copyWeight(it) }.thenBy { it.id })) {
val source = copy.sources.firstOrNull { supportedDirectContainer(it.optString("Container")) != null && EmbyClient.validId(it.optString("Id")) && sourceVideoCodec(it)?.let(canDecode) != false }
if (source == null) { passed += "${copy.id} ${if (copy.sources.isEmpty()) "no media" else "can't play here"}"; continue }
val ticks = source.optLong("RunTimeTicks", 0).takeIf { it > 0 } ?: copy.runTimeTicks
@@ -73,6 +75,21 @@ internal fun resolveFilm(identity: FilmIdentity, candidates: List<FilmCandidate>
return FilmLookup(null, "film=\"${identity.label}\" unplayable: ${passed.joinToString()}")
}
/**
* How heavy a copy is to stream and play, lower first: 4K picture, a very high bitrate, and only lossless surround sound
* (TrueHD, DTS-HD MA) each count against it. Ties keep the library's order.
*/
internal fun copyWeight(copy: FilmCandidate): Int {
val source = copy.sources.firstOrNull() ?: return 9
val streams = source.optJSONArray("MediaStreams")?.let { a -> (0 until minOf(a.length(), 32)).mapNotNull { a.optJSONObject(it) } }.orEmpty()
val width = streams.firstOrNull { it.optString("Type") == "Video" }?.optInt("Width", 0) ?: 0
val bitrate = source.optLong("Bitrate", 0)
val audio = streams.filter { it.optString("Type") == "Audio" }
fun lossless(a: JSONObject) = a.optString("Codec").lowercase().let { it == "truehd" || it == "mlp" } || a.optString("Profile").contains("MA", ignoreCase = false)
return (if (width > 1920) 2 else 0) + (if (bitrate > 30_000_000) 2 else if (bitrate > 15_000_000) 1 else 0) +
(if (audio.isNotEmpty() && audio.all(::lossless)) 1 else 0)
}
/** A library row as a candidate. Rows that aren't films, or carry an invalid ID, are left out. */
internal fun filmCandidate(row: JSONObject): FilmCandidate? {
val id = row.optString("Id")
@@ -63,3 +63,20 @@ class DolbyVisionCopyTest {
assertEquals("h264", sourceVideoCodec(source("None", "h264")))
}
}
/** 13 November 2026: American Werewolf's 54 Mbps 4K copy drifted out of sync; its 10 Mbps 1080p copy was in the library. */
class CopyChoiceTest {
private fun copy(id: String, width: Int, bitrate: Long, vararg audio: Pair<String, String>) = FilmCandidate(id, "An American Werewolf in London", 1981, "tt0082010",
listOf(JSONObject().put("Id", "src$id").put("Container", "mkv").put("Bitrate", bitrate).put("RunTimeTicks", 97 * 60_000L * 10_000)
.put("MediaStreams", JSONArray().put(JSONObject().put("Type", "Video").put("Codec", if (width > 1920) "hevc" else "h264").put("Width", width))
.also { a -> audio.forEach { (c, p) -> a.put(JSONObject().put("Type", "Audio").put("Codec", c).put("Profile", p)) } })))
@Test fun theLighterCopyIsChosen() {
val heavy = copy("aaa1", 3840, 54_488_349, "dts" to "DTS-HD MA", "dts" to "DTS")
val light = copy("zzz9", 1920, 9_647_280, "ac3" to "")
val found = resolveFilm(FilmIdentity("An American Werewolf in London", 1981), listOf(heavy, light))
assertEquals("zzz9", found.film!!.id)
assertTrue(copyWeight(heavy) > copyWeight(light))
// With only the 4K copy, it's still used.
assertEquals("aaa1", resolveFilm(FilmIdentity("An American Werewolf in London", 1981), listOf(heavy)).film!!.id)
}
}