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 (hl:fs `readBytes(path, offset, length)`, hybriel#233) and the ID3v1 block// (the last 128 bytes, found with `stat`); 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 { readBytes, stat } from 'hl:fs'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) => {return readBytes(path, 0, n)}static tailBytes = (path, n) => {st = stat(path)if (st == null || st.size < n) { return null }return readBytes(path, st.size - n, n)}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
- 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
- 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