-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
69 lines (58 loc) · 1.99 KB
/
main.ts
File metadata and controls
69 lines (58 loc) · 1.99 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
import * as log from 'jsr:@std/log';
import * as path from 'jsr:@std/path';
import { walk } from 'jsr:@std/fs/walk';
import { type IAudioMetadata, parseFile } from 'npm:music-metadata';
import parse from './cli.ts';
import setup from './logger.ts';
// DEFINE FUNCTIONS
async function isMp3BelowBitrate(filePath: string): Promise<boolean> {
try {
const metadata: IAudioMetadata = await parseFile(filePath);
const metadataBitrate = metadata.format.bitrate;
if (metadataBitrate === undefined) {
log.error(`could not get bitrate from ${filePath}`);
Deno.exit(1);
}
const bitrateKbps = Math.round(metadataBitrate / 1000);
if (bitrateKbps < args.bitratelimit) {
return true;
}
} catch (err: unknown) {
log.error(`Error processing ${filePath}: ${(err as Error).message}`);
Deno.exit(1);
}
return false;
}
async function main() {
const targetDir = args.path;
log.info(`Scanning directory: ${targetDir}`);
log.info(`Bitrate limit: ${args.bitratelimit}kbps`);
if ((await Array.fromAsync(walk(targetDir, { exts: ['.mp3'] }))).length === 0) {
log.warn('no files found');
Deno.exit(0);
}
for await (const dirEntry of walk(targetDir, { includeFiles: false, includeSymlinks: false })) {
for await (
const entry of walk(dirEntry.path, { exts: ['.mp3'], includeSymlinks: false, includeDirs: false, maxDepth: 1 })
) {
const isFileBelowBitrate = await isMp3BelowBitrate(entry.path);
if (isFileBelowBitrate) {
if (log.getLogger().levelName !== 'DEBUG') {
const basePath = path.dirname(entry.path);
log.info(`"${basePath}" contains at least one file below ${args.bitratelimit}kbps`);
break;
}
log.debug(`"${entry.path}" is below ${args.bitratelimit}kbps`);
}
}
}
}
// RUN PROGRAM
const args = await parse();
try {
await setup(args);
} catch (err: unknown) {
log.error(`Error setting up logger: ${(err as Error).message}`);
Deno.exit(1);
}
main().catch(log.error);