From 1b43032bf71434dd769f42e438b2fcbb20772d8c Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 10 Jan 2021 18:15:43 +0200 Subject: [PATCH] Add support for videoEdit.preview() --- __TESTS__/unit/actions/VideoEdit.test.ts | 12 +++++ src/actions/videoEdit.ts | 20 ++++++-- src/actions/videoEdit/PreviewAction.ts | 58 ++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 src/actions/videoEdit/PreviewAction.ts diff --git a/__TESTS__/unit/actions/VideoEdit.test.ts b/__TESTS__/unit/actions/VideoEdit.test.ts index 273cd1e7..74910330 100644 --- a/__TESTS__/unit/actions/VideoEdit.test.ts +++ b/__TESTS__/unit/actions/VideoEdit.test.ts @@ -116,4 +116,16 @@ describe('Tests for Transformation Action -- VideoEdit', () => { expect(url).toBe('https://res.cloudinary.com/demo/video/upload/e_volume:10/sample'); }); + + it('Tests a preview transformation for a video', () => { + const url = createNewVideo('sample') + .videoEdit( + VideoEdit.preview() + .duration(5) + .minimumSegmentDuration(1) + .maximumSegments(10) + ).toString(); + + expect(url).toContain('e_preview:duration_5.0:min_seg_dur_1.0:max_seg_10'); + }); }); diff --git a/src/actions/videoEdit.ts b/src/actions/videoEdit.ts index f7399fd6..d1adb92d 100644 --- a/src/actions/videoEdit.ts +++ b/src/actions/videoEdit.ts @@ -2,6 +2,7 @@ import TrimAction from './videoEdit/TrimAction'; import ConcatenateAction from './videoEdit/ConcatenateAction'; import VolumeAction from "./videoEdit/VolumeAction"; import {VideoSource} from "../values/source/sourceTypes/VideoSource"; +import {PreviewAction} from "./videoEdit/PreviewAction"; /** * Methods for editing a video. @@ -53,7 +54,20 @@ function volume(volumeValue: string | number): VolumeAction{ return new VolumeAction(volumeValue); } -export declare type videoEditType = VolumeAction | TrimAction | ConcatenateAction; +/** + * @description A video preview is a short excerpt from a video that can be used to engage your audience and help them select the video content that interests them. + * + * Learn more: {@link https://cloudinary.com/documentation/video_manipulation_and_delivery#generate_an_ai_based_video_preview | + * Create a video preview} + * + * @memberOf Actions.VideoEdit + * @return {Actions.VideoEdit.PreviewAction} + */ +function preview(): PreviewAction{ + return new PreviewAction(); +} + +export declare type videoEditType = VolumeAction | TrimAction | ConcatenateAction | PreviewAction; -const VideoEdit = {concatenate, trim, volume}; -export {VideoEdit, concatenate, trim, volume}; +const VideoEdit = {concatenate, trim, volume, preview}; +export {VideoEdit, concatenate, trim, volume, preview}; diff --git a/src/actions/videoEdit/PreviewAction.ts b/src/actions/videoEdit/PreviewAction.ts new file mode 100644 index 00000000..1fbac5a0 --- /dev/null +++ b/src/actions/videoEdit/PreviewAction.ts @@ -0,0 +1,58 @@ +import {Action} from "../../internal/Action"; +import {toFloatAsString} from "../../internal/utils/toFloatAsString"; + +/** + * @description Class for creating a preview of a video + * @memberOf Actions.VideoEdit + * @extend {SDK.Action} + */ +class PreviewAction extends Action { + private _minSeg: string | number; + private _maxSeg: string | number; + private _duration: string | number; + + constructor() { + super(); + } + + /** + * @description Control the duration of the video segments + * @param {string|number} minSegDuration The duration of a video segment + * @return {this} + */ + minimumSegmentDuration(minSegDuration: string | number): this { + this._minSeg = minSegDuration; + return this; + } + + /** + * @description Control the number of the video segments + * @param {string|number} maxSeg The number of the video segments. + * @return {this} + */ + maximumSegments(maxSeg: string | number): this { + this._maxSeg = maxSeg; + return this; + } + + /** + * @description control the length of the generated preview + * @param {string|number} duration The duration in seconds such as 1.2, or 5.0 + * @return {this} + */ + duration(duration: string | number): this { + this._duration = duration; + return this; + } + + toString(): string { + return [ + 'e_preview', + this._duration && `duration_${toFloatAsString(this._duration)}`, + this._minSeg && `min_seg_dur_${toFloatAsString(this._minSeg)}`, + this._maxSeg && `max_seg_${toFloatAsString(this._maxSeg)}` + ].filter((a) => a).join(':'); + } +} + +export {PreviewAction};