Skip to content

Commit 4ba749b

Browse files
authored
Feature/add transcode from to json (#506)
1 parent 8f3b107 commit 4ba749b

File tree

14 files changed

+373
-4
lines changed

14 files changed

+373
-4
lines changed

__TESTS_BUNDLE_SIZE__/bundleSizeTestCases.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const bundleSizeTestCases:ITestCase[] = [
7474
},
7575
{
7676
name: 'Import all of the SDK',
77-
sizeLimitInKB: 119,
77+
sizeLimitInKB: 121,
7878
importsArray: [
7979
importFromPackage('* as CloudinaryURLGEN')
8080
]
@@ -88,7 +88,7 @@ const bundleSizeTestCases:ITestCase[] = [
8888
},
8989
{
9090
name: 'Import All Actions',
91-
sizeLimitInKB: 45,
91+
sizeLimitInKB: 47,
9292
importsArray: [
9393
importFromPackage('Actions')
9494
]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {fromJson} from "../../../src/internal/fromJson";
2+
3+
describe('transcode.fromJson', () => {
4+
it('should generate a url with transcode actions from array of models', function () {
5+
const transformation = fromJson({actions:[
6+
{ actionType: 'keyframeInterval', interval: 20},
7+
{ actionType: 'fps', fps: 6 },
8+
{ actionType: 'fps', fps: {from: 6, to: 8} },
9+
{ actionType: 'fps', fps: {from: 6} },
10+
{actionType: 'bitRate', bitRate: 6},
11+
{actionType: 'bitRate', bitRate: '500k', constant: true},
12+
{actionType: 'audioCodec', audioCodec: 'aac'},
13+
{actionType: 'audioFrequency', audioFrequencyType: 'freq8000'},
14+
{actionType: 'streamingProfile', profile: 'fullHd'},
15+
{actionType: 'toAnimated', animatedFormat: 'gif', delay: 20, sampling: '4s'}
16+
]});
17+
18+
expect(transformation.toString()).toStrictEqual([
19+
'ki_20.0',
20+
'fps_6',
21+
'fps_6-8',
22+
'fps_6-',
23+
'br_6',
24+
'br_500k:constant',
25+
'ac_aac',
26+
'af_8000',
27+
'sp_full_hd',
28+
'dl_20,f_gif,fl_animated,vs_4s'
29+
].join('/'));
30+
});
31+
});
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import {Transformation} from "../../../src";
2+
import {Transcode} from "../../../src/actions/transcode";
3+
import {AudioCodec} from "../../../src/qualifiers/audioCodec";
4+
import {AudioFrequency} from "../../../src/qualifiers/audioFrequency";
5+
import {AnimatedFormat} from "../../../src/qualifiers/animatedFormat";
6+
7+
describe('Transcode toJson()', () => {
8+
it('transcode.keyframeInterval number', () => {
9+
const transformation = new Transformation()
10+
.addAction(Transcode.keyframeInterval(4));
11+
expect(transformation.toJson()).toStrictEqual({
12+
actions: [
13+
{
14+
actionType: 'keyframeInterval',
15+
interval: 4
16+
}
17+
]
18+
});
19+
});
20+
21+
it('transcode.fps', () => {
22+
const transformation = new Transformation()
23+
.addAction(Transcode.fps(4));
24+
expect(transformation.toJson()).toStrictEqual({
25+
actions: [
26+
{
27+
actionType: 'fps',
28+
fps: 4
29+
}
30+
]
31+
});
32+
});
33+
34+
it('transcode.fpsRange', () => {
35+
const transformation = new Transformation()
36+
.addAction(Transcode.fpsRange(4, 7));
37+
expect(transformation.toJson()).toStrictEqual({
38+
actions: [
39+
{
40+
actionType: 'fps',
41+
fps: {from: 4, to: 7}
42+
}
43+
]
44+
});
45+
});
46+
47+
it('transcode.fpsRange from', () => {
48+
const transformation = new Transformation()
49+
.addAction(Transcode.fpsRange(4));
50+
expect(transformation.toJson()).toStrictEqual({
51+
actions: [
52+
{
53+
actionType: 'fps',
54+
fps: {from: 4}
55+
}
56+
]
57+
});
58+
});
59+
60+
it('transcode.bitRate', () => {
61+
const transformation = new Transformation()
62+
.addAction(Transcode.bitRate('500k'));
63+
expect(transformation.toJson()).toStrictEqual({
64+
actions: [
65+
{
66+
actionType: 'bitRate',
67+
bitRate: '500k'
68+
}
69+
]
70+
});
71+
});
72+
73+
it('transcode.bitRate.constant', () => {
74+
const transformation = new Transformation()
75+
.addAction(Transcode.bitRate('500k').constant());
76+
expect(transformation.toJson()).toStrictEqual({
77+
actions: [
78+
{
79+
actionType: 'bitRate',
80+
bitRate: '500k',
81+
constant: true
82+
}
83+
]
84+
});
85+
});
86+
87+
it('transcode.audioCodec', () => {
88+
const transformation = new Transformation()
89+
.addAction(Transcode.audioCodec(AudioCodec.aac()));
90+
expect(transformation.toJson()).toStrictEqual({
91+
actions: [
92+
{
93+
actionType: 'audioCodec',
94+
audioCodec: 'aac',
95+
}
96+
]
97+
});
98+
});
99+
100+
it('transcode.audioFrequency', () => {
101+
const transformation = new Transformation()
102+
.addAction(Transcode
103+
.audioFrequency('freq8000'));
104+
expect(transformation.toJson()).toStrictEqual({
105+
actions: [
106+
{
107+
actionType: 'audioFrequency',
108+
audioFrequencyType: 'freq8000',
109+
}
110+
]
111+
});
112+
});
113+
114+
it('transcode.streamingProfile', () => {
115+
const transformation = new Transformation()
116+
.addAction(Transcode
117+
.streamingProfile("full_hd"));
118+
expect(transformation.toJson()).toStrictEqual({
119+
actions: [
120+
{
121+
actionType: 'streamingProfile',
122+
profile: 'fullHd',
123+
}
124+
]
125+
});
126+
});
127+
128+
it('transcode.animatedFormat', () => {
129+
const transformation = new Transformation()
130+
.addAction(Transcode
131+
.toAnimated('gif').delay(20).sampling('4s'));
132+
expect(transformation.toJson()).toStrictEqual({
133+
actions: [
134+
{
135+
actionType: 'toAnimated',
136+
animatedFormat: 'gif',
137+
delay: 20,
138+
sampling: '4s'
139+
}
140+
]
141+
});
142+
});
143+
});

src/actions/transcode/AudioCodecAction.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {Action} from "../../internal/Action.js";
22
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
3+
import {IAudioCodecActionModel} from "../../internal/models/ITranscodeActionModel.js";
4+
import {IActionModel} from "../../internal/models/IActionModel.js";
35

46
/**
57
* @extends SDK.Action
@@ -10,9 +12,21 @@ import {Qualifier} from "../../internal/qualifier/Qualifier.js";
1012
* @see Visit {@link Actions.Transcode|Transcode} for an example
1113
*/
1214
class AudioCodecAction extends Action {
15+
protected _actionModel : IAudioCodecActionModel = {actionType: 'audioCodec'};
1316
constructor(codec: string) {
1417
super();
1518
this.addQualifier(new Qualifier('ac', codec));
19+
this._actionModel.audioCodec = codec;
20+
}
21+
22+
static fromJson(actionModel: IActionModel): AudioCodecAction {
23+
const {audioCodec} = (actionModel as IAudioCodecActionModel);
24+
25+
// We are using this() to allow inheriting classes to use super.fromJson.apply(this, [actionModel])
26+
// This allows the inheriting classes to determine the class to be created
27+
const result = new this(audioCodec);
28+
29+
return result;
1630
}
1731
}
1832

src/actions/transcode/AudioFrequencyAction.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {Action} from "../../internal/Action.js";
22
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
3+
import {IAudioFrequencyActionModel} from "../../internal/models/ITranscodeActionModel.js";
4+
import {IActionModel} from "../../internal/models/IActionModel.js";
35

46
/**
57
* @extends SDK.Action
@@ -10,9 +12,21 @@ import {Qualifier} from "../../internal/qualifier/Qualifier.js";
1012
* @see Visit {@link Actions.Transcode|Transcode} for an example
1113
*/
1214
class AudioFrequencyAction extends Action {
15+
protected _actionModel : IAudioFrequencyActionModel = {actionType: 'audioFrequency'};
1316
constructor(freq: string|number) {
1417
super();
1518
this.addQualifier(new Qualifier('af', freq));
19+
this._actionModel.audioFrequencyType = freq as string;
20+
}
21+
22+
static fromJson(actionModel: IActionModel): AudioFrequencyAction {
23+
const {audioFrequencyType} = (actionModel as IAudioFrequencyActionModel);
24+
25+
// We are using this() to allow inheriting classes to use super.fromJson.apply(this, [actionModel])
26+
// This allows the inheriting classes to determine the class to be created
27+
const result = new this(audioFrequencyType.replace('freq', ''));
28+
29+
return result;
1630
}
1731
}
1832

src/actions/transcode/BitRateAction.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {Action} from "../../internal/Action.js";
22
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
33
import {QualifierValue} from "../../internal/qualifier/QualifierValue.js";
4+
import {IBitRateActionModel} from "../../internal/models/ITranscodeActionModel.js";
5+
import {IActionModel} from "../../internal/models/IActionModel.js";
46

57
/**
68
* @extends SDK.Action
@@ -13,16 +15,19 @@ import {QualifierValue} from "../../internal/qualifier/QualifierValue.js";
1315
class BitRateAction extends Action {
1416
private bitRate: string|number;
1517
private isConstant = false;
18+
protected _actionModel : IBitRateActionModel = {actionType: 'bitRate'}
1619

1720
constructor(bitRate: string|number) {
1821
super();
1922
this.bitRate = bitRate;
23+
this._actionModel.bitRate = bitRate;
2024
}
2125
/**
2226
* @description video plays with a constant bitrate (CBR).
2327
*/
2428
constant(): this {
2529
this.isConstant = true;
30+
this._actionModel.constant = true;
2631
return this;
2732
}
2833

@@ -36,6 +41,17 @@ class BitRateAction extends Action {
3641
this.addQualifier(new Qualifier('br', qualifierValue));
3742
return this;
3843
}
44+
45+
static fromJson(actionModel: IActionModel): BitRateAction {
46+
const {bitRate, constant} = (actionModel as IBitRateActionModel);
47+
48+
// We are using this() to allow inheriting classes to use super.fromJson.apply(this, [actionModel])
49+
// This allows the inheriting classes to determine the class to be created
50+
const result = new this(bitRate);
51+
constant && result.constant();
52+
53+
return result;
54+
}
3955
}
4056

4157
export default BitRateAction;

src/actions/transcode/FPSAction.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {Action} from "../../internal/Action.js";
22
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
3+
import {IFPSActionModel, IFPSRangeActionModel} from "../../internal/models/ITranscodeActionModel.js";
4+
import {IActionModel} from "../../internal/models/IActionModel.js";
5+
import FPSRangeAction from "./FPSRangeAction.js";
36

47
/**
58
* @extends SDK.Action
@@ -12,11 +15,28 @@ import {Qualifier} from "../../internal/qualifier/Qualifier.js";
1215
* @see Visit {@link Actions.Transcode|Transcode} for an example
1316
*/
1417
class FPSAction extends Action {
18+
protected _actionModel : IFPSActionModel = {actionType: 'fps'};
1519

1620
constructor(from: number) {
1721
super();
22+
this._actionModel.fps = from;
1823
this.addQualifier(new Qualifier('fps', from));
1924
}
25+
26+
static fromJson(actionModel: IActionModel): FPSAction | FPSRangeAction {
27+
const {fps} = (actionModel as IFPSActionModel);
28+
let result;
29+
if(typeof fps === 'object') {
30+
//@ts-ignore
31+
result = new FPSRangeAction(fps.from, fps.to);
32+
}else {
33+
result = new this(fps);
34+
}
35+
// We are using this() to allow inheriting classes to use super.fromJson.apply(this, [actionModel])
36+
// This allows the inheriting classes to determine the class to be created
37+
38+
return result;
39+
}
2040
}
2141

2242
export default FPSAction;

src/actions/transcode/FPSRangeAction.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {Action} from "../../internal/Action.js";
22
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
33
import {QualifierValue} from "../../internal/qualifier/QualifierValue.js";
4+
import {IFPSRangeActionModel} from "../../internal/models/ITranscodeActionModel.js";
5+
import {IActionModel} from "../../internal/models/IActionModel.js";
46

57
/**
68
* @extends SDK.Action
@@ -15,11 +17,19 @@ import {QualifierValue} from "../../internal/qualifier/QualifierValue.js";
1517
class FPSRangeAction extends Action {
1618
private from: number;
1719
private to: number;
20+
protected _actionModel : IFPSRangeActionModel = {};
1821

1922
constructor(from: number, to?: number) {
2023
super();
2124
this.from = from;
22-
this.to = to;
25+
this._actionModel = {
26+
actionType: 'fps',
27+
fps: {from}
28+
};
29+
if (to != null) {
30+
this.to = to;
31+
this._actionModel.fps.to = to;
32+
}
2333
}
2434

2535

0 commit comments

Comments
 (0)