jukebox
All repositories: gitoria
7.1 KB
// 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// first bytes of the file (read with `head -c`, since hl:fs has no ranged read yet, hybriel#233) and the ID3v1 block// (`tail -c`); the duration from the first audio frame (lib/mpeg.hl). Every other format: one ffprobe process.// The library is opened for reading only, by those programs. The normal form (genre names, "n/m" numbers, the year,// "Unknown artist") is `songFrom`. The cover picture of an mp3 comes back beside the record, not inside it.import { run } from 'hl:proc'import { readId3v2, readId3v1, tagLength } from './id3.hl'import { mp3Seconds } from './mpeg.hl'import { genreName, genreNames } from './id3-helpers.hl'import { leadingInt, baseName, folderOf, extOf, fold, padNumber, SEP, safeKey } from './util.hl'import { splitDisc, albumKeyOf, discFor, sortKeys } from './albums.hl'static headBytes = (path, n) => {r = run(['head', '-c', '' + n, path], { binary = true })return r == null || r.exit != 0 ? null : r.data}static tailBytes = (path, n) => {r = run(['tail', '-c', '' + n, path], { binary = true })return r == null || r.exit != 0 ? null : r.data}static first = (t, name) => {v = t[name]return v == null ? '' : v[0]}// a year out of "2002", "2002-05-01", "(c) 1999": the first 4 digits that make a year, else nullstatic yearOf = (s) => {if (s == null) { return null }let i = 0while (i + 4 <= s.length) {n = leadingInt(s.slice(i, i + 4))if (n != null && s.slice(i, i + 4).length == 4 && n >= 1000 && n <= 2999 && '' + n == s.slice(i, i + 4)) { return n }i = i + 1}return null}// "11/21" → [11, 21]; "3" → [3, null]; '' → [null, null]static pairOf = (s) => {if (s == null || s == '') { return [null, null] }k = s.indexOf('/')if (k < 0) { return [leadingInt(s.trim()), null] }return [leadingInt(s.slice(0, k).trim()), leadingInt(s.slice(k + 1).trim())]}static starsOf = (rating) => {if (rating <= 0) { return 0 }if (rating < 32) { return 1 }if (rating < 96) { return 2 }if (rating < 160) { return 3 }if (rating < 224) { return 4 }return 5}// THE RAW READ of an mp3: what the tags say, before normalising. { title, artist, albumArtist, album, track, disc, year,// genre, duration, popm = [{owner, rating, count}], pcnt, cover = { mime, data } | null } — or null when unreadable.static readMp3 = (path, size) => {let b = headBytes(path, 131072)if (b == null) { return null }tl = tagLength(b)let need = tl + 4096if (need > 8388608) { need = 8388608 }if (need > b.length && size > b.length) {more = headBytes(path, need)if (more != null) { b = more }}t = readId3v2(b)text = t == null ? {} : t.textlet title = first(text, 'TIT2')let artist = first(text, 'TPE1')let album = first(text, 'TALB')let year = first(text, 'TDRC')if (year == '') { year = first(text, 'TYER') }if (year == '') { year = first(text, 'TORY') }if (year == '') { year = first(text, 'TDOR') }let genre = first(text, 'TCON')let track = first(text, 'TRCK')if (t == null || (title == '' && artist == '' && album == '')) {tail = tailBytes(path, 128)v1 = tail == null ? null : readId3v1(tail)if (v1 != null) {if (title == '') { title = v1.title }if (artist == '') { artist = v1.artist }if (album == '') { album = v1.album }if (year == '') { year = v1.year }if (genre == '' && v1.genre < genreNames.length) { genre = genreNames[v1.genre] }if (track == '' && v1.track != null) { track = '' + v1.track }}}let duration = nulltlen = leadingInt(first(text, 'TLEN'))audioStart = tlsecs = mp3Seconds(b, audioStart, size - audioStart)if (secs != null) { duration = secs }else if (tlen != null) { duration = tlen / 1000 }return {title = title artist = artist albumArtist = first(text, 'TPE2') album = album track = trackdisc = first(text, 'TPOS') year = year genre = genre duration = durationpopm = t == null ? [] : t.popm pcnt = t == null ? null : t.pcnt cover = t == null ? null : t.cover}}// THE RAW READ of anything else, with ffprobe (one process): the format's tags and durationstatic readProbe = (path) => {r = run(['ffprobe', '-v', 'error', '-print_format', 'json', '-show_entries', 'format=duration:format_tags:stream_tags', path], { timeout = 60 })if (r == null || r.exit != 0) { return null }let text = ''for (l of r.lines) { text = text + l + ' ' }if (!text.includes('"format"')) { return null }j = JSON.parse(text)if (j == null || j.format == null) { return null }tags = {}// the tags of the format first; an ogg or flac file keeps them on its audio streamif (j.streams != null && j.streams.length != null) {for (st of j.streams) {if (st.tags != null) {for (k in st.tags) { tags[k.toLowerCase()] = st.tags[k] }}}}if (j.format.tags != null) {for (k in j.format.tags) { tags[k.toLowerCase()] = j.format.tags[k] }}pick = (a, b2) => {let v = tags[a]if (v == null && b2 != null) { v = tags[b2] }return v == null ? '' : ('' + v).trim()}dur = j.format.duration == null ? null : toNumber(j.format.duration)return {title = pick('title', null) artist = pick('artist', null) albumArtist = pick('album_artist', 'albumartist') album = pick('album', null)track = pick('track', 'tracknumber') disc = pick('disc', 'discnumber') year = pick('date', 'year') genre = pick('genre', null)duration = dur popm = [] pcnt = null cover = null}}static videoExt = { 'mp4' = true 'm4v' = true 'mkv' = true 'webm' = true 'mov' = true 'avi' = true }// THE SONG RECORD from a raw read. `meta` = { path, size, changeTime }.static songFrom = (raw, meta, coverId) => {path = meta.pathfolder = folderOf(path)ext = extOf(path)let artist = raw.artistlet albumArtist = raw.albumArtistif (artist == '') { artist = albumArtist }if (artist == '') { artist = 'Unknown artist' }let album = raw.albumif (album == '') { album = 'Unknown album' }let title = raw.titleif (title == '') { title = baseName(path) }tn = pairOf(raw.track)dn = pairOf(raw.disc)let play = raw.pcnt == null ? 0 : raw.pcntlet rating = 0for (p of raw.popm) {if (p.count > play) { play = p.count }if (p.rating > rating) { rating = p.rating }}groupArtist = albumArtist != '' ? albumArtist : artistparts = splitDisc(album)album = parts.namealbumKey = albumKeyOf(groupArtist, album)disc = discFor(dn[0], parts.disc)track = tn[0] == null ? 0 : tn[0]keys = sortKeys(groupArtist, album, disc, track, title)return {path = path folder = folder title = title artist = artist albumArtist = groupArtist album = albumtrack = track trackTotal = tn[1] disc = disc discTotal = dn[1] year = yearOf(raw.year)genre = genreName(raw.genre) duration = raw.duration format = ext kind = videoExt[ext] == true ? 'video' : 'audio'size = meta.size changeTime = meta.changeTime playCount = play popm = raw.popm rating = ratingstars = starsOf(rating) coverId = coverIdalbumKey = albumKey artistKey = safeKey(fold(groupArtist))search = fold(artist + ' ' + title + ' ' + album + ' ' + raw.genre)sortArtist = keys.sortArtistsortAlbum = keys.sortAlbumsortTitle = safeKey(fold(title) + SEP + fold(groupArtist) + SEP + fold(album))}}
Branches
Latest commits
- c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre
- 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