source: Klonkt/scripts/backfill-durations.mjs@ 5861849

main
Last change on this file since 5861849 was 834bcc3, checked in by Robin Genis <roboburr@…>, 3 months ago

i18n: translate Dutch code comments to English across src/

Comments in routes/services/views/config/middleware/assets translated to
English for the public repo. A few dev-facing throw/console message strings
were Englished too. No user-facing UI strings or i18n dictionary values changed
(src/services/i18n.js untouched). Logic unchanged.

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 2.1 KB
Line 
1#!/usr/bin/env node
2/**
3 * Backfill audio_tracks.duration for existing tracks that have no duration yet.
4 * Reads the duration from the mp3 file via ffmpeg (probeDuration — no separate
5 * ffprobe binary required). Idempotent: only touches rows with duration NULL or 0,
6 * so it is safe to run repeatedly.
7 *
8 * npm run backfill:durations
9 *
10 * Respects AUDIO_PATH (env) just like the upload route.
11 */
12import path from 'path';
13import fs from 'fs';
14import { fileURLToPath } from 'url';
15import db from '../src/config/database.js';
16import { probeDuration } from '../src/services/AudioTranscoder.js';
17
18const __dirname = path.dirname(fileURLToPath(import.meta.url));
19const AUDIO_DIR = path.resolve(
20 process.env.AUDIO_PATH || path.join(__dirname, '..', 'storage', 'audio')
21);
22
23// storage_path is absolute (stored at upload time); fall back to AUDIO_DIR/filename.
24function resolveFile(t) {
25 const candidates = [t.storage_path, t.filename ? path.join(AUDIO_DIR, t.filename) : null].filter(Boolean);
26 for (const c of candidates) {
27 try { if (fs.statSync(c).isFile()) return c; } catch { /* try next candidate */ }
28 }
29 return null;
30}
31
32const rows = db.prepare(`
33 SELECT t.id, m.filename, m.storage_path
34 FROM audio_tracks t
35 JOIN media m ON m.id = t.media_id
36 WHERE (t.duration IS NULL OR t.duration = 0) AND m.filename IS NOT NULL
37`).all();
38
39console.log(`[backfill-durations] ${rows.length} track(s) zonder duur`);
40const update = db.prepare('UPDATE audio_tracks SET duration = ? WHERE id = ?');
41
42let ok = 0, miss = 0, fail = 0;
43for (const t of rows) {
44 const file = resolveFile(t);
45 if (!file) { console.warn(` - ${t.id}: bestand niet gevonden (${t.filename})`); miss++; continue; }
46 try {
47 const sec = await probeDuration(file);
48 if (sec && sec > 0) { update.run(sec, t.id); ok++; console.log(` ✓ ${t.id}: ${sec}s`); }
49 else { console.warn(` - ${t.id}: geen duur uit ffmpeg`); fail++; }
50 } catch (e) { console.warn(` - ${t.id}: ${e.message}`); fail++; }
51}
52console.log(`[backfill-durations] klaar — ${ok} bijgewerkt, ${miss} bestand-mist, ${fail} mislukt`);
53process.exit(0);
Note: See TracBrowser for help on using the repository browser.