-
Notifications
You must be signed in to change notification settings - Fork 0
/
createVideo.ts
58 lines (49 loc) · 1.52 KB
/
createVideo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import stream from "stream";
import ffmpeg from "fluent-ffmpeg";
import path from 'path';
import fs from 'fs';
// Real hacky chat gpt video merger (refactor for scale)
export const createVideoFromBuffers = async (buffers: Buffer[]) => {
try {
// Create an array to store the paths of temporary image files
const tempFiles = [];
// Create a text file for FFmpeg's concat demuxer
const concatFilePath = path.join(__dirname, 'concat.txt');
const concatFile = fs.createWriteStream(concatFilePath);
// Convert Base64 strings to image files and write to the concat file
for (let i = 0; i < buffers.length; i++) {
const buffer = buffers[i];
const tempFilePath = path.join(__dirname, `temp_image_${i}.png`);
fs.writeFileSync(tempFilePath, buffer);
tempFiles.push(tempFilePath);
concatFile.write(`file '${tempFilePath}'\n`);
}
// Close the concat file
concatFile.end();
const time = String(new Date().getUTCDate())
// Run FFmpeg command to create a video
await new Promise((resolve, reject) => {
ffmpeg()
.input(concatFilePath)
.inputOptions([
'-f', 'concat',
'-safe', '0'
])
.videoCodec('libx264')
.outputOptions([
'-framerate', '1'
])
.output(`output_video${time}.mp4`)
.on('end', resolve)
.on('error', reject)
.run();
});
// Delete temporary image files and the concat file
for (const tempFilePath of tempFiles) {
fs.unlinkSync(tempFilePath);
}
fs.unlinkSync(concatFilePath);
} catch (err) {
console.log('Error creating video:', err);
}
};