gitoria

jukebox

All repositories: gitoria

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

Branches

Latest commits

  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre
  • 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
  • 1fecd6a7Merge branch 'records-day39' into mainJukebox
  • d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre