Skip to content

Native Android client for Spotify, built with Jetpack Compose

Music videos: a video must be the song's artist and title (normalized, feat. and video tags left out), live, cover, lyric, reaction, fan and remix versions only when the song is one, its length near the song's; nothing is shown rather than another song, and answers cached under older rules are looked up again

kota-zo6df75dba0bf2
diff --git a/base/app/src/main/java/com/music/spotui/video/MusicVideoController.kt b/base/app/src/main/java/com/music/spotui/video/MusicVideoController.kt
index 47f2b6e0..656edaa5 100644
--- a/base/app/src/main/java/com/music/spotui/video/MusicVideoController.kt
+++ b/base/app/src/main/java/com/music/spotui/video/MusicVideoController.kt
@@ -115,8 +115,12 @@ object MusicVideoController {
// song shows its answer at once, with no disk and no job. No audio, no features, no decoder.
private const val RECENT_MAX = 20
private const val NONE_TTL_MS = 7 * 86_400_000L
- /** Bumped when the search finds clips it missed before (Spotify-confirmed label uploads, "$" names). */
- private const val NONE_RULES = 2
+ /**
+ * Bumped when the search finds clips it missed before (Spotify-confirmed label uploads, "$" names) or
+ * refuses ones it wrongly took (artist, title, other versions, length: MusicVideoMatch). Answers
+ * under older rules, "none" and clips alike, are looked up again.
+ */
+ private const val MATCH_RULES = 3
private class Recent(val state: State, val at: Long)
private val recent = object : LinkedHashMap<String, Recent>(32, 0.75f, true) {
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<String, Recent>) = size > RECENT_MAX
@@ -151,9 +155,11 @@ object MusicVideoController {
if (System.currentTimeMillis() - f.lastModified() > 86_400_000L) f.setLastModified(System.currentTimeMillis())
if (o.optBoolean("none")) {
// "No clip" found under older search rules is looked up again.
- return@runCatching if (o.optInt("rules") >= NONE_RULES && System.currentTimeMillis() - o.optLong("at") < NONE_TTL_MS)
+ return@runCatching if (o.optInt("rules") >= MATCH_RULES && System.currentTimeMillis() - o.optLong("at") < NONE_TTL_MS)
State.Unavailable.also { remember(trackId, it, o.optLong("at")) } else null
}
+ // A clip picked under older rules may be a wrong one: looked up again.
+ if (o.optInt("rules") < MATCH_RULES) return@runCatching null
val score = o.getDouble("score")
require(score.isFinite() && score >= MIN_SCORE)
val jsonSegments = o.getJSONArray("segments")
@@ -177,12 +183,12 @@ object MusicVideoController {
val o = JSONObject()
when (s) {
is State.Ready -> {
- o.put("video", s.videoUrl).put("score", s.map.score).put("complete", s.mapComplete)
+ o.put("video", s.videoUrl).put("score", s.map.score).put("complete", s.mapComplete).put("rules", MATCH_RULES)
o.put("segments", JSONArray().apply {
s.map.segments.forEach { put(JSONObject().put("from", it.songStartMs).put("to", it.songEndMs).put("offset", it.offsetMs)) }
})
}
- else -> o.put("none", true).put("at", System.currentTimeMillis()).put("rules", NONE_RULES)
+ else -> o.put("none", true).put("at", System.currentTimeMillis()).put("rules", MATCH_RULES)
}
runCatching {
val file = AtomicFile(cacheFile(trackId))
diff --git a/base/app/src/main/java/com/music/spotui/video/MusicVideoMatch.kt b/base/app/src/main/java/com/music/spotui/video/MusicVideoMatch.kt
new file mode 100644
index 00000000..26455634
--- /dev/null
+++ b/base/app/src/main/java/com/music/spotui/video/MusicVideoMatch.kt
@@ -0,0 +1,121 @@
+package com.music.spotui.video
+
+import java.text.Normalizer
+import java.util.Locale
+
+/**
+ * Which search results can be the music video of a track, and in which order. Pure (no network,
+ * no Android): a candidate is a YouTube result's name, uploader and duration in seconds.
+ *
+ * A candidate passes only when all of these hold:
+ * - the artist is in its channel or its title (whole words, accents and case ignored);
+ * - the track's title is in its title (normalized: "(Official Video)", "[MV]", "feat. …" dropped);
+ * - it is not a live, cover, lyric, reaction, fan, remix, audio-only… version, unless the track's
+ * own title says so;
+ * - its length is near the track's: an MV may be longer than the song by an intro or an outro.
+ * A duration unknown to either side is not held against the candidate, except a candidate
+ * without a duration when the track's is known.
+ * An empty result is an answer: no video is better than a wrong one.
+ */
+internal object MusicVideoMatch {
+ data class Entry(val name: String, val uploader: String, val durationSec: Long)
+
+ /** A candidate may be this much shorter than the track (a cut clip). */
+ const val SHORTER_TOLERANCE_MS = 10_000L
+ /** A candidate may be this much longer than the track (an intro or an outro in the clip). */
+ const val LONGER_TOLERANCE_MS = 90_000L
+
+ /** Versions that are not the track's clip, each refused unless the track's own title names it. */
+ private val unwanted: List<Regex> = listOf(
+ Regex("""\blive\b"""),
+ Regex("""\bcover(?:ed|s)?\b|\breprise\b"""),
+ Regex("""\blyrics?\b|\bparoles\b|\bletra\b|\btesto\b"""),
+ Regex("""\breact(?:ion|ions|s)?\b|\breaction video\b"""),
+ Regex("""\bfan ?(?:made|edit|cam|mv|video)\b|\bfancam\b|\bfanmade\b"""),
+ Regex("""\bremix\b|\brmx\b|\bbootleg\b|\bmashup\b"""),
+ Regex("""\bkaraoke\b|\binstrumental\b|\btutorial\b|\bpiano\b|\bguitar\b"""),
+ Regex("""\bslowed\b|\bsped ?up\b|\bnightcore\b|\b8d\b|\breverb\b"""),
+ Regex("""\b(?:official )?audio\b|\baudio officiel\b"""),
+ )
+
+ /** Words of a track title that are not part of its name. */
+ private val titleNoise = Regex("""\(.*?\)|\[.*?]|\{.*?}|\b(?:feat|ft|featuring)\b\.?\s.*""")
+
+ /** Accents, case, "$" read as "s" ("A$AP" is "ASAP") and the elided apostrophe ("J'SAVAIS" is "jsavais"). */
+ private fun fold(s: String): String = Normalizer.normalize(s.replace('$', 's').replace('’', '\''), Normalizer.Form.NFD)
+ .replace(Regex("""\p{M}+"""), "")
+ .lowercase(Locale.ROOT)
+ .replace(Regex("""(?<=\p{L})'(?=\p{L})"""), "")
+
+ private fun tokens(s: String): List<String> = fold(s).split(Regex("""[^\p{L}\p{N}]+""")).filter { it.isNotEmpty() }
+
+ /** The primary artist of "A, B": a track's artist list is matched on its first name. */
+ private fun primaryArtist(artist: String): String = artist.substringBefore(",").trim()
+
+ /** A channel's own name: "Artist - Topic" and "ArtistVEVO" are the artist. */
+ private fun channelTokens(uploader: String): List<String> =
+ tokens(fold(uploader).replace(Regex("""\s*-\s*topic$|(?<=.)vevo$"""), ""))
+
+ private fun containsRun(haystack: List<String>, needle: List<String>): Boolean {
+ if (needle.isEmpty() || needle.size > haystack.size) return false
+ return (0..haystack.size - needle.size).any { i -> needle.indices.all { j -> haystack[i + j] == needle[j] } }
+ }
+
+ /** The artist is the uploader's channel, or appears among the words of the name. */
+ internal fun artistMatches(e: Entry, artist: String): Boolean {
+ val a = tokens(primaryArtist(artist))
+ if (a.isEmpty()) return false
+ return containsRun(channelTokens(e.uploader), a) || containsRun(tokens(e.name), a)
+ }
+
+ /** The track's words (without the parenthesised and featured parts) must be in the name, most of them. */
+ internal fun titleMatches(name: String, title: String): Boolean {
+ val words = tokens(fold(title).replace(titleNoise, " "))
+ if (words.isEmpty()) return fold(name).contains(fold(title))
+ val nameWords = tokens(name).toSet()
+ return words.count { it in nameWords } >= (words.size * 0.6).coerceAtLeast(1.0)
+ }
+
+ /** True when the name shows a version the track is not, and the track's own title does not name it. */
+ internal fun isOtherVersion(name: String, title: String): Boolean {
+ val n = fold(name)
+ val t = fold(title)
+ return unwanted.any { re -> re.containsMatchIn(n) && !re.containsMatchIn(t) }
+ }
+
+ /** Length within the tolerance of the track's; unknown on either side is not held against it. */
+ internal fun durationMatches(durationSec: Long, durationMs: Long): Boolean {
+ if (durationMs <= 0) return true
+ if (durationSec <= 0) return false
+ val sec = durationSec * 1000
+ return sec >= durationMs - SHORTER_TOLERANCE_MS && sec <= durationMs + LONGER_TOLERANCE_MS
+ }
+
+ private val officialWords = Regex("""\bofficial\b|\bofficiel(?:le)?\b|\bvevo\b|\bmusic video\b|\bmv\b|\bclip\b""")
+
+ /**
+ * Preference among the accepted candidates: the artist's own channel first, then a name that
+ * says it is the official clip, then a length that is the track's own.
+ */
+ private fun preference(e: Entry, title: String, artist: String, durationMs: Long): Int {
+ val a = tokens(primaryArtist(artist))
+ var score = 0
+ if (channelTokens(e.uploader) == a) score += 4
+ if (officialWords.containsMatchIn(fold(e.name))) score += 2
+ if (durationMs > 0 && kotlin.math.abs(e.durationSec * 1000 - durationMs) <= 5_000) score += 1
+ return score
+ }
+
+ /**
+ * Indices of the entries that can be the track's clip, best first (ties keep the search order).
+ * Empty when none passes: the caller shows no video.
+ */
+ fun rank(title: String, artist: String, durationMs: Long, entries: List<Entry>): List<Int> =
+ entries.indices
+ .filter { i ->
+ val e = entries[i]
+ titleMatches(e.name, title) && artistMatches(e, artist) &&
+ !isOtherVersion(e.name, title) && durationMatches(e.durationSec, durationMs)
+ }
+ .sortedByDescending { i -> preference(entries[i], title, artist, durationMs) }
+}
diff --git a/base/app/src/main/java/com/music/spotui/video/YouTubeSource.kt b/base/app/src/main/java/com/music/spotui/video/YouTubeSource.kt
index 256dadec..c99ddd93 100644
--- a/base/app/src/main/java/com/music/spotui/video/YouTubeSource.kt
+++ b/base/app/src/main/java/com/music/spotui/video/YouTubeSource.kt
@@ -151,18 +151,12 @@ object YouTubeSource {
*/
suspend fun videoCandidates(title: String, artist: String, durationMs: Long): List<Hit> = coroutineScope {
val main = artist.substringBefore(",").trim()
- val a = norm(main)
val music = async(Dispatchers.IO) { runInterruptible { search("$title $main", YoutubeSearchQueryHandlerFactory.MUSIC_VIDEOS) } }
val plain = async(Dispatchers.IO) { runInterruptible { search("$title $main clip officiel official video", YoutubeSearchQueryHandlerFactory.VIDEOS) } }
- (music.await().take(6) + plain.await().take(8))
- .distinctBy { it.url }
- .filter { h ->
- val sec = h.durationSec * 1000
- titleMatches(h.name, title) && !excluded.containsMatchIn(h.name) &&
- (durationMs <= 0 || (sec >= durationMs - 10_000 && sec <= durationMs + 240_000))
- }
- // Same artist (in the channel or the title) first, then the search order.
- .sortedBy { h -> if (norm(h.uploader).contains(a) || norm(h.name).contains(a)) 0 else 1 }
+ val pool = (music.await().take(6) + plain.await().take(8)).distinctBy { it.url }
+ // Artist, title, no other version, length near the track's; the official clips first (MusicVideoMatch).
+ MusicVideoMatch.rank(title, artist, durationMs, pool.map { MusicVideoMatch.Entry(it.name, it.uploader, it.durationSec) })
+ .map { pool[it] }
}
/** A YouTube upload of the plain song (official audio) when YouTube Music has none. */
diff --git a/base/app/src/test/java/com/music/spotui/video/MusicVideoMatchTest.kt b/base/app/src/test/java/com/music/spotui/video/MusicVideoMatchTest.kt
new file mode 100644
index 00000000..7598545f
--- /dev/null
+++ b/base/app/src/test/java/com/music/spotui/video/MusicVideoMatchTest.kt
@@ -0,0 +1,94 @@
+package com.music.spotui.video
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class MusicVideoMatchTest {
+ private val demon = "デーモンダンストーキョー"
+ private val demonMs = 220_000L
+
+ private fun e(name: String, uploader: String, sec: Long) = MusicVideoMatch.Entry(name, uploader, sec)
+
+ /** The accepted entries, best first. */
+ private fun accepted(title: String, artist: String, durationMs: Long, vararg entries: MusicVideoMatch.Entry): List<MusicVideoMatch.Entry> =
+ MusicVideoMatch.rank(title, artist, durationMs, entries.toList()).map { entries[it] }
+
+ @Test fun otherSongOfTheSameArtistIsRefused() {
+ // Discord report: "Ambivalent - Eve MV" (3:42) played for デーモンダンストーキョー (3:40).
+ assertTrue(accepted(demon, "Eve", demonMs, e("Ambivalent - Eve MV", "Eve", 222)).isEmpty())
+ }
+
+ @Test fun theTracksOwnMvIsKeptAndTheOtherDropped() {
+ val own = e("デーモンダンストーキョー - Eve MV", "Eve", 241)
+ assertEquals(listOf(own), accepted(demon, "Eve", demonMs, e("Ambivalent - Eve MV", "Eve", 222), own))
+ }
+
+ @Test fun artistMustMatchTheChannelOrTheName() {
+ assertTrue(accepted("Song", "Eve", 200_000, e("Song (Official Video)", "Someone Else", 200)).isEmpty())
+ }
+
+ @Test fun artistIsWholeWordsNotASubstring() {
+ assertTrue(accepted("Demon", "Eve", 200_000, e("Demon - Evening Show", "Evening Music", 200)).isEmpty())
+ }
+
+ @Test fun vevoAndTopicChannelsAreTheArtist() {
+ assertEquals(1, accepted("Song", "Eve", 200_000, e("Song", "EveVEVO", 200)).size)
+ assertEquals(1, accepted("Song", "Eve", 200_000, e("Song", "Eve - Topic", 200)).size)
+ }
+
+ @Test fun accentsCaseAndDollarSignAreNormalized() {
+ assertEquals(1, accepted("Halo", "Beyoncé", 261_000, e("HALO (Official Music Video)", "BEYONCE", 261)).size)
+ assertEquals(1, accepted("Praise The Lord", "A\$AP Rocky", 200_000, e("Praise The Lord - A\$AP Rocky (Official Video)", "ASAP Rocky", 200)).size)
+ }
+
+ @Test fun featuredPartAndDecorationsAreNotInTheTitle() {
+ assertEquals(1, accepted("Song (feat. Someone)", "Artist", 200_000, e("Song ft. Someone [Official MV]", "Artist", 200)).size)
+ assertEquals(1, accepted("Song feat. Someone", "Artist", 200_000, e("Song (Official Video)", "Artist", 200)).size)
+ }
+
+ @Test fun titleMustBeInTheName() {
+ assertTrue(accepted("Song", "Eve", 200_000, e("Songbird - Eve", "Eve", 200)).isEmpty())
+ }
+
+ @Test fun liveCoverLyricReactionAndFanVersionsAreRefused() {
+ assertTrue(accepted(demon, "Eve", demonMs, e("デーモンダンストーキョー (Live) - Eve", "Eve", 220)).isEmpty())
+ assertTrue(accepted("Demon", "Eve", 200_000, e("Demon cover - Eve", "Someone", 200)).isEmpty())
+ assertTrue(accepted(demon, "Eve", demonMs, e("デーモンダンストーキョー (Lyrics) - Eve", "Eve", 220)).isEmpty())
+ assertTrue(accepted("Demon", "Eve", 200_000, e("Demon - Eve reaction", "Reactor", 200)).isEmpty())
+ assertTrue(accepted("Demon", "Eve", 200_000, e("Demon - Eve fan made MV", "Fan", 200)).isEmpty())
+ }
+
+ @Test fun aVersionTheTitleNamesIsAllowed() {
+ assertEquals(1, accepted("Live Forever", "Oasis", 270_000, e("Live Forever (Official Video)", "Oasis", 270)).size)
+ assertEquals(1, accepted("Song (Remix)", "Eve", 200_000, e("Song (Remix) - Eve", "Eve", 200)).size)
+ }
+
+ @Test fun remixAndAudioOnlyAreRefusedUnlessTheTitleSaysSo() {
+ assertTrue(accepted("Song", "Eve", 200_000, e("Song (Remix) - Eve", "Eve", 200)).isEmpty())
+ assertTrue(accepted("Song", "Eve", 200_000, e("Song - Eve (Official Audio)", "Eve", 200)).isEmpty())
+ }
+
+ @Test fun lengthToleranceIsMinus10SecondsToPlus90() {
+ val track = 200_000L
+ assertEquals(1, accepted("Song", "Eve", track, e("Song - Eve", "Eve", 190)).size)
+ assertTrue(accepted("Song", "Eve", track, e("Song - Eve", "Eve", 189)).isEmpty())
+ assertEquals(1, accepted("Song", "Eve", track, e("Song - Eve", "Eve", 290)).size)
+ assertTrue(accepted("Song", "Eve", track, e("Song - Eve", "Eve", 291)).isEmpty())
+ }
+
+ @Test fun unknownDurationIsHeldAgainstACandidateOnlyWhenTheTrackHasOne() {
+ assertTrue(accepted("Song", "Eve", 200_000, e("Song - Eve", "Eve", 0)).isEmpty())
+ assertEquals(1, accepted("Song", "Eve", 0, e("Song - Eve", "Eve", 0)).size)
+ }
+
+ @Test fun artistChannelAndOfficialNameComeFirst() {
+ val byLabel = e("Song - Eve (Official Video)", "Warner Music", 200)
+ val byArtist = e("Song - Eve", "Eve", 200)
+ assertEquals(listOf(byArtist, byLabel), accepted("Song", "Eve", 200_000, byLabel, byArtist))
+ }
+
+ @Test fun primaryArtistOfAListIsMatched() {
+ assertEquals(1, accepted("Song", "Eve, Someone", 200_000, e("Song - Eve", "Eve", 200)).size)
+ }
+}