|
import path from "node:path" |
|
|
|
import { v4 as uuidv4 } from "uuid" |
|
import tmpDir from "temp-dir" |
|
import ffmpeg from "fluent-ffmpeg" |
|
import { moveFileFromTmpToPending } from "../utils/moveFileFromTmpToPending.mts" |
|
import { pendingFilesDirFilePath } from "../config.mts" |
|
|
|
export const postInterpolation = async (fileName: string, durationMs: number, nbFrames: number, noiseAmount: number): Promise<string> => { |
|
return new Promise((resolve,reject) => { |
|
|
|
const tmpFileName = `${uuidv4()}.mp4` |
|
|
|
const filePath = path.join(pendingFilesDirFilePath, fileName) |
|
const tmpFilePath = path.join(tmpDir, tmpFileName) |
|
|
|
ffmpeg.ffprobe(filePath, function(err, metadata) { |
|
if (err) { reject(err); return; } |
|
|
|
const durationInSec = durationMs / 1000 |
|
|
|
const currentVideoDurationInSec = metadata.format.duration |
|
|
|
console.log(`currentVideoDurationInSec in sec: ${currentVideoDurationInSec}s`) |
|
|
|
console.log(`target duration in sec: ${durationInSec}s (${durationMs}ms)`) |
|
|
|
|
|
const durationRatio = currentVideoDurationInSec / durationInSec |
|
console.log(`durationRatio: ${durationRatio}`) |
|
|
|
ffmpeg(filePath) |
|
|
|
|
|
.size("1280x720") |
|
|
|
.videoFilters([ |
|
`setpts=0.5*PTS`, |
|
|
|
|
|
`noise=c0s=${noiseAmount}:c0f=t+u` |
|
]) |
|
.outputOptions([ |
|
`-r ${nbFrames}`, |
|
]) |
|
|
|
.save(tmpFilePath) |
|
.on("end", async () => { |
|
await moveFileFromTmpToPending(tmpFileName, fileName) |
|
|
|
resolve(fileName) |
|
}) |
|
.on("error", (err) => { |
|
reject(err) |
|
}) |
|
}) |
|
}) |
|
} |