react-native-video-processing 3.0.0-beta.0 → 3.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +56 -5
  2. package/android/src/main/java/com/shahenlibrary/videoprocessing/ColorMatrixEffect.kt +114 -0
  3. package/android/src/main/java/com/shahenlibrary/videoprocessing/ProcessPlan.kt +9 -0
  4. package/android/src/main/java/com/shahenlibrary/videoprocessing/ThumbnailGenerator.kt +25 -3
  5. package/android/src/main/java/com/shahenlibrary/videoprocessing/VideoProcessingModule.kt +1 -0
  6. package/android/src/main/java/com/shahenlibrary/videoprocessing/VideoProcessor.kt +10 -3
  7. package/ios/ColorMatrix.swift +36 -0
  8. package/ios/FrameRenderer.swift +11 -6
  9. package/ios/ProcessPlan.swift +5 -1
  10. package/ios/ThumbnailGenerator.swift +18 -1
  11. package/ios/VideoExporter.swift +1 -1
  12. package/lib/module/ProcessingBuilder.js +271 -0
  13. package/lib/module/ProcessingBuilder.js.map +1 -0
  14. package/lib/module/VideoProcessing.js +16 -1
  15. package/lib/module/VideoProcessing.js.map +1 -1
  16. package/lib/module/filters.js +142 -0
  17. package/lib/module/filters.js.map +1 -0
  18. package/lib/module/index.js +2 -0
  19. package/lib/module/index.js.map +1 -1
  20. package/lib/module/source.js +4 -1
  21. package/lib/module/source.js.map +1 -1
  22. package/lib/module/validation.js +18 -0
  23. package/lib/module/validation.js.map +1 -1
  24. package/lib/typescript/src/ProcessingBuilder.d.ts +56 -0
  25. package/lib/typescript/src/ProcessingBuilder.d.ts.map +1 -0
  26. package/lib/typescript/src/VideoProcessing.d.ts +2 -1
  27. package/lib/typescript/src/VideoProcessing.d.ts.map +1 -1
  28. package/lib/typescript/src/filters.d.ts +58 -0
  29. package/lib/typescript/src/filters.d.ts.map +1 -0
  30. package/lib/typescript/src/index.d.ts +2 -0
  31. package/lib/typescript/src/index.d.ts.map +1 -1
  32. package/lib/typescript/src/types.d.ts +37 -0
  33. package/lib/typescript/src/types.d.ts.map +1 -1
  34. package/lib/typescript/src/validation.d.ts +6 -1
  35. package/lib/typescript/src/validation.d.ts.map +1 -1
  36. package/package.json +1 -1
  37. package/src/ProcessingBuilder.ts +302 -0
  38. package/src/VideoProcessing.ts +19 -1
  39. package/src/filters.ts +195 -0
  40. package/src/index.tsx +2 -0
  41. package/src/source.ts +5 -1
  42. package/src/types.ts +25 -0
  43. package/src/validation.ts +28 -2
@@ -0,0 +1,56 @@
1
+ import type { CropRect, Filter, JobOptions, ProcessResult, ProcessingSpec, Segment, Thumbnail, ThumbnailOptions, VideoSource } from './types.js';
2
+ /**
3
+ * Builds an edit step by step and runs it in a single encode. Every method
4
+ * returns a new builder, so a base edit can be branched freely.
5
+ *
6
+ * Clip methods (`trim`, `crop`, `reverse`) change the most recently added
7
+ * clip. Output methods (`filter`, `resize`, `bitrate`, `removeAudio`) apply
8
+ * to the whole result. Frames always go through crop, then filters, then
9
+ * scaling, whatever order the methods are called in.
10
+ */
11
+ export declare class ProcessingBuilder {
12
+ private readonly spec;
13
+ constructor(source: VideoSource);
14
+ /** Starts from a plain-object edit, such as one stored as JSON. */
15
+ static fromSpec(spec: ProcessingSpec): ProcessingBuilder;
16
+ /** Keeps `startTime` to `endTime` of the current clip, relative to any earlier trim. */
17
+ trim(startTime: number, endTime?: number): ProcessingBuilder;
18
+ /** Crops the current clip, relative to any earlier crop. Display pixels, origin top-left. */
19
+ crop(rect: CropRect): ProcessingBuilder;
20
+ /** Plays the current clip backwards. Reversed clips are silent. */
21
+ reverse(): ProcessingBuilder;
22
+ /** Adds a clip after the others, optionally trimmed, cropped or reversed. */
23
+ append(source: VideoSource, configure?: (clip: ClipBuilder) => ClipBuilder): ProcessingBuilder;
24
+ /** Appends the current clip played backwards. The output is silent. */
25
+ boomerang(): ProcessingBuilder;
26
+ /** Adds colour filters, applied in order. Accepts presets as well as single filters. */
27
+ filter(...filters: Array<Filter | Filter[]>): ProcessingBuilder;
28
+ /** Fits the output inside this box, keeping the aspect ratio and never upscaling. */
29
+ resize(size: {
30
+ width?: number;
31
+ height?: number;
32
+ }): ProcessingBuilder;
33
+ bitrate(bitsPerSecond: number): ProcessingBuilder;
34
+ removeAudio(): ProcessingBuilder;
35
+ /** The edit as plain data, accepted by `process(spec)` and `ProcessingBuilder.fromSpec`. */
36
+ toSpec(): ProcessingSpec;
37
+ /**
38
+ * A frame of the first clip with the builder's filters, for previews.
39
+ * `time` is in seconds from the start of that clip. Crops aren't applied.
40
+ */
41
+ thumbnail(time?: number, options?: Omit<ThumbnailOptions, 'filters'>): Promise<Thumbnail>;
42
+ /** Runs the edit. */
43
+ export(options?: JobOptions): Promise<ProcessResult>;
44
+ private lastClip;
45
+ private updateLastClip;
46
+ }
47
+ /** The clip methods, for configuring a clip passed to `append`. */
48
+ export declare class ClipBuilder {
49
+ private readonly clip;
50
+ constructor(clip: Segment);
51
+ trim(startTime: number, endTime?: number): ClipBuilder;
52
+ crop(rect: CropRect): ClipBuilder;
53
+ reverse(): ClipBuilder;
54
+ toClip(): Segment;
55
+ }
56
+ //# sourceMappingURL=ProcessingBuilder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ProcessingBuilder.d.ts","sourceRoot":"","sources":["../../../src/ProcessingBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,QAAQ,EACR,MAAM,EACN,UAAU,EACV,aAAa,EACb,cAAc,EACd,OAAO,EACP,SAAS,EACT,gBAAgB,EAChB,WAAW,EACZ,MAAM,YAAS,CAAC;AAEjB;;;;;;;;GAQG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAiB;gBAE1B,MAAM,EAAE,WAAW;IAI/B,mEAAmE;IACnE,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,cAAc,GAAG,iBAAiB;IAUxD,wFAAwF;IACxF,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,G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lB"}
@@ -1,8 +1,9 @@
1
- import type { CropRect, ProcessOptions, ProcessResult, Segment, Thumbnail, ThumbnailOptions, ThumbnailTimes, TimeRange, VideoInfo, VideoSource } from './types.js';
1
+ import type { CropRect, JobOptions, ProcessOptions, ProcessingSpec, ProcessResult, Segment, Thumbnail, ThumbnailOptions, ThumbnailTimes, TimeRange, VideoInfo, VideoSource } from './types.js';
2
2
  export declare function getVideoInfo(source: VideoSource): Promise<VideoInfo>;
3
3
  export declare function getThumbnails(source: VideoSource, spec: ThumbnailTimes, options?: ThumbnailOptions): Promise<Thumbnail[]>;
4
4
  export declare function getThumbnail(source: VideoSource, time?: number, options?: ThumbnailOptions): Promise<Thumbnail>;
5
5
  /** Runs segments back to back into a single output. Every other operation is built on this. */
6
+ export declare function process(spec: ProcessingSpec, options?: JobOptions): Promise<ProcessResult>;
6
7
  export declare function process(segments: Segment[], options?: ProcessOptions): Promise<ProcessResult>;
7
8
  export declare function trim(source: VideoSource, options: TimeRange & ProcessOptions): Promise<ProcessResult>;
8
9
  export declare function crop(source: VideoSource, rect: CropRect, options?: TimeRange & ProcessOptions): Promise<ProcessResult>;
@@ -1 +1 @@
1
- {"version":3,"file":"VideoProcessing.d.ts","sourceRoot":"","sources":["../../../src/VideoProcessing.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EACV,QAAQ,EACR,cAAc,EACd,aAAa,EACb,OAAO,EACP,SAAS,EACT,gBAAgB,EAChB,cAAc,EACd,SAAS,EACT,SAAS,EACT,WAAW,EACZ,MAAM,YAAS,CAAC;AAEjB,wBAAsB,YAAY,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,CAE1E;AAED,wBAAsB,aAAa,CACjC,MAAM,EAAE,WAAW,EACnB,IAAI,EAAE,cAAc,EACpB,OAAO,CAAC,EAAE,gBAAgB,GACzB,OAAO,CAAC,SAAS,EAAE,CAAC,CAatB;AAED,wBAAsB,YAAY,CAChC,MAAM,EAAE,WAAW,EACnB,IA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E,WAAW,EACnB,OAAO,GAAE,SAAS,GAAG,cAAmB,GACvC,OAAO,CAAC,aAAa,CAAC,CAOxB"}
@@ -0,0 +1,58 @@
1
+ import type { Filter } from './types.js';
2
+ /** Ready-made looks. They are plain filter lists, so they can be extended or combined. */
3
+ export declare const presets: {
4
+ vintage: ({
5
+ type: "sepia";
6
+ amount: number;
7
+ } | {
8
+ type: "contrast";
9
+ amount: number;
10
+ } | {
11
+ type: "saturation";
12
+ amount: number;
13
+ } | {
14
+ type: "brightness";
15
+ amount: number;
16
+ })[];
17
+ noir: ({
18
+ type: "grayscale";
19
+ amount?: undefined;
20
+ } | {
21
+ type: "contrast";
22
+ amount: number;
23
+ })[];
24
+ fade: ({
25
+ type: "contrast";
26
+ amount: number;
27
+ } | {
28
+ type: "brightness";
29
+ amount: number;
30
+ } | {
31
+ type: "saturation";
32
+ amount: number;
33
+ })[];
34
+ vivid: ({
35
+ type: "saturation";
36
+ amount: number;
37
+ } | {
38
+ type: "contrast";
39
+ amount: number;
40
+ })[];
41
+ warm: ({
42
+ type: "sepia";
43
+ amount: number;
44
+ } | {
45
+ type: "saturation";
46
+ amount: number;
47
+ } | {
48
+ type: "brightness";
49
+ amount: number;
50
+ })[];
51
+ };
52
+ /**
53
+ * Combines filters, in order, into one colour matrix: 4 rows (R, G, B, A) of
54
+ * 5 numbers, row-major, where the fifth number is an offset in 0–1 units.
55
+ * Values are gamma-encoded RGB, as stored in the video, like CSS filters.
56
+ */
57
+ export declare function filterMatrix(filters: Filter[]): number[];
58
+ //# sourceMappingURL=filters.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filters.d.ts","sourceRoot":"","sources":["../../../src/filters.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAS,CAAC;AAEtC,0FAA0F;AAC1F,eAAO,MAAM,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsBgB,CAAC;AAQrC;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAoBxD"}
@@ -1,4 +1,6 @@
1
1
  export { boomerang, compress, crop, getThumbnail, getThumbnails, getVideoInfo, merge, process, reverse, trim, } from './VideoProcessing.js';
2
+ export { filterMatrix, presets } from './filters.js';
3
+ export { ClipBuilder, ProcessingBuilder } from './ProcessingBuilder.js';
2
4
  export { Trimmer, type TrimmerProps } from './Trimmer/Trimmer.js';
3
5
  export type { TrimRange } from './Trimmer/trimmerMath.js';
4
6
  export { VideoProcessingError } from './errors.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,QAAQ,EACR,IAAI,EACJ,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,KAAK,EACL,OAAO,EACP,OAAO,EACP,IAAI,GACL,MAAM,sBAAmB,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,sBAAmB,CAAC;AAC/D,YAAY,EAAE,SAAS,EAAE,MAAM,0BAAuB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAU,CAAC;AAChD,mBAAmB,YAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,QAAQ,EACR,IAAI,EACJ,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,KAAK,EACL,OAAO,EACP,OAAO,EACP,IAAI,GACL,MAAM,sBAAmB,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,cAAW,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,wBAAqB,CAAC;AACrE,OAAO,EAAE,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,sBAAmB,CAAC;AAC/D,YAAY,EAAE,SAAS,EAAE,MAAM,0BAAuB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAU,CAAC;AAChD,mBAAmB,YAAS,CAAC"}
@@ -31,6 +31,8 @@ export type ThumbnailOptions = {
31
31
  format?: ThumbnailFormat;
32
32
  /** JPEG quality between 0 and 1. Defaults to 0.8. */
33
33
  quality?: number;
34
+ /** Applied to the thumbnail exactly as `process` applies them to video, for filter previews. */
35
+ filters?: Filter[];
34
36
  };
35
37
  export type ThumbnailTimes = {
36
38
  times: number[];
@@ -58,6 +60,32 @@ export type CropRect = {
58
60
  width: number;
59
61
  height: number;
60
62
  };
63
+ /**
64
+ * Colour filters with CSS filter semantics. `amount` 0 leaves the video
65
+ * unchanged; adjustments run from -1 to 1, the others from 0 to 1.
66
+ */
67
+ export type Filter = {
68
+ type: 'brightness';
69
+ amount: number;
70
+ } | {
71
+ type: 'contrast';
72
+ amount: number;
73
+ } | {
74
+ type: 'saturation';
75
+ amount: number;
76
+ } | {
77
+ type: 'hue';
78
+ degrees: number;
79
+ } | {
80
+ type: 'grayscale';
81
+ amount?: number;
82
+ } | {
83
+ type: 'sepia';
84
+ amount?: number;
85
+ } | {
86
+ type: 'invert';
87
+ amount?: number;
88
+ };
61
89
  export type Segment = TimeRange & {
62
90
  source: VideoSource;
63
91
  crop?: CropRect;
@@ -79,6 +107,8 @@ export type OutputOptions = {
79
107
  */
80
108
  bitrate?: number;
81
109
  removeAudio?: boolean;
110
+ /** Colour filters, applied in order to every segment after cropping. */
111
+ filters?: Filter[];
82
112
  };
83
113
  export type JobOptions = {
84
114
  /** Called with values between 0 and 1. */
@@ -87,6 +117,13 @@ export type JobOptions = {
87
117
  signal?: AbortSignal;
88
118
  };
89
119
  export type ProcessOptions = OutputOptions & JobOptions;
120
+ /**
121
+ * A whole edit as plain data: the clips to play back to back plus the output
122
+ * options. JSON-serialisable when every source is a string or `{ uri }`.
123
+ */
124
+ export type ProcessingSpec = OutputOptions & {
125
+ clips: Segment[];
126
+ };
90
127
  export type ProcessResult = {
91
128
  /** `file://` URI of an H.264/AAC MP4 in the app's cache directory. */
92
129
  uri: string;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAC;AAE5D,MAAM,MAAM,SAAS,GAAG;IACtB,GAAG,EAAE,MAAM,CAAC;IACZ,eAAe;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,oDAAoD;IACpD,KAAK,EAAE,MAAM,CAAC;IACd,qDAAqD;IACrD,MAAM,EAAE,MAAM,CAAC;IACf,4EAA4E;IAC5E,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,+DAA+D;IAC/D,OAAO,EAAE,MAAM,CAAC;IAChB,iDAAiD;IACjD,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,OAAO,CAAC;IAClB,wEAAwE;IACxE,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,KAAK,CAAC;AAE7C,MAAM,MAAM,gBAAgB,GAAG;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,eAAe,CAAC;IACzB,qDAAqD;IACrD,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,cAAc,GACxB;IAAE,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEhF,MAAM,MAAM,SAAS,GAAG;IACtB,GAAG,EAAE,M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OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,wEAAwE;IACxE,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,0CAA0C;IAC1C,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;IACxC,8EAA8E;IAC9E,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,aAAa,GAAG,UAAU,CAAC;AAExD;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,aAAa,GAAG;IAC3C,KAAK,EAAE,OAAO,EAAE,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,sEAAsE;IACtE,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,wBAAwB,GAClC,mBAAmB,GAAG,UAAU,GAAG,aAAa,GAAG,cAAc,CAAC"}
@@ -1,7 +1,7 @@
1
1
  import type { CropRect, OutputOptions, Segment, ThumbnailOptions, ThumbnailTimes } from './types.js';
2
2
  export declare function toNativeSegment(segment: Segment, index: number): NativeSegment;
3
3
  export declare function toNativeOutput(options?: OutputOptions): NativeOutput;
4
- export declare function toNativeThumbnailOptions(options?: ThumbnailOptions): ThumbnailOptions;
4
+ export declare function toNativeThumbnailOptions(options?: ThumbnailOptions): NativeThumbnailOptions;
5
5
  export declare function thumbnailTimes(spec: ThumbnailTimes, duration?: number): number[];
6
6
  /** Picks the middle of each of `count` equal buckets, so no frame lands on the very last instant. */
7
7
  export declare function evenlySpacedTimes(startTime: number, endTime: number, count: number): number[];
@@ -17,5 +17,10 @@ export type NativeOutput = {
17
17
  height?: number;
18
18
  bitrate?: number;
19
19
  removeAudio?: boolean;
20
+ /** 4x5 row-major; see filterMatrix. */
21
+ colorMatrix?: number[];
22
+ };
23
+ export type NativeThumbnailOptions = Omit<ThumbnailOptions, 'filters'> & {
24
+ colorMatrix?: number[];
20
25
  };
21
26
  //# sourceMappingURL=validation.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../../src/validation.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,QAAQ,EACR,aAAa,EACb,OAAO,EACP,gBAAgB,EAChB,cAAc,EAEf,MAAM,YAAS,CAAC;AAEjB,wBAAgB,eAAe,CAC7B,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,MAAM,GACZ,aAAa,CAYf;AAED,wBAAgB,cAAc,CAAC,OAAO,GAAE,aAAkB,GAAG,YAAY,CAgBxE;AAED,wBAAgB,wBAAwB,CACtC,OAAO,GAAE,gBAAqB,GAC7B,gBAAgB,CAoBlB;AAED,wBAAgB,cAAc,CAC5B,IAAI,EAAE,cAAc,EACpB,QAAQ,CAAC,EAAE,MAAM,GAChB,MAAM,EAAE,CAgBV;AAED,qGAAqG;AACrG,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,GACZ,MAAM,EAAE,CAMV;AAoDD,MAAM,MAAM,aAAa,GAAG;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC"}
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../../src/validation.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV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0.0-beta.2",
4
4
  "description": "Native video processing for React Native: trim, compress, crop, merge, thumbnails",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -0,0 +1,302 @@
1
+ import { invalidOptions } from './errors';
2
+ import { getThumbnail, getVideoInfo, process } from './VideoProcessing';
3
+ import type {
4
+ CropRect,
5
+ Filter,
6
+ JobOptions,
7
+ ProcessResult,
8
+ ProcessingSpec,
9
+ Segment,
10
+ Thumbnail,
11
+ ThumbnailOptions,
12
+ VideoSource,
13
+ } from './types';
14
+
15
+ /**
16
+ * Builds an edit step by step and runs it in a single encode. Every method
17
+ * returns a new builder, so a base edit can be branched freely.
18
+ *
19
+ * Clip methods (`trim`, `crop`, `reverse`) change the most recently added
20
+ * clip. Output methods (`filter`, `resize`, `bitrate`, `removeAudio`) apply
21
+ * to the whole result. Frames always go through crop, then filters, then
22
+ * scaling, whatever order the methods are called in.
23
+ */
24
+ export class ProcessingBuilder {
25
+ private readonly spec: ProcessingSpec;
26
+
27
+ constructor(source: VideoSource) {
28
+ this.spec = { clips: [{ source }] };
29
+ }
30
+
31
+ /** Starts from a plain-object edit, such as one stored as JSON. */
32
+ static fromSpec(spec: ProcessingSpec): ProcessingBuilder {
33
+ const hasClips = Array.isArray(spec?.clips) && spec.clips.length > 0;
34
+
35
+ if (!hasClips) {
36
+ throw invalidOptions('spec.clips must contain at least one clip');
37
+ }
38
+
39
+ return withSpec(copySpec(spec));
40
+ }
41
+
42
+ /** Keeps `startTime` to `endTime` of the current clip, relative to any earlier trim. */
43
+ trim(startTime: number, endTime?: number): ProcessingBuilder {
44
+ return this.updateLastClip((clip) => narrowRange(clip, startTime, endTime));
45
+ }
46
+
47
+ /** Crops the current clip, relative to any earlier crop. Display pixels, origin top-left. */
48
+ crop(rect: CropRect): ProcessingBuilder {
49
+ return this.updateLastClip((clip) => narrowCrop(clip, rect));
50
+ }
51
+
52
+ /** Plays the current clip backwards. Reversed clips are silent. */
53
+ reverse(): ProcessingBuilder {
54
+ return this.updateLastClip((clip) => ({ ...clip, reverse: !clip.reverse }));
55
+ }
56
+
57
+ /** Adds a clip after the others, optionally trimmed, cropped or reversed. */
58
+ append(
59
+ source: VideoSource,
60
+ configure?: (clip: ClipBuilder) => ClipBuilder
61
+ ): ProcessingBuilder {
62
+ const clip = configure
63
+ ? configure(new ClipBuilder({ source })).toClip()
64
+ : { source };
65
+
66
+ return withSpec({ ...this.spec, clips: [...this.spec.clips, clip] });
67
+ }
68
+
69
+ /** Appends the current clip played backwards. The output is silent. */
70
+ boomerang(): ProcessingBuilder {
71
+ const last = this.lastClip();
72
+
73
+ return withSpec({
74
+ ...this.spec,
75
+ clips: [...this.spec.clips, { ...last, reverse: !last.reverse }],
76
+ removeAudio: true,
77
+ });
78
+ }
79
+
80
+ /** Adds colour filters, applied in order. Accepts presets as well as single filters. */
81
+ filter(...filters: Array<Filter | Filter[]>): ProcessingBuilder {
82
+ return withSpec({
83
+ ...this.spec,
84
+ filters: [...(this.spec.filters ?? []), ...filters.flat()],
85
+ });
86
+ }
87
+
88
+ /** Fits the output inside this box, keeping the aspect ratio and never upscaling. */
89
+ resize(size: { width?: number; height?: number }): ProcessingBuilder {
90
+ const { width, height } = size;
91
+ const hasSize = width !== undefined || height !== undefined;
92
+
93
+ if (!hasSize) {
94
+ throw invalidOptions('resize needs a width, a height or both');
95
+ }
96
+
97
+ const next: ProcessingSpec = { ...this.spec };
98
+
99
+ delete next.width;
100
+ delete next.height;
101
+
102
+ if (width !== undefined) {
103
+ next.width = positive(width, 'resize width');
104
+ }
105
+ if (height !== undefined) {
106
+ next.height = positive(height, 'resize height');
107
+ }
108
+ return withSpec(next);
109
+ }
110
+
111
+ bitrate(bitsPerSecond: number): ProcessingBuilder {
112
+ return withSpec({
113
+ ...this.spec,
114
+ bitrate: positive(bitsPerSecond, 'bitrate'),
115
+ });
116
+ }
117
+
118
+ removeAudio(): ProcessingBuilder {
119
+ return withSpec({ ...this.spec, removeAudio: true });
120
+ }
121
+
122
+ /** The edit as plain data, accepted by `process(spec)` and `ProcessingBuilder.fromSpec`. */
123
+ toSpec(): ProcessingSpec {
124
+ return copySpec(this.spec);
125
+ }
126
+
127
+ /**
128
+ * A frame of the first clip with the builder's filters, for previews.
129
+ * `time` is in seconds from the start of that clip. Crops aren't applied.
130
+ */
131
+ async thumbnail(
132
+ time: number = 0,
133
+ options: Omit<ThumbnailOptions, 'filters'> = {}
134
+ ): Promise<Thumbnail> {
135
+ const first = this.spec.clips[0]!;
136
+ const start = first.startTime ?? 0;
137
+
138
+ const end = first.reverse
139
+ ? (first.endTime ?? (await getVideoInfo(first.source)).duration)
140
+ : undefined;
141
+
142
+ const sourceTime = end === undefined ? start + time : end - time;
143
+
144
+ return getThumbnail(first.source, Math.max(start, sourceTime), {
145
+ ...options,
146
+ filters: this.spec.filters,
147
+ });
148
+ }
149
+
150
+ /** Runs the edit. */
151
+ export(options?: JobOptions): Promise<ProcessResult> {
152
+ return process(this.spec, options);
153
+ }
154
+
155
+ private lastClip(): Segment {
156
+ return this.spec.clips[this.spec.clips.length - 1]!;
157
+ }
158
+
159
+ private updateLastClip(
160
+ update: (clip: Segment) => Segment
161
+ ): ProcessingBuilder {
162
+ const clips = [...this.spec.clips];
163
+ clips[clips.length - 1] = update(this.lastClip());
164
+
165
+ return withSpec({ ...this.spec, clips });
166
+ }
167
+ }
168
+
169
+ /** The clip methods, for configuring a clip passed to `append`. */
170
+ export class ClipBuilder {
171
+ private readonly clip: Segment;
172
+
173
+ constructor(clip: Segment) {
174
+ this.clip = clip;
175
+ }
176
+
177
+ trim(startTime: number, endTime?: number): ClipBuilder {
178
+ return new ClipBuilder(narrowRange(this.clip, startTime, endTime));
179
+ }
180
+
181
+ crop(rect: CropRect): ClipBuilder {
182
+ return new ClipBuilder(narrowCrop(this.clip, rect));
183
+ }
184
+
185
+ reverse(): ClipBuilder {
186
+ return new ClipBuilder({ ...this.clip, reverse: !this.clip.reverse });
187
+ }
188
+
189
+ toClip(): Segment {
190
+ return { ...this.clip };
191
+ }
192
+ }
193
+
194
+ function withSpec(spec: ProcessingSpec): ProcessingBuilder {
195
+ const builder = Object.create(
196
+ ProcessingBuilder.prototype
197
+ ) as ProcessingBuilder;
198
+
199
+ Object.defineProperty(builder, 'spec', { value: spec });
200
+
201
+ return builder;
202
+ }
203
+
204
+ function narrowRange(
205
+ clip: Segment,
206
+ startTime: number,
207
+ endTime?: number
208
+ ): Segment {
209
+ nonNegative(startTime, 'trim start');
210
+
211
+ if (endTime !== undefined) {
212
+ nonNegative(endTime, 'trim end');
213
+
214
+ if (endTime <= startTime) {
215
+ throw invalidOptions('trim end must be after its start');
216
+ }
217
+ }
218
+
219
+ const base = clip.startTime ?? 0;
220
+ const start = base + startTime;
221
+ const end = endTime === undefined ? clip.endTime : base + endTime;
222
+
223
+ if (clip.endTime !== undefined) {
224
+ const length = clip.endTime - base;
225
+ const isInsideCurrentRange =
226
+ start < clip.endTime && (end ?? 0) <= clip.endTime;
227
+
228
+ if (!isInsideCurrentRange) {
229
+ throw invalidOptions(
230
+ `trim goes past the clip's current ${length}s range`
231
+ );
232
+ }
233
+ }
234
+ return { ...clip, startTime: start, endTime: end };
235
+ }
236
+
237
+ function narrowCrop(clip: Segment, rect: CropRect): Segment {
238
+ nonNegative(rect.x, 'crop x');
239
+ nonNegative(rect.y, 'crop y');
240
+ positive(rect.width, 'crop width');
241
+ positive(rect.height, 'crop height');
242
+
243
+ const previous = clip.crop;
244
+
245
+ if (!previous) {
246
+ return { ...clip, crop: { ...rect } };
247
+ }
248
+
249
+ const isInsidePrevious =
250
+ rect.x + rect.width <= previous.width &&
251
+ rect.y + rect.height <= previous.height;
252
+
253
+ if (!isInsidePrevious) {
254
+ throw invalidOptions(
255
+ `crop goes outside the clip's current ${previous.width}x${previous.height} crop`
256
+ );
257
+ }
258
+ return {
259
+ ...clip,
260
+ crop: {
261
+ x: previous.x + rect.x,
262
+ y: previous.y + rect.y,
263
+ width: rect.width,
264
+ height: rect.height,
265
+ },
266
+ };
267
+ }
268
+
269
+ function copySpec(spec: ProcessingSpec): ProcessingSpec {
270
+ const copy: ProcessingSpec = {
271
+ ...spec,
272
+ clips: spec.clips.map((clip) =>
273
+ clip.crop ? { ...clip, crop: { ...clip.crop } } : { ...clip }
274
+ ),
275
+ };
276
+
277
+ if (spec.filters) {
278
+ copy.filters = spec.filters.map((filter) => ({ ...filter }));
279
+ }
280
+
281
+ return copy;
282
+ }
283
+
284
+ function nonNegative(value: number, name: string) {
285
+ const isValid =
286
+ typeof value === 'number' && Number.isFinite(value) && value >= 0;
287
+
288
+ if (!isValid) {
289
+ throw invalidOptions(`${name} must be a finite number >= 0`);
290
+ }
291
+ }
292
+
293
+ function positive(value: number, name: string): number {
294
+ const isValid =
295
+ typeof value === 'number' && Number.isFinite(value) && value > 0;
296
+
297
+ if (!isValid) {
298
+ throw invalidOptions(`${name} must be a number > 0`);
299
+ }
300
+
301
+ return value;
302
+ }
@@ -9,7 +9,9 @@ import {
9
9
  } from './validation';
10
10
  import type {
11
11
  CropRect,
12
+ JobOptions,
12
13
  ProcessOptions,
14
+ ProcessingSpec,
13
15
  ProcessResult,
14
16
  Segment,
15
17
  Thumbnail,
@@ -53,10 +55,26 @@ export async function getThumbnail(
53
55
  }
54
56
 
55
57
  /** Runs segments back to back into a single output. Every other operation is built on this. */
56
- export async function process(
58
+ export function process(
59
+ spec: ProcessingSpec,
60
+ options?: JobOptions
61
+ ): Promise<ProcessResult>;
62
+ export function process(
57
63
  segments: Segment[],
64
+ options?: ProcessOptions
65
+ ): Promise<ProcessResult>;
66
+ export async function process(
67
+ input: ProcessingSpec | Segment[],
58
68
  options: ProcessOptions = {}
59
69
  ): Promise<ProcessResult> {
70
+ if (!Array.isArray(input)) {
71
+ const { clips, ...output } = input ?? {};
72
+ if (!Array.isArray(clips)) {
73
+ throw invalidOptions('spec.clips must be an array of clips');
74
+ }
75
+ return process(clips, { ...output, ...options });
76
+ }
77
+ const segments = input;
60
78
  if (segments.length === 0) {
61
79
  throw invalidOptions('at least one segment is required');
62
80
  }