mix-id 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +121 -0
- package/cli.mjs +135 -0
- package/lib/audio.mjs +84 -0
- package/lib/format.mjs +56 -0
- package/lib/recognize.mjs +27 -0
- package/lib/scanner.mjs +94 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Scott Bell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# mix-id
|
|
2
|
+
|
|
3
|
+
Identify every track in a DJ mix ā from a local file or streaming URL.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
$ npx mix-id https://soundcloud.com/dj/my-set
|
|
7
|
+
|
|
8
|
+
š„ Downloading...
|
|
9
|
+
ā
my-set.mp3 (142.3 MB)
|
|
10
|
+
|
|
11
|
+
šµ mix-id
|
|
12
|
+
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
13
|
+
File: my-set.mp3
|
|
14
|
+
Duration: 1:30:12
|
|
15
|
+
Settings: 30s step, 18s sample
|
|
16
|
+
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
17
|
+
|
|
18
|
+
[00:00] 1% ā
The Orb ā Little Fluffy Clouds
|
|
19
|
+
[00:30] 1% ā©ļø The Orb ā Little Fluffy Clouds
|
|
20
|
+
[01:00] 2% ā
Surface ā Falling in Love
|
|
21
|
+
...
|
|
22
|
+
|
|
23
|
+
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
24
|
+
š§ TRACKLIST ā my-set.mp3
|
|
25
|
+
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
26
|
+
1. [00:00] The Orb ā Little Fluffy Clouds
|
|
27
|
+
2. [01:00] Surface ā Falling in Love
|
|
28
|
+
3. [04:30] Madonna ā Vogue
|
|
29
|
+
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
30
|
+
|
|
31
|
+
š¾ Output:
|
|
32
|
+
my-set_tracklist.txt
|
|
33
|
+
my-set.cue
|
|
34
|
+
my-set_tracklist.json
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install -g mix-id
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Or run directly:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npx mix-id my-mix.mp3
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Requirements
|
|
50
|
+
|
|
51
|
+
- **Node.js** 18+
|
|
52
|
+
- **ffmpeg** ā audio processing (`brew install ffmpeg`)
|
|
53
|
+
- **yt-dlp** ā URL downloads (`brew install yt-dlp`) ā only needed for URLs
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# Local file
|
|
59
|
+
mix-id my-mix.mp3
|
|
60
|
+
|
|
61
|
+
# SoundCloud
|
|
62
|
+
mix-id https://soundcloud.com/dj/set-name
|
|
63
|
+
|
|
64
|
+
# Mixcloud
|
|
65
|
+
mix-id https://www.mixcloud.com/dj/show-name
|
|
66
|
+
|
|
67
|
+
# YouTube
|
|
68
|
+
mix-id https://www.youtube.com/watch?v=...
|
|
69
|
+
|
|
70
|
+
# Custom scan settings
|
|
71
|
+
mix-id my-mix.mp3 --step 60 --segment 20
|
|
72
|
+
|
|
73
|
+
# Resume from a specific position
|
|
74
|
+
mix-id my-mix.mp3 --start 3600
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Options
|
|
78
|
+
|
|
79
|
+
| Flag | Default | Description |
|
|
80
|
+
|------|---------|-------------|
|
|
81
|
+
| `--step` | `30` | Seconds between scan points |
|
|
82
|
+
| `--segment` | `18` | Sample length for recognition |
|
|
83
|
+
| `--start` | `0` | Skip to this position (seconds) |
|
|
84
|
+
| `--help` | | Show help |
|
|
85
|
+
|
|
86
|
+
## Output
|
|
87
|
+
|
|
88
|
+
mix-id generates three files:
|
|
89
|
+
|
|
90
|
+
- **`_tracklist.txt`** ā Paste-friendly format for Mixcloud, etc.
|
|
91
|
+
- **`.cue`** ā CUE sheet with track markers and timestamps
|
|
92
|
+
- **`_tracklist.json`** ā Structured data with full metadata
|
|
93
|
+
|
|
94
|
+
## How it works
|
|
95
|
+
|
|
96
|
+
1. Downloads audio from URL (if given) using yt-dlp
|
|
97
|
+
2. Splits the audio into overlapping segments
|
|
98
|
+
3. Fingerprints each segment via Shazam's recognition API
|
|
99
|
+
4. Deduplicates consecutive matches (handles DJ transitions)
|
|
100
|
+
5. Outputs clean tracklist in multiple formats
|
|
101
|
+
|
|
102
|
+
## Supported sources
|
|
103
|
+
|
|
104
|
+
Any URL that [yt-dlp](https://github.com/yt-dlp/yt-dlp) supports ā that's **1000+ sites** including:
|
|
105
|
+
|
|
106
|
+
- SoundCloud
|
|
107
|
+
- Mixcloud
|
|
108
|
+
- YouTube
|
|
109
|
+
- Bandcamp
|
|
110
|
+
- And many more
|
|
111
|
+
|
|
112
|
+
## Tips
|
|
113
|
+
|
|
114
|
+
- **Longer mixes?** The default 30s step works well. Use `--step 60` to scan faster at the cost of precision.
|
|
115
|
+
- **Transitions fuzzy?** Shazam sometimes bounces between two tracks during a mix. mix-id deduplicates these automatically.
|
|
116
|
+
- **No API key needed.** mix-id uses Shazam's public recognition endpoint.
|
|
117
|
+
- **Rate limited?** mix-id waits 2s between requests to be respectful. A 2-hour mix takes ~8 minutes to scan.
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
MIT
|
package/cli.mjs
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* mix-id ā Identify tracks in any DJ mix.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* mix-id <file-or-url> [options]
|
|
8
|
+
*
|
|
9
|
+
* Examples:
|
|
10
|
+
* mix-id my-mix.mp3
|
|
11
|
+
* mix-id https://soundcloud.com/dj/set-name
|
|
12
|
+
* mix-id https://www.mixcloud.com/dj/show-name
|
|
13
|
+
* mix-id https://youtube.com/watch?v=... --step 60
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { existsSync } from 'fs';
|
|
17
|
+
import { basename } from 'path';
|
|
18
|
+
import { scan } from './lib/scanner.mjs';
|
|
19
|
+
import { dedupe, formatTime, writeTXT, writeCUE, writeJSON } from './lib/format.mjs';
|
|
20
|
+
import { downloadURL, fileSize, hasCommand } from './lib/audio.mjs';
|
|
21
|
+
|
|
22
|
+
// --- Parse args ---
|
|
23
|
+
|
|
24
|
+
const args = process.argv.slice(2);
|
|
25
|
+
const flags = {};
|
|
26
|
+
const positional = [];
|
|
27
|
+
|
|
28
|
+
for (let i = 0; i < args.length; i++) {
|
|
29
|
+
if (args[i].startsWith('--')) {
|
|
30
|
+
const key = args[i].slice(2);
|
|
31
|
+
flags[key] = args[i + 1] || true;
|
|
32
|
+
i++;
|
|
33
|
+
} else {
|
|
34
|
+
positional.push(args[i]);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const input = positional[0];
|
|
39
|
+
const step = parseInt(flags.step || '30');
|
|
40
|
+
const segment = parseInt(flags.segment || '18');
|
|
41
|
+
const start = parseInt(flags.start || '0');
|
|
42
|
+
|
|
43
|
+
if (!input || flags.help) {
|
|
44
|
+
console.log(`
|
|
45
|
+
mix-id ā Identify tracks in any DJ mix.
|
|
46
|
+
|
|
47
|
+
Usage:
|
|
48
|
+
mix-id <file-or-url> [options]
|
|
49
|
+
|
|
50
|
+
Options:
|
|
51
|
+
--step <sec> Time between scan points (default: 30)
|
|
52
|
+
--segment <sec> Sample length for recognition (default: 18)
|
|
53
|
+
--start <sec> Skip to position before scanning (default: 0)
|
|
54
|
+
--help Show this help
|
|
55
|
+
|
|
56
|
+
Examples:
|
|
57
|
+
mix-id my-mix.mp3
|
|
58
|
+
mix-id https://soundcloud.com/dj/set-name
|
|
59
|
+
mix-id mix.wav --step 60 --segment 20
|
|
60
|
+
|
|
61
|
+
Requirements:
|
|
62
|
+
ffmpeg Audio processing (brew install ffmpeg)
|
|
63
|
+
yt-dlp URL downloads (brew install yt-dlp)
|
|
64
|
+
`);
|
|
65
|
+
process.exit(input ? 0 : 1);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// --- Preflight checks ---
|
|
69
|
+
|
|
70
|
+
if (!hasCommand('ffmpeg') || !hasCommand('ffprobe')) {
|
|
71
|
+
console.error('ā ffmpeg is required. Install: brew install ffmpeg');
|
|
72
|
+
process.exit(1);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// --- Resolve input ---
|
|
76
|
+
|
|
77
|
+
const isURL = /^https?:\/\//i.test(input);
|
|
78
|
+
let file;
|
|
79
|
+
|
|
80
|
+
if (isURL) {
|
|
81
|
+
console.log(`\nš„ Downloading...`);
|
|
82
|
+
console.log(` ${input}\n`);
|
|
83
|
+
file = downloadURL(input, process.cwd());
|
|
84
|
+
console.log(`ā
${basename(file)} (${fileSize(file)})\n`);
|
|
85
|
+
} else {
|
|
86
|
+
file = input;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!existsSync(file)) {
|
|
90
|
+
console.error(`ā File not found: ${file}`);
|
|
91
|
+
process.exit(1);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// --- Scan ---
|
|
95
|
+
|
|
96
|
+
const result = await scan(file, { step, segment, start });
|
|
97
|
+
|
|
98
|
+
// --- Dedupe & output ---
|
|
99
|
+
|
|
100
|
+
const tracks = dedupe(result.tracks);
|
|
101
|
+
|
|
102
|
+
if (tracks.length === 0) {
|
|
103
|
+
console.log('\nā No tracks identified.');
|
|
104
|
+
process.exit(0);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Print tracklist
|
|
108
|
+
console.log('\n' + 'ā'.repeat(50));
|
|
109
|
+
console.log(`š§ TRACKLIST ā ${basename(file)}`);
|
|
110
|
+
console.log('ā'.repeat(50));
|
|
111
|
+
tracks.forEach((t, i) => {
|
|
112
|
+
const num = String(i + 1).padStart(2);
|
|
113
|
+
const album = t.album ? ` [${t.album}]` : '';
|
|
114
|
+
console.log(`${num}. [${t.timestamp}] ${t.artist} ā ${t.title}${album}`);
|
|
115
|
+
});
|
|
116
|
+
const removed = result.tracks.length - tracks.length;
|
|
117
|
+
if (removed > 0) console.log(`\nš ${removed} duplicate(s) removed`);
|
|
118
|
+
console.log('ā'.repeat(50));
|
|
119
|
+
|
|
120
|
+
// Write files
|
|
121
|
+
const base = file.replace(/\.[^.]+$/, '');
|
|
122
|
+
const audioFilename = basename(file);
|
|
123
|
+
|
|
124
|
+
writeTXT(tracks, base + '_tracklist.txt');
|
|
125
|
+
writeCUE(tracks, base + '.cue', audioFilename);
|
|
126
|
+
writeJSON(tracks, base + '_tracklist.json', {
|
|
127
|
+
source: audioFilename,
|
|
128
|
+
duration: result.duration,
|
|
129
|
+
segments_scanned: result.segmentsScanned,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
console.log(`\nš¾ Output:`);
|
|
133
|
+
console.log(` ${base}_tracklist.txt`);
|
|
134
|
+
console.log(` ${base}.cue`);
|
|
135
|
+
console.log(` ${base}_tracklist.json`);
|
package/lib/audio.mjs
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Audio utilities ā download, probe, and extract segments.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { execSync, spawnSync } from 'child_process';
|
|
6
|
+
import { readFileSync, existsSync, statSync } from 'fs';
|
|
7
|
+
import { join, basename } from 'path';
|
|
8
|
+
|
|
9
|
+
/** Check if a command exists on PATH. */
|
|
10
|
+
export function hasCommand(cmd) {
|
|
11
|
+
try {
|
|
12
|
+
execSync(`which ${cmd}`, { stdio: 'ignore' });
|
|
13
|
+
return true;
|
|
14
|
+
} catch {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Get audio duration in seconds via ffprobe. */
|
|
20
|
+
export function getDuration(file) {
|
|
21
|
+
const out = execSync(
|
|
22
|
+
`ffprobe -v quiet -print_format json -show_format "${file}"`,
|
|
23
|
+
{ encoding: 'utf8' }
|
|
24
|
+
);
|
|
25
|
+
return parseFloat(JSON.parse(out).format.duration);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Extract a segment as raw s16le mono 16kHz PCM for Shazam. */
|
|
29
|
+
export function extractSegment(file, startSec, durationSec, outPath) {
|
|
30
|
+
try {
|
|
31
|
+
execSync(
|
|
32
|
+
`ffmpeg -y -ss ${startSec} -t ${durationSec} ` +
|
|
33
|
+
`-i "${file}" -ac 1 -ar 16000 -f s16le -acodec pcm_s16le "${outPath}" 2>/dev/null`
|
|
34
|
+
);
|
|
35
|
+
return true;
|
|
36
|
+
} catch {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Download audio from a URL using yt-dlp. Returns path to downloaded file. */
|
|
42
|
+
export function downloadURL(url, outputDir) {
|
|
43
|
+
if (!hasCommand('yt-dlp')) {
|
|
44
|
+
throw new Error('yt-dlp is required for URL downloads. Install: brew install yt-dlp');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Get title for filename
|
|
48
|
+
const info = spawnSync('yt-dlp', ['--print', 'title', '--no-download', url], {
|
|
49
|
+
encoding: 'utf8',
|
|
50
|
+
timeout: 30_000,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const title = (info.status === 0 && info.stdout.trim())
|
|
54
|
+
? info.stdout.trim().replace(/[^a-zA-Z0-9\s\-_]/g, '').replace(/\s+/g, '-').toLowerCase().slice(0, 80)
|
|
55
|
+
: 'downloaded-mix';
|
|
56
|
+
|
|
57
|
+
const outTemplate = join(outputDir, `${title}.%(ext)s`);
|
|
58
|
+
|
|
59
|
+
const dl = spawnSync('yt-dlp', [
|
|
60
|
+
'-x', '--audio-format', 'mp3', '--audio-quality', '0',
|
|
61
|
+
'-o', outTemplate, '--no-playlist', '--progress', url,
|
|
62
|
+
], {
|
|
63
|
+
encoding: 'utf8',
|
|
64
|
+
timeout: 600_000,
|
|
65
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Find the output file
|
|
69
|
+
const mp3Path = join(outputDir, `${title}.mp3`);
|
|
70
|
+
if (existsSync(mp3Path)) return mp3Path;
|
|
71
|
+
|
|
72
|
+
for (const ext of ['webm', 'opus', 'm4a', 'ogg', 'wav']) {
|
|
73
|
+
const alt = join(outputDir, `${title}.${ext}`);
|
|
74
|
+
if (existsSync(alt)) return alt;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const errMsg = [dl.stdout, dl.stderr].filter(Boolean).join('\n');
|
|
78
|
+
throw new Error(`Download failed:\n${errMsg}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Format file size for display. */
|
|
82
|
+
export function fileSize(path) {
|
|
83
|
+
return (statSync(path).size / 1024 / 1024).toFixed(1) + ' MB';
|
|
84
|
+
}
|
package/lib/format.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Output formatting ā TXT, CUE, and JSON.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { writeFileSync } from 'fs';
|
|
6
|
+
import { basename } from 'path';
|
|
7
|
+
|
|
8
|
+
/** Format seconds as H:MM:SS or MM:SS. */
|
|
9
|
+
export function formatTime(sec) {
|
|
10
|
+
const h = Math.floor(sec / 3600);
|
|
11
|
+
const m = Math.floor((sec % 3600) / 60);
|
|
12
|
+
const s = Math.floor(sec % 60);
|
|
13
|
+
const mm = String(m).padStart(2, '0');
|
|
14
|
+
const ss = String(s).padStart(2, '0');
|
|
15
|
+
return h > 0 ? `${h}:${mm}:${ss}` : `${mm}:${ss}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Remove consecutive duplicate tracks (handles transition bouncing). */
|
|
19
|
+
export function dedupe(tracks) {
|
|
20
|
+
const result = [];
|
|
21
|
+
for (const t of tracks) {
|
|
22
|
+
const prev = result[result.length - 1];
|
|
23
|
+
if (prev && prev.title === t.title && prev.artist === t.artist) continue;
|
|
24
|
+
result.push(t);
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Write paste-friendly tracklist. */
|
|
30
|
+
export function writeTXT(tracks, outPath) {
|
|
31
|
+
const lines = tracks.map(t => `${t.artist} - ${t.title} ${t.timestamp}`);
|
|
32
|
+
writeFileSync(outPath, lines.join('\n') + '\n');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Write CUE sheet. */
|
|
36
|
+
export function writeCUE(tracks, outPath, audioFilename) {
|
|
37
|
+
const title = basename(outPath, '.cue').replace(/[-_]/g, ' ');
|
|
38
|
+
let cue = `REM GENRE DJ Mix\nTITLE "${title}"\nFILE "${audioFilename}" MP3\n`;
|
|
39
|
+
|
|
40
|
+
tracks.forEach((t, i) => {
|
|
41
|
+
const num = String(i + 1).padStart(2, '0');
|
|
42
|
+
const mm = String(Math.floor(t.position_sec / 60)).padStart(2, '0');
|
|
43
|
+
const ss = String(t.position_sec % 60).padStart(2, '0');
|
|
44
|
+
cue += ` TRACK ${num} AUDIO\n`;
|
|
45
|
+
cue += ` TITLE "${t.title}"\n`;
|
|
46
|
+
cue += ` PERFORMER "${t.artist}"\n`;
|
|
47
|
+
cue += ` INDEX 01 ${mm}:${ss}:00\n`;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
writeFileSync(outPath, cue);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Write JSON tracklist with metadata. */
|
|
54
|
+
export function writeJSON(tracks, outPath, meta = {}) {
|
|
55
|
+
writeFileSync(outPath, JSON.stringify({ ...meta, tracks }, null, 2));
|
|
56
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Track recognition via Shazam.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { createRequire } from 'module';
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
const { Shazam, s16LEToSamplesArray } = require('shazam-api');
|
|
8
|
+
|
|
9
|
+
const shazam = new Shazam();
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Recognize a track from raw s16le PCM audio data.
|
|
13
|
+
* Returns { title, artist, album, year } or null.
|
|
14
|
+
*/
|
|
15
|
+
export async function recognize(pcmBuffer) {
|
|
16
|
+
const samples = s16LEToSamplesArray(pcmBuffer);
|
|
17
|
+
const result = await shazam.recognizeSong(samples);
|
|
18
|
+
|
|
19
|
+
if (!result) return null;
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
title: result.title || 'Unknown',
|
|
23
|
+
artist: result.artist || 'Unknown',
|
|
24
|
+
album: result.album || '',
|
|
25
|
+
year: result.year || '',
|
|
26
|
+
};
|
|
27
|
+
}
|
package/lib/scanner.mjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core scanner ā splits audio into segments and identifies tracks.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { mkdtempSync, readFileSync, unlinkSync } from 'fs';
|
|
6
|
+
import { join, basename } from 'path';
|
|
7
|
+
import { tmpdir } from 'os';
|
|
8
|
+
import { getDuration, extractSegment } from './audio.mjs';
|
|
9
|
+
import { recognize } from './recognize.mjs';
|
|
10
|
+
import { formatTime } from './format.mjs';
|
|
11
|
+
|
|
12
|
+
const RATE_LIMIT_MS = 2000; // Be polite to Shazam
|
|
13
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Scan an audio file and identify tracks.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} file - Path to audio file
|
|
19
|
+
* @param {object} opts
|
|
20
|
+
* @param {number} opts.step - Seconds between scan points (default: 30)
|
|
21
|
+
* @param {number} opts.segment - Seconds per sample (default: 18)
|
|
22
|
+
* @param {number} opts.start - Start position in seconds (default: 0)
|
|
23
|
+
* @param {boolean} opts.quiet - Suppress progress output
|
|
24
|
+
* @returns {Promise<{ tracks: Array, duration: number, segmentsScanned: number }>}
|
|
25
|
+
*/
|
|
26
|
+
export async function scan(file, opts = {}) {
|
|
27
|
+
const step = opts.step ?? 30;
|
|
28
|
+
const segment = opts.segment ?? 18;
|
|
29
|
+
const start = opts.start ?? 0;
|
|
30
|
+
const quiet = opts.quiet ?? false;
|
|
31
|
+
|
|
32
|
+
const duration = getDuration(file);
|
|
33
|
+
const totalSegs = Math.floor((duration - segment) / step) + 1;
|
|
34
|
+
const tmpDir = mkdtempSync(join(tmpdir(), 'mix-id-'));
|
|
35
|
+
|
|
36
|
+
const log = quiet ? () => {} : (msg) => process.stdout.write(msg);
|
|
37
|
+
|
|
38
|
+
if (!quiet) {
|
|
39
|
+
console.log(`\nšµ mix-id`);
|
|
40
|
+
console.log('ā'.repeat(50));
|
|
41
|
+
console.log(`File: ${basename(file)}`);
|
|
42
|
+
console.log(`Duration: ${formatTime(duration)}`);
|
|
43
|
+
console.log(`Settings: ${step}s step, ${segment}s sample`);
|
|
44
|
+
console.log('ā'.repeat(50) + '\n');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const tracks = [];
|
|
48
|
+
let lastTrack = null;
|
|
49
|
+
let segNum = 0;
|
|
50
|
+
|
|
51
|
+
for (let pos = start; pos + segment <= duration; pos += step) {
|
|
52
|
+
segNum++;
|
|
53
|
+
const ts = formatTime(pos);
|
|
54
|
+
const pct = Math.round((segNum / totalSegs) * 100);
|
|
55
|
+
log(`[${ts}] ${pct}% `);
|
|
56
|
+
|
|
57
|
+
const segPath = join(tmpDir, `seg.raw`);
|
|
58
|
+
|
|
59
|
+
if (!extractSegment(file, pos, segment, segPath)) {
|
|
60
|
+
log('ā ļø skip\n');
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
const pcm = readFileSync(segPath);
|
|
66
|
+
const match = await recognize(pcm);
|
|
67
|
+
|
|
68
|
+
if (match) {
|
|
69
|
+
const same = lastTrack
|
|
70
|
+
&& lastTrack.title === match.title
|
|
71
|
+
&& lastTrack.artist === match.artist;
|
|
72
|
+
|
|
73
|
+
if (same) {
|
|
74
|
+
log(`ā©ļø ${match.artist} ā ${match.title}\n`);
|
|
75
|
+
} else {
|
|
76
|
+
log(`ā
${match.artist} ā ${match.title}\n`);
|
|
77
|
+
tracks.push({ timestamp: ts, position_sec: pos, ...match });
|
|
78
|
+
lastTrack = match;
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
log('ā\n');
|
|
82
|
+
lastTrack = null;
|
|
83
|
+
}
|
|
84
|
+
} catch (err) {
|
|
85
|
+
log(`ā ļø ${err.message}\n`);
|
|
86
|
+
lastTrack = null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
try { unlinkSync(segPath); } catch {}
|
|
90
|
+
await sleep(RATE_LIMIT_MS);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return { tracks, duration, segmentsScanned: segNum };
|
|
94
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mix-id",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Identify tracks in any DJ mix ā from a file or URL. Outputs tracklists as TXT, CUE, and JSON.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"mix-id": "./cli.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"cli.mjs",
|
|
11
|
+
"lib/"
|
|
12
|
+
],
|
|
13
|
+
"keywords": [
|
|
14
|
+
"dj",
|
|
15
|
+
"tracklist",
|
|
16
|
+
"shazam",
|
|
17
|
+
"music",
|
|
18
|
+
"recognition",
|
|
19
|
+
"mix",
|
|
20
|
+
"cue",
|
|
21
|
+
"mixcloud",
|
|
22
|
+
"soundcloud",
|
|
23
|
+
"audio",
|
|
24
|
+
"fingerprint"
|
|
25
|
+
],
|
|
26
|
+
"author": "Scott Bell",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "https://github.com/skip5this/mix-id"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=18"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"shazam-api": "^0.3.0"
|
|
37
|
+
}
|
|
38
|
+
}
|