birdgen/src/parseSceneFrames.js
2022-06-28 21:05:56 -05:00

32 lines
1.3 KiB
JavaScript

import exec from './exec.js'
import addExtraVideos from './addExtraVideos.js'
import getArgument from './getArgument.js'
const chaos = Math.max(Math.min(getArgument('chaos')??50, 100), 0)
async function getSceneTimestamps(inputPath) {
const durationExec = await exec(`ffprobe -i ${inputPath} -v quiet -show_entries format=duration -hide_banner -of default=noprint_wrappers=1:nokey=1`)
const frameTimesExec = await exec(`ffprobe -show_frames -of compact=p=0 -f lavfi "movie=${inputPath},select=gt(scene\\,${Math.ceil(1000*(1.001-(chaos/100)))/1000})"`, {
maxBuffer: 5 * 1024 * 1024
})
let times = frameTimesExec.stdout.match(/pts_time=[0-9]+\.?([0-9]?)+/gm).map(e => Math.round(parseFloat(e.split('=').pop())*1000))
times.push(Math.floor(parseFloat(durationExec.stdout.trim())*1000))
return times
}
export default async function parseSceneFrames(inputPath) {
let sections = (await getSceneTimestamps(inputPath)).map((e, i, arr) => {
return {
start: e,
end: arr[i+1]
}
})
sections.pop()
console.log(sections)
console.log(`Created ${sections.length} clips`)
sections = await addExtraVideos(sections)
return {
duration: sections[sections.length-1].end/1000,
sections
}
}