-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathduration.mjs
More file actions
73 lines (62 loc) · 2.34 KB
/
duration.mjs
File metadata and controls
73 lines (62 loc) · 2.34 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
import {parseFile} from 'music-metadata';
import {existsSync, lstatSync, readdirSync} from "node:fs";
import path from "node:path";
/*
windows:
node duration.mjs "C:\Users\lucan\Nextcloud\_media\_DTTD - Supercuts\_supercuts"
*/
// Function to get the MP3 duration for a single file
async function getMp3Duration(filePath) {
try {
// Parse the local MP3 file to get its metadata
const metadata = await parseFile(filePath, {duration: true});
return metadata.format.duration; // Returns duration in seconds
} catch (error) {
console.error(`Error reading file ${filePath}:`, error.message);
return null; // Return null if there's an error
}
}
// Function to process all MP3 files in a given folder
async function processFolder(folderPath) {
try {
// Read all files in the folder
const files = readdirSync(folderPath);
// Filter for files with `.mp3` extension
const mp3Files = files.filter((file) => path.extname(file).toLowerCase() === '.mp3');
if (mp3Files.length === 0) {
console.warn('No MP3 files found in the folder.');
return;
}
console.log(`Found ${mp3Files.length} MP3 file(s) in the folder. Processing...\n`);
// Loop through each MP3 file and get its duration
for (const file of mp3Files) {
const filePath = path.join(folderPath, file); // Construct absolute file path
const duration = await getMp3Duration(filePath); // Get the duration
if (duration !== null) {
console.log(`File: ${file}`);
console.log(` Duration: ${duration.toFixed(2)} seconds\n`);
} else {
console.log(`File: ${file}`);
console.log(`Could not determine duration.\n`);
}
}
} catch (error) {
console.error('Error processing folder:', error.message);
}
}
// Main script logic
(async () => {
const folderPath = process.argv[2]; // Accept folder path from command-line arguments
if (!folderPath) {
console.error('Please provide a folder path as an argument.');
console.error('Usage: node script.js /path/to/folder');
process.exit(1);
}
// Check if the folder path exists and is a directory
if (!existsSync(folderPath) || !lstatSync(folderPath).isDirectory()) {
console.error('The provided path is not a valid directory.');
process.exit(1);
}
// Process the folder to get MP3 durations
await processFolder(folderPath);
})();