Skip to content

Commit 7577d8a

Browse files
author
Nir Maoz
authored
Add preview.toJson & preview.fromJson (#503)
1 parent adc3d1d commit 7577d8a

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {fromJson} from "../../../src/internal/fromJson";
2+
import {IPreviewActionModel} from "../../../src/internal/models/IPreviewActionModel";
3+
4+
describe('videoEdit.preview fromJson', () => {
5+
it('Should generate PreviewAction from model', () => {
6+
const previewModel: IPreviewActionModel = {
7+
actionType: 'preview',
8+
duration: 5,
9+
maximumSegments: 10,
10+
minimumSegmentDuration: 1
11+
};
12+
13+
const transformation = fromJson({actions: [previewModel]});
14+
15+
expect(transformation.toString()).toStrictEqual('e_preview:duration_5.0:max_seg_10:min_seg_dur_1.0');
16+
});
17+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {Transformation} from '../../../src';
2+
import {VideoEdit} from "../../../src/actions";
3+
4+
describe('videoEdit.preview toJson', () => {
5+
it('Should return a json representation of given preview action', () => {
6+
const transformation = new Transformation()
7+
.videoEdit(VideoEdit.preview()
8+
.duration(1)
9+
.maximumSegments(10)
10+
.minimumSegmentDuration(1)
11+
);
12+
expect(transformation.toJson()).toStrictEqual({
13+
actions: [
14+
{
15+
actionType: 'preview',
16+
duration: 1,
17+
maximumSegments: 10,
18+
minimumSegmentDuration: 1
19+
}
20+
]
21+
});
22+
});
23+
});

src/actions/videoEdit/PreviewAction.ts

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

46
/**
57
* @description Class for creating a preview of a video
@@ -8,12 +10,16 @@ import {toFloatAsString} from "../../internal/utils/toFloatAsString.js";
810
* @see Visit {@link Actions.VideoEdit|VideoEdit} for an example
911
*/
1012
class PreviewAction extends Action {
13+
protected _actionModel: IPreviewActionModel;
1114
private _minSeg: string | number;
1215
private _maxSeg: string | number;
1316
private _duration: string | number;
1417

1518
constructor() {
1619
super();
20+
this._actionModel = {
21+
actionType: 'preview'
22+
};
1723
}
1824

1925
/**
@@ -22,6 +28,7 @@ class PreviewAction extends Action {
2228
* @return {this}
2329
*/
2430
minimumSegmentDuration(minSegDuration: string | number): this {
31+
this._actionModel.minimumSegmentDuration = +minSegDuration;
2532
this._minSeg = minSegDuration;
2633
return this;
2734
}
@@ -32,6 +39,7 @@ class PreviewAction extends Action {
3239
* @return {this}
3340
*/
3441
maximumSegments(maxSeg: string | number): this {
42+
this._actionModel.maximumSegments = +maxSeg;
3543
this._maxSeg = maxSeg;
3644
return this;
3745
}
@@ -42,6 +50,7 @@ class PreviewAction extends Action {
4250
* @return {this}
4351
*/
4452
duration(duration: string | number): this {
53+
this._actionModel.duration = +duration;
4554
this._duration = duration;
4655
return this;
4756
}
@@ -54,6 +63,26 @@ class PreviewAction extends Action {
5463
this._minSeg && `min_seg_dur_${toFloatAsString(this._minSeg)}`
5564
].filter((a) => a).join(':');
5665
}
66+
67+
static fromJson(actionModel: IActionModel): PreviewAction {
68+
const {duration, maximumSegments, minimumSegmentDuration} = (actionModel as IPreviewActionModel);
69+
// We are using this() to allow inheriting classes to use super.fromJson.apply(this, [actionModel])
70+
// This allows the inheriting classes to determine the class to be created
71+
const result = new this();
72+
if (duration != null){
73+
result.duration(duration);
74+
}
75+
76+
if (maximumSegments != null){
77+
result.maximumSegments(maximumSegments);
78+
}
79+
80+
if (minimumSegmentDuration != null){
81+
result.minimumSegmentDuration(minimumSegmentDuration);
82+
}
83+
84+
return result;
85+
}
5786
}
5887

5988
export {PreviewAction};

src/internal/fromJson.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {ImproveAction} from "../actions/adjust/ImproveAction.js";
4242
import {DeliveryDPRAction} from "../actions/delivery/DeliveryDPRAction.js";
4343
import ConcatenateAction from "../actions/videoEdit/ConcatenateAction.js";
4444
import {ITransformationModel} from "./models/ITransformationModel.js";
45+
import {PreviewAction} from "../actions/videoEdit/PreviewAction.js";
4546

4647
const ActionModelMap: Record<string, IHasFromJson> = {
4748
scale: ResizeScaleAction,
@@ -93,7 +94,8 @@ const ActionModelMap: Record<string, IHasFromJson> = {
9394
contrast: EffectActionWithLevel,
9495
brightness: EffectActionWithLevel,
9596
gamma: EffectActionWithLevel,
96-
concatenate: ConcatenateAction
97+
concatenate: ConcatenateAction,
98+
preview: PreviewAction
9799
};
98100

99101
/**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {IActionModel} from "./IActionModel.js";
2+
3+
export interface IPreviewActionModel extends IActionModel{
4+
duration?: number;
5+
maximumSegments?: number;
6+
minimumSegmentDuration?: number;
7+
}

0 commit comments

Comments
 (0)