Skip to content

Commit bd58202

Browse files
nirmaozNir Maoz
authored andcommitted
Fix video & audio mime types
1 parent db11116 commit bd58202

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed

__tests__/audio.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ describe('Audio', () => {
5656
<Transformation duration='2' />
5757
</Audio>
5858
)
59-
expect(tag.find('[type="audio/wav"]').props().src).toEndWith(
59+
expect(tag.find('[type="audio/vnd.wav"]').props().src).toEndWith(
6060
'/du_2/e_volume:30/dog.wav'
6161
)
62-
expect(tag.find('[type="audio/mp3"]').props().src).toEndWith(
62+
expect(tag.find('[type="audio/mpeg"]').props().src).toEndWith(
6363
'/du_2/e_volume:45/dog.mp3'
6464
)
6565
})
@@ -140,7 +140,7 @@ describe('Audio', () => {
140140
expect(tag.find('[type="audio/aac"]').props().src).toEndWith(
141141
'/du_1/dog.aac'
142142
)
143-
expect(tag.find('[type="audio/mp3"]').props().src).toEndWith(
143+
expect(tag.find('[type="audio/mpeg"]').props().src).toEndWith(
144144
'/du_2/dog.mp3'
145145
)
146146
expect(tag.find('[type="audio/ogg"]').props().src).toEndWith(

__tests__/video.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ describe('Video', () => {
371371
{
372372
type: 'webm',
373373
transformations: { videoCodec: 'auto' }
374+
},
375+
{
376+
type: '3gp',
377+
transformations: { videoCodec: 'auto' }
374378
}
375379
]}
376380
audioCodec={'aac'}
@@ -387,7 +391,7 @@ describe('Video', () => {
387391
expect(tag.props().height).toEqual(100);
388392
expect(tag.props().poster).toEqual(`${VIDEO_UPLOAD_PATH}ac_aac,so_3,vc_h264/dog.jpg`);
389393

390-
expect(tag.children('source')).toHaveLength(4);
394+
expect(tag.children('source')).toHaveLength(5);
391395

392396
expect(tag.childAt(0).prop('type')).toEqual('video/mp4; codecs=hev1');
393397
expect(tag.childAt(0).prop('src')).toEqual(`${VIDEO_UPLOAD_PATH}ac_aac,so_3,vc_h265/dog.mp4`);
@@ -400,6 +404,9 @@ describe('Video', () => {
400404

401405
expect(tag.childAt(3).prop('type')).toEqual('video/webm');
402406
expect(tag.childAt(3).prop('src')).toEqual(`${VIDEO_UPLOAD_PATH}ac_aac,so_3,vc_auto/dog.webm`);
407+
408+
expect(tag.childAt(4).prop('type')).toEqual('video/3gpp');
409+
expect(tag.childAt(4).prop('src')).toEqual(`${VIDEO_UPLOAD_PATH}ac_aac,so_3,vc_auto/dog.3gp`);
403410
});
404411
});
405412
})

src/Util/cloudinaryReactUtils.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,35 @@ import {Transformation, Util} from "cloudinary-core";
22
// props passed to cloudinary-core but should not be rendered as dom attributes
33
const CLOUDINARY_REACT_PROPS = ['accessibility', 'breakpoints', 'dataSrc', 'placeholder', 'publicId', 'signature'];
44

5+
/**
6+
* Convert common video file extensions to mime types
7+
* Most other common video file extensions have an identical mime type so do not need conversion.
8+
*/
9+
const VIDEO_MIME_TYPES = {
10+
flv: 'x-flv',
11+
'3gp': '3gpp',
12+
mov: 'quicktime',
13+
mpg: 'mpeg',
14+
avi: 'x-msvideo',
15+
wmv: 'x-ms-wmv',
16+
ogv: 'ogg'
17+
};
18+
19+
/**
20+
* Convert common audio file extensions to mime types
21+
* Most other common audio file extensions have an identical mime type so do not need conversion.
22+
*/
23+
const AUDIO_MIME_TYPES = {
24+
m4a: 'mp4',
25+
wav: 'vnd.wav',
26+
m3u: 'x-mpegurl',
27+
mp3: 'mpeg',
28+
ogv: 'ogg',
29+
aif: 'x-aiff',
30+
aifc: 'x-aiff',
31+
aiff: 'x-aiff',
32+
};
33+
534
// props passed to cloudinary-core for dom elements attributes generation
635
// Map Cloudinary props from array to object for efficient lookup
736
const CLOUDINARY_PROPS = [...Transformation.PARAM_NAMES, ...CLOUDINARY_REACT_PROPS].map(Util.camelCase).reduce(
@@ -47,5 +76,7 @@ const extractCloudinaryProps = ({children, ...props}) => {
4776

4877
export {
4978
CLOUDINARY_REACT_PROPS,
79+
VIDEO_MIME_TYPES,
80+
AUDIO_MIME_TYPES,
5081
extractCloudinaryProps
5182
};

src/components/Audio/Audio.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import Video from '../Video';
4+
import {AUDIO_MIME_TYPES} from "../../Util";
45

56
/**
67
* A component representing a Cloudinary served audio
@@ -10,6 +11,7 @@ import Video from '../Video';
1011
*/
1112
class Audio extends Video {
1213
mimeType = 'audio';
14+
mimeSubTypes = AUDIO_MIME_TYPES;
1315

1416
/**
1517
* Render an audio element

src/components/Video/Video.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import {Cloudinary, Util} from 'cloudinary-core';
44
import CloudinaryComponent from '../CloudinaryComponent';
5-
import {extractCloudinaryProps} from "../../Util";
5+
import {VIDEO_MIME_TYPES, extractCloudinaryProps} from "../../Util";
66

77
/**
88
* A component representing a Cloudinary served video
99
*/
1010
class Video extends CloudinaryComponent {
1111
mimeType = 'video';
12+
mimeSubTypes = VIDEO_MIME_TYPES;
1213

1314
/**
1415
* Merge context with props
@@ -76,7 +77,7 @@ class Video extends CloudinaryComponent {
7677
* @param publicId - identifier of the video asset
7778
* @param childTransformations - child transformations for this video element
7879
* @param transformations - source transformations for specified source type
79-
* @param sourceType - format of the video url
80+
* @param sourceType - format of the video url
8081
* @param mimeType - MIME type if specified source type
8182
*/
8283
toSourceTag = (cld, publicId, childTransformations, transformations, sourceType, mimeType) => {
@@ -91,11 +92,11 @@ class Video extends CloudinaryComponent {
9192

9293
/**
9394
* Determines MIME type of given source type and codecs.
94-
* @param type - format of the video
95+
* @param type - format of the video
9596
* @param codecs - optional information about codecs of the video
9697
*/
9798
buildMimeType = (type, codecs) => {
98-
let mimeType = `${this.mimeType}/${type === 'ogv' ? 'ogg' : type}`;
99+
let mimeType = `${this.mimeType}/${this.mimeSubTypes[type] || type}`;
99100
if (!Util.isEmpty(codecs)) {
100101
mimeType += "; codecs=" + (Util.isArray(codecs) ? codecs.join(', ') : codecs);
101102
}

0 commit comments

Comments
 (0)