65 lines
2.5 KiB
JavaScript
65 lines
2.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.generateSRT = generateSRT;
|
|
exports.generateVTT = generateVTT;
|
|
function formatSrtTime(seconds) {
|
|
const h = Math.floor(seconds / 3600);
|
|
const m = Math.floor((seconds % 3600) / 60);
|
|
const s = Math.floor(seconds % 60);
|
|
const ms = Math.floor((seconds % 1) * 1000);
|
|
return `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')},${ms.toString().padStart(3, '0')}`;
|
|
}
|
|
function formatVttTime(seconds) {
|
|
const h = Math.floor(seconds / 3600);
|
|
const m = Math.floor((seconds % 3600) / 60);
|
|
const s = Math.floor(seconds % 60);
|
|
const ms = Math.floor((seconds % 1) * 1000);
|
|
return `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}.${ms.toString().padStart(3, '0')}`;
|
|
}
|
|
function cleanDescription(text) {
|
|
return text.replace(/\n+/g, ' ').replace(/\s+/g, ' ').trim();
|
|
}
|
|
function generateSRT(segments, videoDuration) {
|
|
if (segments.length === 0)
|
|
return '';
|
|
const sorted = [...segments].sort((a, b) => a.startTime - b.startTime);
|
|
const lines = [];
|
|
for (let i = 0; i < sorted.length; i++) {
|
|
const seg = sorted[i];
|
|
const startTime = seg.startTime;
|
|
let endTime;
|
|
if (i < sorted.length - 1) {
|
|
endTime = sorted[i + 1].startTime;
|
|
}
|
|
else {
|
|
endTime = Math.min(seg.startTime + seg.duration + 0.5, videoDuration);
|
|
}
|
|
lines.push((i + 1).toString());
|
|
lines.push(`${formatSrtTime(startTime)} --> ${formatSrtTime(endTime)}`);
|
|
lines.push(cleanDescription(seg.description));
|
|
lines.push('');
|
|
}
|
|
return lines.join('\n');
|
|
}
|
|
function generateVTT(segments, videoDuration) {
|
|
if (segments.length === 0)
|
|
return '';
|
|
const sorted = [...segments].sort((a, b) => a.startTime - b.startTime);
|
|
const lines = ['WEBVTT', ''];
|
|
for (let i = 0; i < sorted.length; i++) {
|
|
const seg = sorted[i];
|
|
const startTime = seg.startTime;
|
|
let endTime;
|
|
if (i < sorted.length - 1) {
|
|
endTime = sorted[i + 1].startTime;
|
|
}
|
|
else {
|
|
endTime = Math.min(seg.startTime + seg.duration + 0.5, videoDuration);
|
|
}
|
|
lines.push(`${formatVttTime(startTime)} --> ${formatVttTime(endTime)}`);
|
|
lines.push(cleanDescription(seg.description));
|
|
lines.push('');
|
|
}
|
|
return lines.join('\n');
|
|
}
|
|
//# sourceMappingURL=subtitleGenerator.js.map
|