forked from jaruba/PowderWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowserSupport.js
More file actions
393 lines (318 loc) · 9.81 KB
/
Copy pathbrowserSupport.js
File metadata and controls
393 lines (318 loc) · 9.81 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import _ from 'lodash'
import detectBrowser from 'utils/detectBrowser'
const isSafari = detectBrowser.isSafari;
const meta2container = {
video: {
'mov,mp4,m4a,3gp,3g2,mj2': 'mp4',
'matroska,webm': 'webm',
'ogg': 'ogv',
},
audio: {
'mov,mp4,m4a,3gp,3g2,mj2': 'mp4',
'ogg': 'oga',
'mp3': 'mp3',
}
}
const codec2support = {
// video:
'h264': 'avc1.42E01E',
'x264': 'avc1.42E01E',
'mpeg4': 'mp4v.20.8',
'vp8': 'vp8',
'vp9': 'vp9',
'theora': 'theora',
// audio:
'aac': 'mp4a.40.2',
'vorbis': 'vorbis',
'mp3': 'mp3'
}
const contentTypes = {
video: {
'mp4': 'video/mp4',
'webm': 'video/webm',
'ogv': 'video/ogg',
'm3u8': 'application/vnd.apple.mpegurl'
},
audio: {
'mp3': 'audio/mpeg',
'oga': 'audio/ogg',
'mp4': 'audio/mp4'
}
}
const containerCodecs = {
video: {
'mp4': {
video: ['h264', 'x264', 'mpeg4'],
audio: ['aac']
},
'webm': {
video: ['vp8', 'vp9'],
audio: ['vorbis']
},
'ogv': {
video: ['theora'],
audio: ['vorbis']
},
'm3u8': {
video: ['h264', 'x264'],
audio: ['mp3']
},
},
audio: {
'mp3': {
audio: ['mp3']
},
'mp4': {
audio: ['aac']
},
'oga': {
audio: ['vorbis']
}
}
}
const buildSupportTag = (contentType, vCodec, aCodec) => {
return contentType + '; codecs="' + (vCodec ? codec2support[vCodec] : '') + (aCodec ? (vCodec ? ', ' : '') + codec2support[aCodec] : '') + '"'
}
const browserSupport = (video) => {
if ( !video.canPlayType ) return false
const supp = { video: {}, audio: {} }
const validateCodecs = (type, container, vCodec, aCodec) => {
console.log(buildSupportTag(container, vCodec, aCodec))
if (!!video.canPlayType( buildSupportTag(contentTypes[type][container], vCodec, aCodec) )) {
if (!isSafari && container == 'm3u8')
return
if (!supp[type][container])
supp[type][container] = []
const codecCombo = {}
if (vCodec)
codecCombo.video = vCodec
if (aCodec)
codecCombo.audio = aCodec
supp[type][container].push(codecCombo)
}
return
}
const iterateAudio = (type, container, vCodec, audios) => {
audios.forEach( aCodec => {
validateCodecs(type, container, vCodec, aCodec)
})
return
}
_.each(containerCodecs, (el, type) => {
_.each(el, (elm, container) => {
if (elm.video) {
elm.video.forEach( vCodec => {
iterateAudio(type, container, vCodec, elm.audio)
})
} else
iterateAudio(type, container, null, elm.audio)
})
})
if (isSafari) {
// overwrite video container support
supp.video = {
'm3u8': [ { video: containerCodecs.video.m3u8.video[0], audio: containerCodecs.video.m3u8.audio[0] } ]
}
}
return supp
}
let supported = false
export default {
contentTypes,
containerCodecs,
isAudio: (meta) => {
let audioFile
if (meta.streams && meta.streams.length) {
let hasAudio
let hasVideo
meta.streams.some(stream => {
if (stream.codec_type) {
if (stream.codec_type == 'video') {
hasVideo = true
} else if (stream.codec_type == 'audio') {
hasAudio = true
}
if (hasVideo && hasAudio)
return true
}
})
audioFile = !!(hasAudio && !hasVideo)
}
return audioFile
},
init: (video) => {
if (video && !supported)
supported = browserSupport(video)
// opera doesn't like our mp4 transcodes sometimes (just sometimes though)
// so we remove mp4 from supported for it completely to guarantee playback
if (detectBrowser.isOpera && supported && supported.video && supported.video.mp4)
delete supported.video.mp4
console.log('supported')
console.log(supported)
},
isSupported: (meta, type, forceAll) => {
if (forceAll || !supported || !_.size(supported[type || 'video'])) {
return {
container: 'all',
needsAudio: -1,
needsVideo: -1,
maxHeight: 0,
maxWidth: 0,
contentType: 'all',
audio: '',
video: '',
forAudio: -1,
audioCount: 1,
actualHeight: 0,
actualWidth: 0,
audioDelay: 0,
}
} else {
const codecConfig = { container: 'all', maxHeight: 0, maxWidth: 0, needsAudio: -1, needsVideo: -1, audio: '', video: '', forAudio: -1, actualHeight: 0, actualWidth: 0, audioDelay: 0 }
if (meta && meta.format && meta.format.format_name) {
const fileEncodings = { video: [], audio: [] }
if (meta.streams && meta.streams.length) {
meta.streams.forEach(stream => {
if (stream.codec_type) {
if (stream.codec_type == 'video') {
fileEncodings.video.push(stream.codec_name)
} else if (stream.codec_type == 'audio') {
fileEncodings.audio.push(stream.codec_name)
}
}
})
type = fileEncodings.audio.length && !fileEncodings.video.length ? 'audio' : fileEncodings.video.length ? 'video' : type
}
codecConfig.audioCount = fileEncodings.audio.length
type = type || 'video'
// check supported
let origContainer = meta2container[type][meta.format.format_name]
console.log('orig container: ' + origContainer)
// if (origContainer && supported.video[origContainer]) {
// ^ this was wrong because a browser does not need to support the container in order to support the video/audio codecs
if (origContainer) {
if (meta.streams && meta.streams.length) {
const supportsAll = _.some(supported[type], (encodings, container) => {
const supportsAll = encodings.some(encoding => {
if (type == 'video') {
if (fileEncodings.video.indexOf(encoding.video) > -1) {
if (fileEncodings.audio.indexOf(encoding.audio) > -1) {
codecConfig.needsVideo = fileEncodings.video.indexOf(encoding.video)
codecConfig.needsAudio = fileEncodings.audio.indexOf(encoding.audio)
codecConfig.video = encoding.video
codecConfig.audio = encoding.audio
return true
}
}
} else if (type == 'audio') {
if (fileEncodings.audio.indexOf(encoding.audio) > -1) {
codecConfig.needsAudio = fileEncodings.audio.indexOf(encoding.audio)
codecConfig.audio = encoding.audio
return true
}
}
})
if (supportsAll) {
origContainer = container
return true
}
})
console.log('support all: ' + supportsAll)
if (type == 'video') {
if (!supportsAll)
_.some(supported[type], (encodings, container) => {
return encodings.some(encoding => {
// find any valid encoding that we could use
if (codecConfig.needsVideo == -1 && fileEncodings.video.indexOf(encoding.video) > -1) {
codecConfig.needsVideo = fileEncodings.video.indexOf(encoding.video)
}
if (codecConfig.needsAudio == -1 && fileEncodings.audio.indexOf(encoding.audio) > -1) {
codecConfig.needsAudio = fileEncodings.audio.indexOf(encoding.audio)
}
if (codecConfig.needsVideo > -1 || codecConfig.needsAudio > -1) {
codecConfig.video = encoding.video
codecConfig.audio = encoding.audio
console.log('set container to: '+ container)
origContainer = container
return true
}
})
})
let vdId = -1
meta.streams.some(stream => {
if (stream.codec_type && stream.codec_type == 'video') {
if (codecConfig.needsVideo > -1) {
vdId++
if (vdId == codecConfig.needsVideo) {
codecConfig.maxHeight = stream.height || 0
codecConfig.maxWidth = stream.width || 0
return true
}
} else {
codecConfig.maxHeight = stream.height > codecConfig.maxHeight ? stream.height : codecConfig.maxHeight
codecConfig.maxWidth = stream.width > codecConfig.maxWidth ? stream.width : codecConfig.maxWidth
}
}
})
} else if (type == 'audio') {
if (!supportsAll)
_.some(supported[type], (encodings, container) => {
return encodings.some(encoding => {
// find any valid encoding that we could use
if (codecConfig.needsAudio == -1 && fileEncodings.audio.indexOf(encoding.audio) > -1) {
codecConfig.needsAudio = fileEncodings.audio.indexOf(encoding.audio)
}
if (codecConfig.needsAudio > -1) {
codecConfig.audio = encoding.audio
console.log('set container to: '+ container)
origContainer = container
return true
}
})
})
}
}
if (origContainer) {
if (type == 'video') {
if (codecConfig.needsVideo > -1 || codecConfig.needsAudio > -1) {
// we set the container to the original then
// as we will not transcode audio or video
// here depending on circumstances
codecConfig.container = origContainer
}
} else if (type == 'audio') {
if (codecConfig.needsAudio > -1) {
// we set the container to the original then
// as we will not transcode audio at all
codecConfig.container = origContainer
}
}
}
} else if (type == 'video') {
// just get maxWidth && maxHeight, we need those for aspect ratio / crop
if (meta.streams && meta.streams.length) {
meta.streams.some(stream => {
if (stream.codec_type && stream.codec_type == 'video') {
codecConfig.maxHeight = stream.height > codecConfig.maxHeight ? stream.height : codecConfig.maxHeight
codecConfig.maxWidth = stream.width > codecConfig.maxWidth ? stream.width : codecConfig.maxWidth
}
})
}
}
}
type = type || 'video'
if (codecConfig.container == 'all') {
const encId = Object.keys(supported[type])[0]
codecConfig.container = encId
if (supported[type][encId][0].audio)
codecConfig.audio = supported[type][encId][0].audio
if (supported[type][encId][0].video)
codecConfig.video = supported[type][encId][0].video
}
if (type == 'audio') {
codecConfig.isAudio = true
}
return codecConfig
}
}
}