35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import * as fs from "fs";
|
|
import * as path from "path";
|
|
|
|
function toSrtTime(secondsFloat: number): string {
|
|
const totalMs = Math.max(0, Math.round(secondsFloat * 1000));
|
|
const hours = Math.floor(totalMs / 3600000);
|
|
const minutes = Math.floor((totalMs % 3600000) / 60000);
|
|
const seconds = Math.floor((totalMs % 60000) / 1000);
|
|
const ms = totalMs % 1000;
|
|
const pad = (n: number, w: number) => n.toString().padStart(w, "0");
|
|
return `${pad(hours, 2)}:${pad(minutes, 2)}:${pad(seconds, 2)},${pad(ms, 3)}`;
|
|
}
|
|
|
|
export function createSrt(storyName: string, chunks: string[], chunkDurations: number[]): string {
|
|
const srtPath = path.resolve("stories", storyName, "subtitles.srt");
|
|
let srtContent = "";
|
|
let currentTime = 0;
|
|
|
|
for (let i = 0; i < chunks.length; i++) {
|
|
const chunk = chunks[i];
|
|
const duration = chunkDurations[i];
|
|
const startTime = toSrtTime(currentTime);
|
|
const endTime = toSrtTime(currentTime + duration);
|
|
|
|
srtContent += `${i + 1}\n`;
|
|
srtContent += `${startTime} --> ${endTime}\n`;
|
|
srtContent += `${chunk}\n\n`;
|
|
|
|
currentTime += duration;
|
|
}
|
|
|
|
fs.writeFileSync(srtPath, srtContent);
|
|
return srtPath;
|
|
}
|