-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.transpose.js
31 lines (30 loc) · 1.89 KB
/
dev.transpose.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
const fs = require('fs');
const TRAKTKHDSIGN = Buffer.from([0x74, 0x72, 0x61, 0x6B, 0x00, 0x00, 0x00, 0x5C, 0x74, 0x6B, 0x68, 0x64]);
const LASTSIGN = Buffer.from([0x40]);
const VIDESIGN = Buffer.from([0x76, 0x69, 0x64, 0x65]);
async function transpose(rotate) {
const rotateMartix = {
"90": Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40]),
"180": Buffer.from([0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40]),
"270": Buffer.from([0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40]),
"0": Buffer.from([0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40]),
}[rotate];
const mp4Buffer = await fs.promises.readFile('./demo.mp4');
mp4Buffer.reverse();
const r_videIndex = mp4Buffer.indexOf(VIDESIGN.reverse());
const r_tkhdIndex = mp4Buffer.indexOf(TRAKTKHDSIGN.reverse(), r_videIndex);
const tkhdIndex = mp4Buffer.length - r_tkhdIndex - TRAKTKHDSIGN.length;
mp4Buffer.reverse();
const lastIndex = mp4Buffer.indexOf(LASTSIGN, tkhdIndex);
const martixBegin = lastIndex - 32;
const martixEnd = lastIndex + 1;
const newMp4 = Buffer.concat([
mp4Buffer.slice(0, martixBegin),
rotateMartix,
mp4Buffer.slice(martixEnd),
]);
return newMp4;
}
transpose('0').then(value => {
fs.promises.writeFile('./output.mp4', value);
})