-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
400 lines (340 loc) · 10.4 KB
/
server.js
File metadata and controls
400 lines (340 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
const express = require('express')
const multer = require('multer')
const fs = require('fs')
const csv = require('csv-parser')
const yts = require('yt-search')
const archiver = require('archiver')
const youtubedl = require('youtube-dl-exec')
const { spawn } = require('child_process')
const app = express()
const upload = multer({ dest: 'uploads/' })
// serve index.html + assets
app.use(express.static('.'))
// simple in-memory progress tracker (single-user / local use)
const progress = { total: 0, done: 0 }
// normalize user-entered quality (e.g. "128" -> "128K", "best" -> "0")
function normalizeQuality (q) {
if (!q) return '0' // default: best VBR
q = String(q).trim().toLowerCase()
if (q === 'best' || q === '0') return '0' // yt-dlp: 0 = best VBR
if (/^\d+$/.test(q)) return q + 'K' // "128" -> "128K"
if (/^\d+k$/.test(q)) return q.toUpperCase() // "128k" -> "128K"
return '0'
}
// turn song title into a safe filename WITH SPACES (no slashes etc)
function makeFileBaseFromTitle (title) {
return String(title)
.replace(/[\/\\?%*:|"<>]/g, ' ') // remove path-unsafe chars
.replace(/\s+/g, ' ') // collapse multiple spaces
.trim()
}
// helper: get first non-empty field from a list of possible column names
function getField (row, candidates) {
for (const key of candidates) {
if (Object.prototype.hasOwnProperty.call(row, key) && row[key] != null) {
const val = String(row[key]).trim()
if (val) return val
}
}
return ''
}
// helper: derive year from multiple possible fields
function getYear (row) {
// direct year-ish fields
const directYearRaw = getField(row, [
'year',
'Year',
'release_year',
'Release Year',
'ReleaseYear',
'published_year',
'Published Year'
])
if (directYearRaw) {
const m = String(directYearRaw).match(/\d{4}/)
if (m) return m[0]
}
// album / release date style fields
const albumDateRaw = getField(row, [
'albumdate',
'AlbumDate',
'album_date',
'album date',
'album_release_date',
'Album Release Date',
'release_date',
'Release Date',
'released_at',
'Released At',
'date',
'Date'
])
if (albumDateRaw) {
const m = String(albumDateRaw).match(/\d{4}/)
if (m) return m[0]
}
return ''
}
// helper: get genre from multiple possible fields
function getGenre (row) {
return getField(row, [
'genre',
'Genre',
'genres',
'Genres',
'style',
'Style',
'mood',
'Mood'
])
}
// 1) Download MP3 audio only (no metadata, no thumbnail)
function downloadMP3 (url, outputTemplate, userQuality) {
const audioQuality = normalizeQuality(userQuality)
return youtubedl(url, {
extractAudio: true, // -x / --extract-audio
audioFormat: 'mp3', // --audio-format mp3
audioQuality, // --audio-quality (0-10 or "128K" etc)
noPlaylist: true, // --no-playlist
output: outputTemplate // -o "<folder>/<file>.%(ext)s"
// NOTE: no embedThumbnail / addMetadata here
})
}
// 2) Fetch album art from iTunes (square) and save as JPG
async function fetchAlbumArt (title, artist, tempFolder, fileBase) {
try {
const term = `${title} ${artist}`
const apiURL = `https://itunes.apple.com/search?term=${encodeURIComponent(
term
)}&entity=song&limit=1`
const res = await fetch(apiURL)
if (!res.ok) {
console.log('iTunes search failed:', res.status)
return null
}
const json = await res.json()
if (!json.results || !json.results.length) {
console.log('No iTunes result for:', term)
return null
}
// artworkUrl100 is square 100x100; we can often get a larger square:
// .../100x100bb.jpg -> .../600x600bb.jpg
let artUrl = json.results[0].artworkUrl100
if (artUrl) {
artUrl = artUrl.replace(/100x100bb\.jpg$/, '600x600bb.jpg')
}
const imgRes = await fetch(artUrl)
if (!imgRes.ok) {
console.log('Failed to download artwork:', artUrl)
return null
}
const arrayBuffer = await imgRes.arrayBuffer()
const buffer = Buffer.from(arrayBuffer)
const coverPath = `${tempFolder}/${fileBase}_cover.jpg`
await fs.promises.writeFile(coverPath, buffer)
return coverPath
} catch (err) {
console.log('Error fetching album art:', err)
return null
}
}
// 3) Use ffmpeg to apply metadata + optional cover art
function applyMetadataAndCover (mp3Path, coverPath, meta) {
return new Promise((resolve, reject) => {
// Make sure the temp file still ends in .mp3 so ffmpeg knows the format
const tempOut = mp3Path.replace(/\.mp3$/i, '.tagged.mp3')
const args = []
// overwrite output if exists
args.push('-y')
// INPUTS
args.push('-i', mp3Path) // audio
if (coverPath) {
args.push('-i', coverPath) // cover image
}
// MAPS
if (coverPath) {
// map audio & image
args.push('-map', '0:a')
args.push('-map', '1:v')
} else {
args.push('-map', '0:a')
}
// copy streams, don't re-encode
args.push('-c', 'copy')
// ensure proper ID3v2 + write ID3v1 for older players/iPods
args.push('-id3v2_version', '3')
args.push('-write_id3v1', '1')
// metadata from CSV
if (meta.title) args.push('-metadata', `title=${meta.title}`)
if (meta.artist) args.push('-metadata', `artist=${meta.artist}`)
if (meta.album) args.push('-metadata', `album=${meta.album}`)
if (meta.genre) args.push('-metadata', `genre=${meta.genre}`)
if (meta.year) {
// keep it simple: standard "year" field for ID3v2.3
args.push('-metadata', `year=${meta.year}`)
}
// extra tags for cover
if (coverPath) {
args.push('-metadata:s:v', 'title=Album cover')
args.push('-metadata:s:v', 'comment=Cover (front)')
}
// OUTPUT
args.push(tempOut)
const ff = spawn('ffmpeg', args)
ff.stderr.on('data', (d) => {
// ffmpeg logs – useful for debugging
process.stderr.write(d.toString())
})
ff.on('close', (code) => {
if (code === 0) {
// Replace original file with tagged version
fs.promises
.rename(tempOut, mp3Path)
.then(async () => {
// clean up cover file once we've embedded it
if (coverPath) {
try {
await fs.promises.unlink(coverPath)
} catch (e) {
// ignore delete errors
}
}
resolve()
})
.catch(reject)
} else {
// clean up temp on error
fs.promises
.unlink(tempOut)
.catch(() => {})
.finally(() => {
reject(new Error('ffmpeg exited with code ' + code))
})
}
})
ff.on('error', (err) => reject(err))
})
}
// Simple endpoint for polling progress from frontend
app.get('/progress', (req, res) => {
res.json(progress)
})
// Handle CSV upload → return ZIP
app.post('/upload', upload.single('csv'), async (req, res) => {
const csvPath = req.file.path
const songs = []
const userQuality = req.body.quality || '0'
fs.createReadStream(csvPath)
.pipe(csv())
.on('data', (row) => songs.push(row))
.on('end', async () => {
const tempFolder = 'mp3s_' + Date.now()
fs.mkdirSync(tempFolder)
progress.total = songs.length
progress.done = 0
for (const s of songs) {
try {
// UNIVERSAL FIELD DETECTION
const title = getField(s, [
'title',
'Title',
'track',
'Track',
'track_name',
'Track Name',
'trackName',
'name',
'Name',
'song',
'Song'
])
const artist = getField(s, [
'artist',
'Artist',
'artists',
'Artists',
'artist_name',
'Artist Name',
'singer',
'Singer',
'performer',
'Performer'
])
const album = getField(s, [
'album',
'Album',
'album_name',
'Album Name',
'albumName',
'record',
'Record',
'release',
'Release'
])
const year = getYear(s)
const genre = getGenre(s)
if (!title || !artist) {
console.log('Skipping row (no title/artist):', s)
progress.done++
continue
}
const query = `${title} ${artist}`
console.log('Searching:', query)
const results = await yts(query)
const video = results.videos[0]
if (!video) {
console.log('No video found for:', query)
progress.done++
continue
}
// filename ONLY from title, with spaces (no underscores)
const fileBase = makeFileBaseFromTitle(title)
// yt-dlp will write "<fileBase>.mp3"
const outputTemplate = `${tempFolder}/${fileBase}.%(ext)s`
const mp3Path = `${tempFolder}/${fileBase}.mp3`
// 1) download pure audio
await downloadMP3(video.url, outputTemplate, userQuality)
// 2) fetch nice square album art (movie/album style)
const coverPath = await fetchAlbumArt(
title,
artist,
tempFolder,
fileBase
)
// 3) apply metadata from CSV + embed cover
try {
await applyMetadataAndCover(mp3Path, coverPath, {
title,
artist,
album,
year,
genre
})
console.log('Tagged & Downloaded:', mp3Path)
} catch (tagErr) {
console.log(
'Tagging / cover error (keeping audio anyway):',
tagErr
)
}
} catch (err) {
console.log('Error downloading:', err)
} finally {
progress.done++
}
}
// when we're done, reset progress after a little while (optional)
setTimeout(() => {
progress.total = 0
progress.done = 0
}, 60_000)
// Create ZIP to send to user
res.setHeader('Content-Type', 'application/zip')
res.setHeader('Content-Disposition', 'attachment; filename=songs.zip')
const zip = archiver('zip')
zip.pipe(res)
zip.directory(tempFolder, false)
zip.finalize()
})
})
app.listen(3000, () => console.log('Server running at http://localhost:3000'))