feat(media): subtitles that travel with the film — CLI, browser <track>, ExoPlayer
`shade media put Film.mkv` seals Film.srt / Film.no.srt / Film.en.vtt found beside it and carries their grants in the film's sealed meta (extra["shade.subtitles"]), so they follow the film alone or in an album and the relay sees unrelated objects. /watch/ adds them as <track>s (SRT to VTT, labels in the viewer's language, the viewer's language on, one at a time); `media get` writes them back; Android opens them with ShadeMedia.openFilm and gives them to ExoPlayer as side-loaded tracks (ShadeMediaDataSource now resolves readers by URI). Verified: 76 TS tests; Chromium nb-NO shows the Norwegian track only, en-US the English one; film + subtitles round-trip bit-identical; Robolectric ExoPlayer sees both text tracks with the Norwegian one default; a shared vector checks the meta field in Kotlin (82/0). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package no.zyon.shade.media
|
||||
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONException
|
||||
import org.json.JSONObject
|
||||
|
||||
/**
|
||||
* Subtitles — mirror of `@shade/media/src/subtitles.ts`. A film carries the
|
||||
* grants of its sealed subtitle files in its own sealed meta, under
|
||||
* `extra["shade.subtitles"]`: `[{"lang":"no","label":"…","grant":{…}}, …]`.
|
||||
*/
|
||||
|
||||
const val SUBTITLES_KEY = "shade.subtitles"
|
||||
private const val MAX_TRACKS = 64
|
||||
private val LANG_RE = Regex("^[A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8})*$")
|
||||
|
||||
class SubtitleTrack(val grant: MediaGrant, val lang: String? = null, val label: String? = null)
|
||||
|
||||
object Subtitles {
|
||||
/** The subtitle tracks a film's meta names, strictly; none if it names none. */
|
||||
fun of(meta: MediaMeta?): List<SubtitleTrack> {
|
||||
val raw = meta?.extra?.get(SUBTITLES_KEY) ?: return emptyList()
|
||||
val list = try { JSONArray(raw) } catch (e: JSONException) { throw MediaFormatException("subtitles are not a JSON array") }
|
||||
if (list.length() > MAX_TRACKS) throw MediaFormatException("more than $MAX_TRACKS subtitle tracks")
|
||||
return (0 until list.length()).map { i ->
|
||||
val o = list.opt(i) as? JSONObject ?: throw MediaFormatException("subtitle $i is not an object")
|
||||
val g = Grants.parse((o.opt("grant") as? JSONObject ?: throw MediaFormatException("subtitle $i has no grant")).toString())
|
||||
if (g !is MediaGrant) throw MediaFormatException("subtitle $i is not a sealed file")
|
||||
val lang = if (!o.has("lang")) null else (o.opt("lang") as? String)?.takeIf { LANG_RE.matches(it) }
|
||||
?: throw MediaFormatException("subtitle $i: bad lang")
|
||||
val label = if (!o.has("label")) null else (o.opt("label") as? String)?.takeIf { it.length <= 200 }
|
||||
?: throw MediaFormatException("subtitle $i: bad label")
|
||||
SubtitleTrack(g, lang, label)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import no.zyon.shade.media.MediaKeys
|
||||
import no.zyon.shade.media.MediaMeta
|
||||
import no.zyon.shade.media.MediaReader
|
||||
import no.zyon.shade.media.MediaSealer
|
||||
import no.zyon.shade.media.Subtitles
|
||||
import no.zyon.shade.media.chainStep
|
||||
import no.zyon.shade.media.decodeFrame
|
||||
import no.zyon.shade.media.deriveMediaSubkeys
|
||||
@@ -208,4 +209,23 @@ class MediaVectorTest {
|
||||
assertThrows(MediaFormatException::class.java) { Albums.parse("""{"kind":"shade.album/v2","title":"","items":[]}""".toByteArray()) }
|
||||
assertThrows(MediaFormatException::class.java) { Albums.parse(byteArrayOf(0xff.toByte(), 0xfe.toByte())) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun subtitlesInAFilmsMetaParse() {
|
||||
val v = byPrefix("subtitles:").single()
|
||||
val tracks = Subtitles.of(MediaMeta.fromJson(v.getString("meta")))
|
||||
val langs = v.getJSONArray("langs")
|
||||
val labels = v.getJSONArray("labels")
|
||||
val ids = v.getJSONArray("mediaIds")
|
||||
assertEquals(langs.length(), tracks.size)
|
||||
tracks.forEachIndexed { i, t ->
|
||||
assertEquals(langs.getString(i), t.lang)
|
||||
assertEquals(if (labels.isNull(i)) null else labels.getString(i), t.label)
|
||||
assertEquals(ids.getString(i), toHex(t.grant.mediaId))
|
||||
}
|
||||
assertEquals(emptyList<Any>(), Subtitles.of(MediaMeta(name = "no subtitles")))
|
||||
assertThrows(MediaFormatException::class.java) {
|
||||
Subtitles.of(MediaMeta(extra = mapOf("shade.subtitles" to """[{"lang":"no; x","grant":{}}]""")))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user