-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (49 loc) · 2.27 KB
/
index.js
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
const fs = require("fs");
const sharp = require("sharp");
const ffmpeg = require("fluent-ffmpeg");
const videoPath = "input.mp4"; //mp4のファイルパス
const framesDir = "frames"; //リサイズしたpngファイルが入るディレクトリ名
const framesDataDir = "frames_data"; //出来たフレームデータが入るディレクトリ名
const frameRate = 30; //フレームレート(30fpsでしか動作確認してないから他は知らん)
if (!fs.existsSync(framesDir)) fs.mkdirSync(framesDir);
if (!fs.existsSync(framesDataDir)) fs.mkdirSync(framesDataDir);
async function processFrame(imagePath, frameNumber) {
try {
const frameDataPath = `${framesDataDir}/frame_${frameNumber}.txt`;
const { data, info } = await sharp(imagePath)
.resize(33, 25) //サイズ(drawing.luaの縦横サイズと同じにする)
.raw()
.toBuffer({ resolveWithObject: true });
let frameData = "";
for (let y = 0; y < info.height; y++) {
for (let x = 0; x < info.width; x++) {
const pixelIndex = (y * info.width + x) * info.channels;
const brightness =
0.2126 * data[pixelIndex] +
0.7152 * data[pixelIndex + 1] +
0.0722 * data[pixelIndex + 2];
frameData += brightness > 128 ? "0" : "1";
}
frameData += "\n";
}
fs.writeFileSync(frameDataPath, frameData);
console.log(`フレーム${frameNumber}を処理`);
} catch (error) {
console.error(`フレーム処理エラー${frameNumber}:`, error);
}
}
ffmpeg(videoPath)
.output(`${framesDir}/frame_%04d.png`)
.outputOptions(["-vf", `fps=${frameRate}`])
.on("end", async () => {
console.log("フレームの抽出が完了しました!データを生成します");
const frameFiles = fs.readdirSync(framesDir).sort();
await Promise.all(
frameFiles.map((file, i) => processFrame(`${framesDir}/${file}`, i + 1))
);
console.log("すべてのフレームデータが生成されました!");
})
.on("error", (err) => {
console.error("FFmpeg エラー:", err);
})
.run();