gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit9daebb4b9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre9daebb4b/lib/tags.hl

7.1 KB

  1. // lib/tags.hl — READ ONE FILE'S TAGS and make a song record of them. mp3: our own ID3 reader (lib/id3.hl) on the
  2. // first bytes of the file (read with `head -c`, since hl:fs has no ranged read yet, hybriel#233) and the ID3v1 block
  3. // (`tail -c`); the duration from the first audio frame (lib/mpeg.hl). Every other format: one ffprobe process.
  4. // The library is opened for reading only, by those programs. The normal form (genre names, "n/m" numbers, the year,
  5. // "Unknown artist") is `songFrom`. The cover picture of an mp3 comes back beside the record, not inside it.
  6. import { run } from 'hl:proc'
  7. import { readId3v2, readId3v1, tagLength } from './id3.hl'
  8. import { mp3Seconds } from './mpeg.hl'
  9. import { genreName, genreNames } from './id3-helpers.hl'
  10. import { leadingInt, baseName, folderOf, extOf, fold, padNumber, SEP, safeKey } from './util.hl'
  11. import { splitDisc, albumKeyOf, discFor, sortKeys } from './albums.hl'
  12. static headBytes = (path, n) => {
  13. r = run(['head', '-c', '' + n, path], { binary = true })
  14. return r == null || r.exit != 0 ? null : r.data
  15. }
  16. static tailBytes = (path, n) => {
  17. r = run(['tail', '-c', '' + n, path], { binary = true })
  18. return r == null || r.exit != 0 ? null : r.data
  19. }
  20. static first = (t, name) => {
  21. v = t[name]
  22. return v == null ? '' : v[0]
  23. }
  24. // a year out of "2002", "2002-05-01", "(c) 1999": the first 4 digits that make a year, else null
  25. static yearOf = (s) => {
  26. if (s == null) { return null }
  27. let i = 0
  28. while (i + 4 <= s.length) {
  29. n = leadingInt(s.slice(i, i + 4))
  30. if (n != null && s.slice(i, i + 4).length == 4 && n >= 1000 && n <= 2999 && '' + n == s.slice(i, i + 4)) { return n }
  31. i = i + 1
  32. }
  33. return null
  34. }
  35. // "11/21" → [11, 21]; "3" → [3, null]; '' → [null, null]
  36. static pairOf = (s) => {
  37. if (s == null || s == '') { return [null, null] }
  38. k = s.indexOf('/')
  39. if (k < 0) { return [leadingInt(s.trim()), null] }
  40. return [leadingInt(s.slice(0, k).trim()), leadingInt(s.slice(k + 1).trim())]
  41. }
  42. static starsOf = (rating) => {
  43. if (rating <= 0) { return 0 }
  44. if (rating < 32) { return 1 }
  45. if (rating < 96) { return 2 }
  46. if (rating < 160) { return 3 }
  47. if (rating < 224) { return 4 }
  48. return 5
  49. }
  50. // THE RAW READ of an mp3: what the tags say, before normalising. { title, artist, albumArtist, album, track, disc, year,
  51. // genre, duration, popm = [{owner, rating, count}], pcnt, cover = { mime, data } | null } — or null when unreadable.
  52. static readMp3 = (path, size) => {
  53. let b = headBytes(path, 131072)
  54. if (b == null) { return null }
  55. tl = tagLength(b)
  56. let need = tl + 4096
  57. if (need > 8388608) { need = 8388608 }
  58. if (need > b.length && size > b.length) {
  59. more = headBytes(path, need)
  60. if (more != null) { b = more }
  61. }
  62. t = readId3v2(b)
  63. text = t == null ? {} : t.text
  64. let title = first(text, 'TIT2')
  65. let artist = first(text, 'TPE1')
  66. let album = first(text, 'TALB')
  67. let year = first(text, 'TDRC')
  68. if (year == '') { year = first(text, 'TYER') }
  69. if (year == '') { year = first(text, 'TORY') }
  70. if (year == '') { year = first(text, 'TDOR') }
  71. let genre = first(text, 'TCON')
  72. let track = first(text, 'TRCK')
  73. if (t == null || (title == '' && artist == '' && album == '')) {
  74. tail = tailBytes(path, 128)
  75. v1 = tail == null ? null : readId3v1(tail)
  76. if (v1 != null) {
  77. if (title == '') { title = v1.title }
  78. if (artist == '') { artist = v1.artist }
  79. if (album == '') { album = v1.album }
  80. if (year == '') { year = v1.year }
  81. if (genre == '' && v1.genre < genreNames.length) { genre = genreNames[v1.genre] }
  82. if (track == '' && v1.track != null) { track = '' + v1.track }
  83. }
  84. }
  85. let duration = null
  86. tlen = leadingInt(first(text, 'TLEN'))
  87. audioStart = tl
  88. secs = mp3Seconds(b, audioStart, size - audioStart)
  89. if (secs != null) { duration = secs }
  90. else if (tlen != null) { duration = tlen / 1000 }
  91. return {
  92. title = title artist = artist albumArtist = first(text, 'TPE2') album = album track = track
  93. disc = first(text, 'TPOS') year = year genre = genre duration = duration
  94. popm = t == null ? [] : t.popm pcnt = t == null ? null : t.pcnt cover = t == null ? null : t.cover
  95. }
  96. }
  97. // THE RAW READ of anything else, with ffprobe (one process): the format's tags and duration
  98. static readProbe = (path) => {
  99. r = run(['ffprobe', '-v', 'error', '-print_format', 'json', '-show_entries', 'format=duration:format_tags:stream_tags', path], { timeout = 60 })
  100. if (r == null || r.exit != 0) { return null }
  101. let text = ''
  102. for (l of r.lines) { text = text + l + ' ' }
  103. if (!text.includes('"format"')) { return null }
  104. j = JSON.parse(text)
  105. if (j == null || j.format == null) { return null }
  106. tags = {}
  107. // the tags of the format first; an ogg or flac file keeps them on its audio stream
  108. if (j.streams != null && j.streams.length != null) {
  109. for (st of j.streams) {
  110. if (st.tags != null) {
  111. for (k in st.tags) { tags[k.toLowerCase()] = st.tags[k] }
  112. }
  113. }
  114. }
  115. if (j.format.tags != null) {
  116. for (k in j.format.tags) { tags[k.toLowerCase()] = j.format.tags[k] }
  117. }
  118. pick = (a, b2) => {
  119. let v = tags[a]
  120. if (v == null && b2 != null) { v = tags[b2] }
  121. return v == null ? '' : ('' + v).trim()
  122. }
  123. dur = j.format.duration == null ? null : toNumber(j.format.duration)
  124. return {
  125. title = pick('title', null) artist = pick('artist', null) albumArtist = pick('album_artist', 'albumartist') album = pick('album', null)
  126. track = pick('track', 'tracknumber') disc = pick('disc', 'discnumber') year = pick('date', 'year') genre = pick('genre', null)
  127. duration = dur popm = [] pcnt = null cover = null
  128. }
  129. }
  130. static videoExt = { 'mp4' = true 'm4v' = true 'mkv' = true 'webm' = true 'mov' = true 'avi' = true }
  131. // THE SONG RECORD from a raw read. `meta` = { path, size, changeTime }.
  132. static songFrom = (raw, meta, coverId) => {
  133. path = meta.path
  134. folder = folderOf(path)
  135. ext = extOf(path)
  136. let artist = raw.artist
  137. let albumArtist = raw.albumArtist
  138. if (artist == '') { artist = albumArtist }
  139. if (artist == '') { artist = 'Unknown artist' }
  140. let album = raw.album
  141. if (album == '') { album = 'Unknown album' }
  142. let title = raw.title
  143. if (title == '') { title = baseName(path) }
  144. tn = pairOf(raw.track)
  145. dn = pairOf(raw.disc)
  146. let play = raw.pcnt == null ? 0 : raw.pcnt
  147. let rating = 0
  148. for (p of raw.popm) {
  149. if (p.count > play) { play = p.count }
  150. if (p.rating > rating) { rating = p.rating }
  151. }
  152. groupArtist = albumArtist != '' ? albumArtist : artist
  153. parts = splitDisc(album)
  154. album = parts.name
  155. albumKey = albumKeyOf(groupArtist, album)
  156. disc = discFor(dn[0], parts.disc)
  157. track = tn[0] == null ? 0 : tn[0]
  158. keys = sortKeys(groupArtist, album, disc, track, title)
  159. return {
  160. path = path folder = folder title = title artist = artist albumArtist = groupArtist album = album
  161. track = track trackTotal = tn[1] disc = disc discTotal = dn[1] year = yearOf(raw.year)
  162. genre = genreName(raw.genre) duration = raw.duration format = ext kind = videoExt[ext] == true ? 'video' : 'audio'
  163. size = meta.size changeTime = meta.changeTime playCount = play popm = raw.popm rating = rating
  164. stars = starsOf(rating) coverId = coverId
  165. albumKey = albumKey artistKey = safeKey(fold(groupArtist))
  166. search = fold(artist + ' ' + title + ' ' + album + ' ' + raw.genre)
  167. sortArtist = keys.sortArtist
  168. sortAlbum = keys.sortAlbum
  169. sortTitle = safeKey(fold(title) + SEP + fold(groupArtist) + SEP + fold(album))
  170. }
  171. }

Branches

Latest commits

  • 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre
  • 59c1373ejukebox JB3 (work in progress): albums without folder and with disc markers folded, compilations, duplicate songs, own play counter, server-side shuffle queuemre
  • 2a4cfc55jukebox: library screens (songs, artists, albums, artist, album, search, favourites), compact player bar, /stream with Range, thumbnails, virtual list vendored, ui gate and real-library checkmre
  • 93a44212jukebox: report, numbers of the full scan, notes on the index key rulemre
  • bf5167dajukebox: index keys without commas (mpackdb loses them after a reopen), comma fixture and gate checksmre
  • 28a82fa2jukebox: README, STATUS, LOG, DECISIONSmre
  • d13987dfjukebox: library scanner, ID3 reader, mpackdb database and JSON API, with fixtures and a gatemre