-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutmp3.js
53 lines (47 loc) · 1.66 KB
/
cutmp3.js
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
const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs');
const moment = require('moment');
const os = require('os');
const path = require('path');
const sourceAudioPath = process.argv[2];
if (!sourceAudioPath) {
console.error('Please provide the absolute path of the audio file as a command line argument.');
process.exit(1);
}
if (!path.isAbsolute(sourceAudioPath)) {
console.error('Please provide an absolute path for the audio file.');
process.exit(1);
}
const targetSongNamesPrefix = path.basename(sourceAudioPath, path.extname(sourceAudioPath));
const timeTable = path.join(path.dirname(sourceAudioPath), 'timetable.txt');
const targetDirPath = path.join(path.dirname(sourceAudioPath), 'splitted-songs');
if (!fs.existsSync(targetDirPath)) {
fs.mkdirSync(targetDirPath, { recursive: true });
}
function durationToSeconds(start) {
return moment.duration(start).asSeconds();
}
const songs = fs.readFileSync(timeTable, 'utf-8').split('\n');
let songNumber = 0;
for (const song of songs) {
let [start, end, name] = song.trim().split(',');
const secondsStart = durationToSeconds(start);
const secondsEnd = durationToSeconds(end);
const duration = secondsEnd - secondsStart;
if (!name) {
name = 'Untitled';
}
if (!duration) {
break;
}
console.log(`Start processing ${name}...`);
const outputPath = `${targetDirPath}/${targetSongNamesPrefix}_${++songNumber}-${name}.mp3`;
ffmpeg(sourceAudioPath)
.seekInput(secondsStart)
.duration(duration)
.output(outputPath)
.on('end', function () {
console.log(`Finished processing ${name}.`);
})
.run();
}