-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.js
127 lines (91 loc) · 2.76 KB
/
export.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
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
let muxer
var mp4Data
var videoEncoder
const exportdata = async ()=>{
document.getElementById("Spinner").style.display = "block"
document.getElementById("spinner1").style.display = "block"
document.getElementById("status").innerHTML = "Encoding in Progress it may take some time";
muxer = new Mp4Muxer.Muxer({
target: new Mp4Muxer.ArrayBufferTarget(),
video: {
codec: 'avc',
width: videoinfo.width,
height: videoinfo.height,
frameRate:25,
duration:30000,
bitrate:110000,
timestamp:1000/30
},
audio: {
codec: 'aac',
sampleRate: bufferarray.sampleRate,
numberOfChannels: 1
},
// Puts metadata to the start of the file. Since we're using ArrayBufferTarget anyway, this makes no difference
// to memory footprint.
fastStart: 'in-memory',
// Because we're directly pumping a MediaStreamTrack's data into it, which doesn't start at timestamp = 0
firstTimestampBehavior: 'offset'
});
videoEncoder = new VideoEncoder({
output: (chunk, meta) => muxer.addVideoChunk(chunk, meta),
error: e => console.error(e)
});
videoEncoder.configure({
codec: 'avc1.42001f',
width: videoinfo.width,
height: videoinfo.height,
frameRate:25,
duration:30000,
bitrate:110000,
timestamp:1000/30
});
for (i=0;i<videodata.length;i++){
const init = {
timestamp: i*1000000/30,
codedWidth: videoinfo.width,
codedHeight: videoinfo.height,
format: "NV12",
duration:1000000/30
};
var videoframe = new VideoFrame(videodata[i], init)
videoEncoder.encode(videoframe)
videoframe.close();
}
var audioEncoder = new AudioEncoder({
output: (chunk, meta) => muxer.addAudioChunk(chunk, meta),
error: e => console.error(e)
});
audioEncoder.configure({
codec: 'mp4a.40.2',
numberOfChannels: 1,
sampleRate: audiocontext.sampleRate,
bitrate: 128000
});
var init1 = {
format: "f32",
sampleRate: audiocontext.sampleRate,
data: bufferarray.getChannelData(0),
numberOfChannels:1,
numberOfFrames: bufferarray.sampleRate*bufferarray.duration,
timestamp:1*bufferarray.duration*100000
}
var audiodata = new AudioData(init1)
audioEncoder.encode(audiodata)
await videoEncoder.flush();
await audioEncoder.flush();
muxer.finalize();
document.getElementById("Spinner").style.display = "none"
document.getElementById("spinner1").style.display = "none"
document.getElementById("status").innerHTML = "";
mp4Data = muxer.target.buffer;
const blob = new Blob([mp4Data], {type:'video/mp4', codec: 'avc'});
let url = window.URL.createObjectURL(blob);
let a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'amooiz.mp4';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
}