Spaces:
Sleeping
Sleeping
Download backend/src/core/metadata.ts from kishankrish19/Chat: direct link, hf CLI and curl.
- Browser
- Download file 2.73 kB
-
https://huggingface.co/spaces/kishankrish19/Chat/resolve/main/backend/src/core/metadata.ts
- Command line
-
hf download hf://spaces/kishankrish19/Chat/backend/src/core/metadata.ts
-
curl -L -o metadata.ts https://huggingface.co/spaces/kishankrish19/Chat/resolve/main/backend/src/core/metadata.ts
2.73 kB
| import type { IAudioMetadata } from 'music-metadata'; | |
| import type { Track } from './models'; | |
| import type { Store } from './store'; | |
| export function saveMetadata(store: Store, track: Track) { | |
| const { artwork, artwork_mime, ...rest } = track; | |
| if (artwork) | |
| store.put( | |
| 'artwork', | |
| { id: track.id, data: artwork, mime: artwork_mime }, | |
| track.owner_id || '', | |
| ); | |
| const saved = { | |
| ...rest, | |
| has_artwork: !!artwork || !!track.has_artwork, | |
| metadata_updated_at: new Date().toISOString(), | |
| }; | |
| store.put('tracks', saved, track.owner_id || ''); | |
| return saved; | |
| } | |
| // Keep lyrics and binary artwork out of list responses. Never invent absent tags. | |
| export function audioMetadata(metadata: IAudioMetadata): Partial<Track> { | |
| const c = metadata.common, | |
| f = metadata.format; | |
| const picture = c.picture?.find( | |
| (p) => | |
| ['image/jpeg', 'image/png', 'image/webp'].includes(p.format) && | |
| p.data.length <= 4 * 1024 * 1024, | |
| ); | |
| const lyrics = (c.lyrics || []) | |
| .slice(0, 8) | |
| .map((l) => ({ | |
| language: l.language, | |
| text: (l.text || '').slice(0, 100000), | |
| lines: (l.syncText || []).slice(0, 5000).map((line) => ({ | |
| text: line.text.slice(0, 2000), | |
| ...(l.timeStampFormat === 2 && Number.isFinite(line.timestamp) | |
| ? { timestamp: line.timestamp } | |
| : {}), | |
| })), | |
| })) | |
| .filter((l) => l.text || l.lines.length); | |
| return { | |
| ...(c.title ? { title: c.title } : {}), | |
| ...(c.artists?.length || c.artist | |
| ? { | |
| artist_name: (c.artists?.length ? c.artists : [c.artist!]).join(', '), | |
| } | |
| : {}), | |
| ...(c.album ? { album_title: c.album } : {}), | |
| ...(c.genre?.length ? { genre: c.genre.join(', ') } : {}), | |
| ...(f.duration ? { duration_ms: Math.round(f.duration * 1000) } : {}), | |
| artwork: picture ? Buffer.from(picture.data).toString('base64') : undefined, | |
| artwork_mime: picture?.format, | |
| lyrics, | |
| metadata_version: 2, | |
| metadata_error: undefined, | |
| details: { | |
| artists: c.artists || (c.artist ? [c.artist] : []), | |
| album_artist: c.albumartist, | |
| year: c.year, | |
| date: c.date, | |
| original_date: c.originaldate, | |
| composers: c.composer, | |
| track_number: c.track.no, | |
| track_total: c.track.of, | |
| disc_number: c.disk.no, | |
| disc_total: c.disk.of, | |
| genres: c.genre, | |
| label: c.label, | |
| copyright: c.copyright, | |
| isrc: c.isrc, | |
| bpm: c.bpm, | |
| encoded_by: c.encodedby, | |
| codec: f.codec, | |
| container: f.container, | |
| bitrate: f.bitrate, | |
| sample_rate: f.sampleRate, | |
| bits_per_sample: f.bitsPerSample, | |
| channels: f.numberOfChannels, | |
| lossless: f.lossless, | |
| tag_formats: f.tagTypes, | |
| }, | |
| }; | |
| } | |