-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
195 lines (159 loc) · 6.44 KB
/
script.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
*
* @package media-analyzer
* @version Release: 1.0.0
* @license GPL3
* @author Ali YILMAZ <[email protected]>
* @category Media, Analyzer, Javascript
* @link https://github.com/aliyilmaz/media-analyzer
*
*/
async function getAudioDuration(audio)
{
let audioTag = document.createElement('audio');
audioTag.src = URL.createObjectURL(audio);
await getPromiseFromEvent(audioTag, 'loadedmetadata')
return audioTag.duration
}
function getPromiseFromEvent(item, event) {
return new Promise((resolve) => {
const listener = () => {
item.removeEventListener(event, listener)
// console.log(item.duration);
resolve()
}
item.addEventListener(event, listener)
})
}
function get_file_info(mediainfo, file) {
let getSize = () => file.size
let readChunk = (chunkSize, offset) =>
new Promise((resolve, reject) => {
let reader = new FileReader()
reader.onload = (event) => {
if (event.target.error) {
reject(event.target.error)
}
resolve(new Uint8Array(event.target.result))
}
reader.readAsArrayBuffer(file.slice(offset, offset + chunkSize))
})
return mediainfo
.analyzeData(getSize, readChunk)
.then( async (result) => {
MediaInfoOutput = JSON.parse(result);
delete MediaInfoOutput['creatingLibrary']; // Thanks to mediainfo.js Library contributors https://mediaarea.net/en/MediaInfo
// extra-codec
if (MediaInfoOutput['media']['track'][1] != undefined) {
// SUPPORT AAC
if ((MediaInfoOutput['media']['track'][1]['@type'] === 'Audio' && MediaInfoOutput['media']['track'][1]['Format'] === 'AAC')){
MediaInfoOutput.duration = await getAudioDuration(file); // Thanks https://stackoverflow.com/a/71216199/10515363
}
// SUPPORT AAC
// OTHER AUDIO
if ((MediaInfoOutput['media']['track'][1]['@type'] === 'Audio' && MediaInfoOutput['media']['track'][1]['Format'] !== 'AAC')){
MediaInfoOutput.duration = MediaInfoOutput['media']['track'][1]['Duration'];
}
// OTHER AUDIO
}
// DETERMINING VIDEO DURATION
if(MediaInfoOutput['media']['track'][1] != undefined){
if ((MediaInfoOutput['media']['track'][1]['@type'] === 'Video' && MediaInfoOutput['media']['track'][2]['@type'] === 'Audio')){
let Media1Duration = MediaInfoOutput['media']['track'][1]['Duration'],
Media2Duration = MediaInfoOutput['media']['track'][2]['Duration'];
if(Media1Duration > Media2Duration){
MediaInfoOutput.duration = Media1Duration;
} else {
MediaInfoOutput.duration = Media2Duration;
}
}
}
// DETERMINING VIDEO DURATION
// EXTRA INFO
MediaInfoOutput.filename = file.name;
MediaInfoOutput.mime_type = (file.type == undefined) ? undefined : file.type;
// EXTRA INFO
// Fix for audio file mime type issue in mobile versions of browsers
// mp3
if(MediaInfoOutput.mime_type == undefined || MediaInfoOutput.mime_type == ''){
MediaInfoOutput.mime_type = (MediaInfoOutput['media']['track'][0]['Format'] == 'MPEG Audio' && MediaInfoOutput['media']['track'][1]['Format'] == 'MPEG Audio') ? 'audio/mpeg' : undefined;
MediaInfoOutput.filename = file.name+'.mp3';
}
// aac
if(MediaInfoOutput.mime_type == undefined || MediaInfoOutput.mime_type == ''){
MediaInfoOutput.mime_type = (MediaInfoOutput['media']['track'][0]['Format'] == 'ADTS' && MediaInfoOutput['media']['track'][1]['Format'] == 'AAC') ? 'audio/aac' : undefined;
MediaInfoOutput.filename = file.name+'.aac';
}
// wav
if(MediaInfoOutput.mime_type == undefined || MediaInfoOutput.mime_type == ''){
MediaInfoOutput.mime_type = (MediaInfoOutput['media']['track'][0]['Format'] == 'Wave' && MediaInfoOutput['media']['track'][1]['Format'] == 'PCM') ? 'audio/wav' : undefined;
MediaInfoOutput.filename = file.name+'.wav';
}
// flac
if(MediaInfoOutput.mime_type == undefined || MediaInfoOutput.mime_type == ''){
MediaInfoOutput.mime_type = (MediaInfoOutput['media']['track'][0]['Format'] == 'FLAC' && MediaInfoOutput['media']['track'][1]['Format'] == 'FLAC') ? 'audio/flac' : undefined;
MediaInfoOutput.filename = file.name+'.flac';
}
// m4a
if(MediaInfoOutput.mime_type == undefined || MediaInfoOutput.mime_type == ''){
MediaInfoOutput.mime_type = (MediaInfoOutput['media']['track'][0]['Format'] == 'MPEG-4' && MediaInfoOutput['media']['track'][1]['Format'] == 'AAC') ? 'audio/x-m4a' : undefined;
MediaInfoOutput.filename = file.name+'.m4a';
}
return MediaInfoOutput;
// extra-codec
})
.catch((error) => {
console.log('An error occured:\n'+error.stack);
})
}
async function onChangeFile(e, mediainfo) {
let file,
result = [],
numb = 4000,
originalStyle = e.target.style;
let intervalId = setInterval(() => {
e.target.style.borderBottom = '8px solid';
e.target.style.borderImage = 'linear-gradient(' + numb * 10 + 'deg, turquoise, greenyellow) 1';
numb = numb-200;
}, 170);
if (e.target.files.length > 1) {
for (let i = 0; i < e.target.files.length; i++) {
file = e.target.files[i];
if (file) {
result[i] = await get_file_info(mediainfo, file);
if (i + 1 == e.target.files.length) {
e.target.style = originalStyle;
clearInterval(intervalId);
}
}
}
} else {
file = e.target.files[0];
if (file) {
result[0] = await get_file_info(mediainfo, file);
}
e.target.style = originalStyle;
clearInterval(intervalId);
}
return result;
}
function media_analyzer(inputElement, callback) {
let fileinput = document.querySelector(inputElement);
MediaInfo({ format: 'JSON' }, (mediainfo) => {
fileinput.addEventListener('change', async function(e) {
response = await onChangeFile(e, mediainfo);
if (callback) callback(e, response);
});
});
}
function truncate(str, n){
return (str.length > n) ? str.substr(0, n-1) + '…' : str;
};
function basename(prevname) {
return prevname.replace(/^(.*[/\\])?/, '').replace(/(\.[^.]*)$/, '');
}
var stringToHTML = function (str) {
var parser = new DOMParser();
var doc = parser.parseFromString(str, 'text/html');
return doc.body;
};