-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoHelper.js
101 lines (90 loc) · 2.81 KB
/
videoHelper.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import ffmpeg from 'fluent-ffmpeg';
import * as fs from 'node:fs';
import path from 'path';
const audioPath = "./assets/audio";
export const getVideoMetadata = (fileName) => {
return new Promise((resolve, reject) => {
ffmpeg.ffprobe(fileName, ["-show_chapters"], (err, metadata) => {
if(err) {
reject(err);
}
resolve(metadata);
})
})
}
export const splitVideo = (fileName, splitSize = 10) => {
splitSize = Math.max(splitSize, 20);
return new Promise((resolve, reject) => {
ffmpeg(fileName)
.format('segment')
.outputOptions('-segment_time ' + splitSize)
.output(`${audioPath}/result-%d.mp3`)
.on('end', () => {
resolve();
})
.run();
});
}
export const videoExists = (file) => {
if(fs.existsSync(file)) {
return true;
}
return false;
}
export const getAudioFiles = () => {
let files = fs.readdirSync(audioPath);
let regex = /result-(\d+)\.mp3/;
files = files.filter((file) => {
return regex.test(file);
})
.sort((a,b) => {
const aNumber = parseInt(a.match(regex)[1], 10);
const bNumber = parseInt(b.match(regex)[1], 10);
return aNumber - bNumber;
})
.map((fileName) => {
return path.join(audioPath, fileName);
});
return files;
}
function secondsToSRTTime(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const remainingSeconds = seconds % 60;
const milliseconds = Math.round((remainingSeconds - Math.floor(remainingSeconds)) * 1000);
const format = (num) => String(num).padStart(2, '0');
return `${format(hours)}:${format(minutes)}:${format(Math.floor(remainingSeconds))},${String(milliseconds).padStart(3, '0')}`;
}
export const generateSRT = (entries, splitSize = 20, filePath) => {
splitSize = Math.max(splitSize, 20);
let result = '';
let index = 1;
let from = 0;
let to = splitSize;
const {dir, name} = path.parse(filePath);
for(const entry of entries) {
result += `${index}\n`;
result += `${secondsToSRTTime(from)} --> ${secondsToSRTTime(to)}\n`;
result += `${entry.text}\n\n`;
index++;
from += splitSize;
to += splitSize
}
result = result.trim();
fs.writeFile(`${dir}/${name}.srt`, result, 'utf8', function(err) {
if (err) {
throw new Error('Error saving the srt file');
}
});
}
export const cleanUpAudioFiles = () => {
let files = fs.readdirSync(audioPath);
for(const file of files) {
const filePath = path.join(audioPath, file);
fs.unlink(filePath, (err) => {
if (err) {
throw new Error("Error deleting audio file");
}
});
}
}