video-optimizer-bot / src /ffmpeg /splitter.js
Hugging Face Deployer
Deploy bot to Hugging Face Spaces
03b405b
Raw History Blame Contribute Delete
1.17 kB
const path = require("node:path");
const { copySegment } = require("./ffmpeg");
function planSegments(duration, splitSeconds) {
if (duration <= splitSeconds + 20) return [{ index: 1, start: 0, duration }];
const segments = [];
let start = 0;
let index = 1;
while (start < duration) {
const remaining = duration - start;
const length = remaining <= splitSeconds + 20 ? remaining : splitSeconds;
segments.push({ index, start, duration: length });
start += length;
index += 1;
}
return segments;
}
async function splitIfNeeded(inputPath, outDir, info, plan) {
const segments = planSegments(info.duration, plan.splitSeconds);
if (segments.length === 1) return [{ ...segments[0], path: inputPath, copied: false }];
const width = String(segments.length).length;
const result = [];
for (const segment of segments) {
const output = path.join(outDir, `source_part_${String(segment.index).padStart(width, "0")}.mp4`);
await copySegment(inputPath, output, segment.start, segment.duration);
result.push({ ...segment, path: output, copied: true });
}
return result;
}
module.exports = { planSegments, splitIfNeeded };