-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmp4box.html
161 lines (139 loc) · 4.63 KB
/
mp4box.html
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<html>
<script src="mp4box.all.js"></script>
<script>
async function reencode(file) {
const startNow = performance.now();
let decoder;
let encoder;
let track = null;
let decodedFrameIndex = 0;
let encodedFrameIndex = 0;
let nextKeyFrameTimestamp = 0;
let sampleDurations = [];
let trackID = null;
const mp4boxOutputFile = MP4Box.createFile();
const mp4boxInputFile = MP4Box.createFile();
mp4boxInputFile.onError = error => console.error(error);
mp4boxInputFile.onReady = async (info) => {
track = info.videoTracks[0];
decoder = new VideoDecoder({
async output(inputFrame) {
const bitmap = await createImageBitmap(inputFrame);
const outputFrame = new VideoFrame(bitmap, {
timestamp: inputFrame.timestamp,
});
const keyFrameEveryHowManySeconds = 2;
let keyFrame = false;
if (inputFrame.timestamp >= nextKeyFrameTimestamp) {
keyFrame = true;
nextKeyFrameTimestamp = inputFrame.timestamp + keyFrameEveryHowManySeconds * 1e6;
}
encoder.encode(outputFrame, { keyFrame });
inputFrame.close();
outputFrame.close();
decodedFrameIndex++;
displayProgress();
},
error(error) {
console.error(error);
}
});
let description;
const trak = mp4boxInputFile.getTrackById(track.id);
for (const entry of trak.mdia.minf.stbl.stsd.entries) {
if (entry.avcC || entry.hvcC) {
const stream = new DataStream(undefined, 0, DataStream.BIG_ENDIAN);
if (entry.avcC) {
entry.avcC.write(stream);
} else {
entry.hvcC.write(stream);
}
description = new Uint8Array(stream.buffer, 8); // Remove the box header.
break;
}
}
decoder.configure({
codec: track.codec,
codedWidth: track.track_width,
codedHeight: track.track_height,
hardwareAcceleration: 'prefer-hardware',
description,
});
encoder = new VideoEncoder({
output(chunk, metadata) {
let uint8 = new Uint8Array(chunk.byteLength);
chunk.copyTo(uint8);
const timescale = 90000;
if (trackID === null) {
const description = metadata.decoderConfig.description;
trackID = mp4boxOutputFile.addTrack({
width: track.track_width,
height: track.track_height,
timescale,
avcDecoderConfigRecord: description,
});
}
const sampleDuration = sampleDurations.shift() / (1_000_000 / timescale);
mp4boxOutputFile.addSample(trackID, uint8, {
duration: sampleDuration,
is_sync: chunk.type === 'key',
});
encodedFrameIndex++;
displayProgress();
},
error(error) {
console.error(error);
}
});
encoder.configure({
codec: 'avc1.4d0034',
width: track.track_width,
height: track.track_height,
hardwareAcceleration: 'prefer-hardware',
bitrate: 14_000_000,
alpha: 'discard',
bitrateMode: 'variable',
latencyMode: 'realtime',
});
mp4boxInputFile.setExtractionOptions(track.id, null, {nbSamples: Infinity});
mp4boxInputFile.start();
};
function displayProgress() {
progress.innerText =
"Decoding frame " + decodedFrameIndex + " (" + Math.round(100 * decodedFrameIndex / track.nb_samples) + "%)\n" +
"Encoding frame " + encodedFrameIndex + " (" + Math.round(100 * encodedFrameIndex / track.nb_samples) + "%)\n";
}
mp4boxInputFile.onSamples = async (track_id, ref, samples) => {
for (const sample of samples) {
sampleDurations.push(sample.duration * 1_000_000 / sample.timescale);
decoder.decode(new EncodedVideoChunk({
type: sample.is_sync ? "key" : "delta",
timestamp: sample.cts * 1_000_000 / sample.timescale,
duration: sample.duration * 1_000_000 / sample.timescale,
data: sample.data
}));
}
await decoder.flush();
await encoder.flush();
encoder.close();
decoder.close();
mp4boxOutputFile.save("mp4box.mp4");
const seconds = (performance.now() - startNow) / 1000;
progress.innerText =
"Re-encoded " + encodedFrameIndex + " frames in " + (Math.round(seconds * 100) / 100) + "s at " +
Math.round(encodedFrameIndex / seconds) + " fps";
};
var reader = new FileReader();
reader.onload = function() {
this.result.fileStart = 0;
mp4boxInputFile.appendBuffer(this.result);
mp4boxInputFile.flush();
};
reader.readAsArrayBuffer(file);
}
</script>
<p>
Select a video to re-encode:
<input type="file" onchange="reencode(event.target.files[0])"></input>
<div id="progress"></div>
</p>