forked from gpac/mp4box.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-segmenter.html
76 lines (69 loc) · 2.96 KB
/
file-segmenter.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
<input id="fileInput" type="file"><br/>
<input id="keepMdatInput" type="checkbox">Keep mdat data</input><br/>
<input id="doSegmentation" type="checkbox" checked>Perform Segmentation</input><br/>
<input id="doReleaseSamples" type="checkbox" checked>Release Samples after Segmentation</input><br/>
Number of samples per segment or per extraction: <input id="nbSamples" type="range" value=1000 min=1 max=10000 onchange="this.nextElementSibling.value = this.value" ></input><output>1000</output><br/>
<button onclick="load()">Load</button>
<div >File Size:
<p id='file_size'></p>
</div>
<div>Memory usage:
<p id='memory_usage'></p>
</div>
<script src="../dist/mp4box.all.min.js"></script>
<script>
var mp4 = null;
function updateMemory() {
document.getElementById('memory_usage').innerText = "jsHeapSizeLimit: "+Math.round(window.performance.memory.jsHeapSizeLimit/(1024*1024))+"MB\n"+
"totalJSHeapSize: "+Math.round(window.performance.memory.totalJSHeapSize/(1024*1024))+"MB\n"+
"usedJSHeapSize: "+Math.round(window.performance.memory.usedJSHeapSize/(1024*1024))+"MB";
}
updateMemory();
function load() {
Log.setLogLevel(Log.info);
console.log("Creating MP4Box file with parameter: "+document.getElementById('keepMdatInput').checked);
mp4 = MP4Box.createFile(document.getElementById('keepMdatInput').checked);
mp4.onError = function(e) { console.log(e); };
mp4.onReady = function(info) {
if (document.getElementById('doSegmentation').checked) {
console.log("Sample batch size: "+(+document.getElementById('nbSamples').value));
for (var i = 0; i < info.tracks.length; i++) {
var track = info.tracks[i];
mp4.setSegmentOptions(info.tracks[i].id, null, { nbSamples: +document.getElementById('nbSamples').value } );
}
mp4.initializeSegmentation();
}
mp4.start();
};
updateMemory();
mp4.onSegment = function (id, user, buffer, sampleNum, is_last) {
console.log("Received"+(is_last?" last":"")+" segment on track "+id+" with sample up to "+sampleNum);
if (document.getElementById('doReleaseSamples').checked) {
mp4.releaseUsedSamples(id, sampleNum);
}
if (is_last) {
updateMemory();
}
}
var offset = 0;
var file = document.getElementById('fileInput').files[0];
if (file) {
var reader = file.stream().getReader();
reader.read().then(function getNextChunk({done, value}) {
if (done) {
mp4.flush();
console.log("done");
document.getElementById('file_size').innerText = Math.round(offset/(1024*1024))+"MB";
updateMemory();
// mp4 = null;
return;
}
var copy = value.buffer;
copy.fileStart = offset;
offset += value.length;
mp4.appendBuffer(copy);
return reader.read().then(getNextChunk);
});
}
}
</script>