diff --git a/src/plugins/cloudinary/models/video-source/video-source.const.js b/src/plugins/cloudinary/models/video-source/video-source.const.js index 11b345fa..835dd49a 100644 --- a/src/plugins/cloudinary/models/video-source/video-source.const.js +++ b/src/plugins/cloudinary/models/video-source/video-source.const.js @@ -37,14 +37,36 @@ export const VIDEO_SUFFIX_REMOVAL_PATTERN = RegExp(`\\.(${COMMON_VIDEO_EXTENSION // eslint-disable-next-line no-control-regex export const URL_PATTERN = RegExp('https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\+.~#?&/=]*)'); - export const CONTAINER_MIME_TYPES = { - dash: ['application/dash+xml'], - hls: ['application/x-mpegURL'], - mpd: ['application/dash+xml'], - m3u8: ['application/x-mpegURL'] + hls: 'application/x-mpegURL', + dash: 'application/dash+xml', + + // See: https://docs.videojs.com/utils_mimetypes.js.html + opus: 'video/ogg', + ogv: 'video/ogg', + mp4: 'video/mp4', + mov: 'video/mp4', + m4v: 'video/mp4', + mkv: 'video/x-matroska', + m4a: 'audio/mp4', + mp3: 'audio/mpeg', + aac: 'audio/aac', + caf: 'audio/x-caf', + flac: 'audio/flac', + oga: 'audio/ogg', + wav: 'audio/wav', + m3u8: 'application/x-mpegURL', + mpd: 'application/dash+xml', + jpg: 'image/jpeg', + jpeg: 'image/jpeg', + gif: 'image/gif', + png: 'image/png', + svg: 'image/svg+xml', + webp: 'image/webp' }; +export const ADAPTIVE_SOURCETYPES = ['hls', 'dash', 'mpd', 'm3u8']; + export const FORMAT_MAPPINGS = { hls: 'm3u8', dash: 'mpd' diff --git a/src/plugins/cloudinary/models/video-source/video-source.js b/src/plugins/cloudinary/models/video-source/video-source.js index e4a30db3..9c76a07a 100644 --- a/src/plugins/cloudinary/models/video-source/video-source.js +++ b/src/plugins/cloudinary/models/video-source/video-source.js @@ -5,6 +5,7 @@ import { castArray } from 'utils/array'; import { SOURCE_TYPE } from 'utils/consts'; import { CONTAINER_MIME_TYPES, + ADAPTIVE_SOURCETYPES, DEFAULT_POSTER_PARAMS, DEFAULT_VIDEO_PARAMS, VIDEO_SUFFIX_REMOVAL_PATTERN @@ -193,7 +194,7 @@ class VideoSource extends BaseSource { const srcs = this.sourceTypes().map(sourceType => { const srcTransformation = this.sourceTransformation()[sourceType] || this.transformation(); const format = normalizeFormat(sourceType); - const isAdaptive = ['mpd', 'm3u8'].indexOf(format) !== -1; + const isAdaptive = ADAPTIVE_SOURCETYPES.includes(format); const opts = {}; if (srcTransformation) { @@ -244,10 +245,12 @@ class VideoSource extends BaseSource { } generateRawSource(url, type) { - const t = type || url.split('.').pop(); - const isAdaptive = !!CONTAINER_MIME_TYPES[t]; - if (isAdaptive) { - type = CONTAINER_MIME_TYPES[t][0]; + type = type || url.split('.').pop(); + + const isAdaptive = ADAPTIVE_SOURCETYPES.includes(type); + + if (CONTAINER_MIME_TYPES[type]) { + type = CONTAINER_MIME_TYPES[type]; } else { type = type ? `video/${type}` : null; } diff --git a/src/plugins/cloudinary/models/video-source/video-source.utils.js b/src/plugins/cloudinary/models/video-source/video-source.utils.js index 663dfeac..715559ac 100644 --- a/src/plugins/cloudinary/models/video-source/video-source.utils.js +++ b/src/plugins/cloudinary/models/video-source/video-source.utils.js @@ -6,16 +6,12 @@ import { some } from '../../../../utils/array'; export function formatToMimeTypeAndTransformation(format) { const [container, codec] = format.toLowerCase().split('\/'); - let result = CONTAINER_MIME_TYPES[container]; - let transformation = null; - - if (!result) { - result = [`video/${container}`, transformation]; - } + const mimetype = CONTAINER_MIME_TYPES[container] || `video/${container}`; + let result = [mimetype]; if (codec) { - transformation = codecToSrcTransformation(codec); - result = [`${result[0]}; codecs="${codecShorthandTrans(codec)}"`, transformation]; + const transformation = codecToSrcTransformation(codec); + result = [`${mimetype}; codecs="${codecShorthandTrans(codec)}"`, transformation]; } return result; diff --git a/src/utils/cloudinary.js b/src/utils/cloudinary.js index a8ddcd27..e4328bfb 100644 --- a/src/utils/cloudinary.js +++ b/src/utils/cloudinary.js @@ -34,7 +34,7 @@ const setError = (that, res) => { }; const setVideoSrc = (that, srcs) => { - console.log('Trying urls: ' + JSON.stringify(srcs)); + console.log('Trying sources: ', srcs); srcs.forEach(s => { s.try = true; }); diff --git a/test/unit/videoSource.test.js b/test/unit/videoSource.test.js index 37469bf4..b910a9bb 100644 --- a/test/unit/videoSource.test.js +++ b/test/unit/videoSource.test.js @@ -158,7 +158,7 @@ describe('Raw url tests', () => { let source = new VideoSource(url, sourceDef); let srcs = source.generateSources(); expect(srcs[0].src).toEqual(url); - expect(srcs[0].type).toEqual(null); + expect(srcs[0].type).toEqual('video/mp4'); expect(srcs[0].isAdaptive).toEqual(false); }); it('Test raw url with transformations', () => { @@ -169,7 +169,7 @@ describe('Raw url tests', () => { let source = new VideoSource(url, sourceDef); let srcs = source.generateSources(); expect(srcs[0].src).toEqual(url); - expect(srcs[0].type).toEqual(null); + expect(srcs[0].type).toEqual('video/mp4'); expect(srcs[0].isAdaptive).toEqual(false); }); it('Test raw url without extension', () => {