Repository navigation
Expand file tree
/
Copy pathaf.js
More file actions
145 lines (122 loc) · 4.67 KB
/
Copy pathaf.js
File metadata and controls
145 lines (122 loc) · 4.67 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const path = require('path');
const ffmpegPath = require('ffmpeg-static');
const { spawnSync } = require('child_process');
const { tmpdir } = require('os');
const fs = require('fs');
const mp4box = require('mp4box');
(function () {
// args input file, output file
const inputFile = process.argv[2];
const outputFile = process.argv[3];
const maxWidth = process.argv[4] || 1080;
const type = process.argv[5] || 'h264'; // h264, h265
const gop = process.argv[6] || 5;
const crf = process.argv[7] || 28;
if (!inputFile || !outputFile) {
console.error('Usage: node af.js <input file> <output file> <max width> <type> <gop> <crf>');
process.exit(1);
}
const tmpMp4 = path.join(tmpdir(), `${Math.random().toString(36).substring(2, 15)}.mp4`);
let cv = null;
let tag = null;
if (type === 'h264') {
cv = 'libx264';
tag = 'avc1';
} else if (type === 'h265') {
cv = 'libx265';
tag = 'hvc1';
}
const ffmpeg = spawnSync(ffmpegPath, [
'-i', inputFile,
'-c:v', cv,
'-tag:v', tag,
'-vf', `scale='min(${maxWidth},iw)':-2`,
'-crf', crf,
'-map_metadata', '-1',
'-refs', '1',
'-sc_threshold', '0',
'-level:v', '5.1',
'-tune', 'fastdecode',
'-preset', 'slower',
'-profile:v', 'main',
'-pix_fmt', 'yuv420p',
'-g', gop,
'-bf', '0',
'-movflags', '+faststart',
'-an',
'-y',
tmpMp4
]);
if (ffmpeg.status !== 0) {
console.error('Failed to generate video');
console.error(ffmpeg.stderr.toString());
process.exit(1);
}
const mp4Buffer = new Uint8Array(fs.readFileSync(tmpMp4)).buffer;
mp4Buffer.fileStart = 0;
// Remove the actual file
fs.unlinkSync(tmpMp4);
const mp4boxfile = mp4box.createFile();
mp4boxfile.onReady = function (info) {
const videoTrack = info.videoTracks[0];
const trak = mp4boxfile.getTrackById(videoTrack.id);
const sampleEntry = trak.mdia.minf.stbl.stsd.entries[0];
let descriptionBase64 = null;
const codecConfigBox = sampleEntry.hvcC || sampleEntry.avcC || sampleEntry.av1C;
if (codecConfigBox) {
const stream = new mp4box.DataStream(null, 0, mp4box.DataStream.BIG_ENDIAN);
codecConfigBox.write(stream);
// First 8 bytes are the MP4 box header (4 size + 4 type), skip them
const descriptionBuffer = new Uint8Array(stream.buffer, 8);
descriptionBase64 = Buffer.from(descriptionBuffer).toString('base64');
} else {
console.error('Missing codec configuration box (expected hvcC or avcC)');
process.exit(1);
}
let offset = 0;
let databuf = new Buffer.alloc(0);
let jsonbuf = [];
let frameKey = 0;
mp4boxfile.onSamples = function (id, user, samples) {
for (const sample of samples) {
const chunkData = Buffer.from(sample.data.buffer || sample.data, sample.data.byteOffset || 0, sample.data.byteLength || sample.data.length);
databuf = Buffer.concat([databuf, chunkData]);
// fs.writeSync(binFd, chunkData);
jsonbuf.push({
o: offset,
l: chunkData.length,
t: Math.round((sample.cts / sample.timescale) * 1000000),
ty: sample.is_sync ? 'key' : 'delta',
i: frameKey
});
offset += chunkData.length;
frameKey += 1;
}
let manifest = {
codec: videoTrack.codec,
fps: videoTrack.nb_samples / (videoTrack.duration / videoTrack.timescale),
totalFrames: videoTrack.nb_samples,
frames: jsonbuf,
width: videoTrack.video.width,
height: videoTrack.video.height,
gop,
type,
description: descriptionBase64
};
// add description to the end of the data buffer
databuf = Buffer.concat([databuf, Buffer.from(JSON.stringify(manifest))]);
const footer = Buffer.alloc(4);
footer.writeUInt32LE(offset, 0);
databuf = Buffer.concat([databuf, footer]);
fs.writeFileSync(outputFile, databuf);
console.log('✅ Video generated successfully!');
};
mp4boxfile.setExtractionOptions(videoTrack.id);
mp4boxfile.start();
};
mp4boxfile.onError = function (e) {
console.error(`Error: ${e}`);
};
mp4boxfile.appendBuffer(mp4Buffer);
mp4boxfile.flush();
})();