react-native-video-processing 3.0.0-beta.1 → 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.
- package/README.md +56 -5
- package/android/src/main/java/com/shahenlibrary/videoprocessing/ColorMatrixEffect.kt +114 -0
- package/android/src/main/java/com/shahenlibrary/videoprocessing/ProcessPlan.kt +9 -0
- package/android/src/main/java/com/shahenlibrary/videoprocessing/ThumbnailGenerator.kt +25 -3
- package/android/src/main/java/com/shahenlibrary/videoprocessing/VideoProcessingModule.kt +1 -0
- package/android/src/main/java/com/shahenlibrary/videoprocessing/VideoProcessor.kt +10 -3
- package/ios/ColorMatrix.swift +36 -0
- package/ios/FrameRenderer.swift +11 -6
- package/ios/ProcessPlan.swift +5 -1
- package/ios/ThumbnailGenerator.swift +18 -1
- package/ios/VideoExporter.swift +1 -1
- package/lib/module/ProcessingBuilder.js +271 -0
- package/lib/module/ProcessingBuilder.js.map +1 -0
- package/lib/module/VideoProcessing.js +16 -1
- package/lib/module/VideoProcessing.js.map +1 -1
- package/lib/module/filters.js +142 -0
- package/lib/module/filters.js.map +1 -0
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/validation.js +18 -0
- package/lib/module/validation.js.map +1 -1
- package/lib/typescript/src/ProcessingBuilder.d.ts +56 -0
- package/lib/typescript/src/ProcessingBuilder.d.ts.map +1 -0
- package/lib/typescript/src/VideoProcessing.d.ts +2 -1
- package/lib/typescript/src/VideoProcessing.d.ts.map +1 -1
- package/lib/typescript/src/filters.d.ts +58 -0
- package/lib/typescript/src/filters.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +2 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/types.d.ts +37 -0
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/lib/typescript/src/validation.d.ts +6 -1
- package/lib/typescript/src/validation.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/ProcessingBuilder.ts +302 -0
- package/src/VideoProcessing.ts +19 -1
- package/src/filters.ts +195 -0
- package/src/index.tsx +2 -0
- package/src/types.ts +25 -0
- package/src/validation.ts +28 -2
package/src/filters.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { invalidOptions } from './errors';
|
|
2
|
+
import type { Filter } from './types';
|
|
3
|
+
|
|
4
|
+
/** Ready-made looks. They are plain filter lists, so they can be extended or combined. */
|
|
5
|
+
export const presets = {
|
|
6
|
+
vintage: [
|
|
7
|
+
{ type: 'sepia', amount: 0.35 },
|
|
8
|
+
{ type: 'contrast', amount: -0.1 },
|
|
9
|
+
{ type: 'saturation', amount: -0.2 },
|
|
10
|
+
{ type: 'brightness', amount: 0.05 },
|
|
11
|
+
],
|
|
12
|
+
noir: [{ type: 'grayscale' }, { type: 'contrast', amount: 0.35 }],
|
|
13
|
+
fade: [
|
|
14
|
+
{ type: 'contrast', amount: -0.25 },
|
|
15
|
+
{ type: 'brightness', amount: 0.05 },
|
|
16
|
+
{ type: 'saturation', amount: -0.15 },
|
|
17
|
+
],
|
|
18
|
+
vivid: [
|
|
19
|
+
{ type: 'saturation', amount: 0.35 },
|
|
20
|
+
{ type: 'contrast', amount: 0.15 },
|
|
21
|
+
],
|
|
22
|
+
warm: [
|
|
23
|
+
{ type: 'sepia', amount: 0.2 },
|
|
24
|
+
{ type: 'saturation', amount: 0.15 },
|
|
25
|
+
{ type: 'brightness', amount: 0.03 },
|
|
26
|
+
],
|
|
27
|
+
} satisfies Record<string, Filter[]>;
|
|
28
|
+
|
|
29
|
+
const IDENTITY: Affine = [
|
|
30
|
+
[1, 0, 0, 0],
|
|
31
|
+
[0, 1, 0, 0],
|
|
32
|
+
[0, 0, 1, 0],
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Combines filters, in order, into one colour matrix: 4 rows (R, G, B, A) of
|
|
37
|
+
* 5 numbers, row-major, where the fifth number is an offset in 0–1 units.
|
|
38
|
+
* Values are gamma-encoded RGB, as stored in the video, like CSS filters.
|
|
39
|
+
*/
|
|
40
|
+
export function filterMatrix(filters: Filter[]): number[] {
|
|
41
|
+
const affine = filters
|
|
42
|
+
.map((filter, index) => filterAffine(filter, `filters[${index}]`))
|
|
43
|
+
.reduce(compose, IDENTITY);
|
|
44
|
+
return [
|
|
45
|
+
...affine[0].slice(0, 3),
|
|
46
|
+
0,
|
|
47
|
+
affine[0][3],
|
|
48
|
+
...affine[1].slice(0, 3),
|
|
49
|
+
0,
|
|
50
|
+
affine[1][3],
|
|
51
|
+
...affine[2].slice(0, 3),
|
|
52
|
+
0,
|
|
53
|
+
affine[2][3],
|
|
54
|
+
0,
|
|
55
|
+
0,
|
|
56
|
+
0,
|
|
57
|
+
1,
|
|
58
|
+
0,
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function filterAffine(filter: Filter, label: string): Affine {
|
|
63
|
+
switch (filter.type) {
|
|
64
|
+
case 'brightness':
|
|
65
|
+
return scale(1 + adjustment(filter.amount, label));
|
|
66
|
+
case 'contrast':
|
|
67
|
+
return contrast(1 + adjustment(filter.amount, label));
|
|
68
|
+
case 'saturation':
|
|
69
|
+
return saturate(1 + adjustment(filter.amount, label));
|
|
70
|
+
case 'hue':
|
|
71
|
+
return hueRotate(finite(filter.degrees, `${label}.degrees`));
|
|
72
|
+
case 'grayscale':
|
|
73
|
+
return saturate(1 - strength(filter.amount, label));
|
|
74
|
+
case 'sepia':
|
|
75
|
+
return sepia(strength(filter.amount, label));
|
|
76
|
+
case 'invert':
|
|
77
|
+
return invert(strength(filter.amount, label));
|
|
78
|
+
default:
|
|
79
|
+
throw invalidOptions(
|
|
80
|
+
`${label}.type must be one of brightness, contrast, saturation, hue, grayscale, sepia or invert`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Applies `first`, then `second`. */
|
|
86
|
+
function compose(first: Affine, second: Affine): Affine {
|
|
87
|
+
return second.map((row) => {
|
|
88
|
+
const column = (j: number) =>
|
|
89
|
+
row[0]! * first[0]![j]! +
|
|
90
|
+
row[1]! * first[1]![j]! +
|
|
91
|
+
row[2]! * first[2]![j]!;
|
|
92
|
+
return [column(0), column(1), column(2), column(3) + row[3]!];
|
|
93
|
+
}) as Affine;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function scale(factor: number): Affine {
|
|
97
|
+
return [
|
|
98
|
+
[factor, 0, 0, 0],
|
|
99
|
+
[0, factor, 0, 0],
|
|
100
|
+
[0, 0, factor, 0],
|
|
101
|
+
];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function contrast(factor: number): Affine {
|
|
105
|
+
const offset = 0.5 * (1 - factor);
|
|
106
|
+
return [
|
|
107
|
+
[factor, 0, 0, offset],
|
|
108
|
+
[0, factor, 0, offset],
|
|
109
|
+
[0, 0, factor, offset],
|
|
110
|
+
];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function saturate(s: number): Affine {
|
|
114
|
+
return [
|
|
115
|
+
[0.213 + 0.787 * s, 0.715 - 0.715 * s, 0.072 - 0.072 * s, 0],
|
|
116
|
+
[0.213 - 0.213 * s, 0.715 + 0.285 * s, 0.072 - 0.072 * s, 0],
|
|
117
|
+
[0.213 - 0.213 * s, 0.715 - 0.715 * s, 0.072 + 0.928 * s, 0],
|
|
118
|
+
];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function hueRotate(degrees: number): Affine {
|
|
122
|
+
const radians = (degrees * Math.PI) / 180;
|
|
123
|
+
const cos = Math.cos(radians);
|
|
124
|
+
const sin = Math.sin(radians);
|
|
125
|
+
return [
|
|
126
|
+
[
|
|
127
|
+
0.213 + cos * 0.787 - sin * 0.213,
|
|
128
|
+
0.715 - cos * 0.715 - sin * 0.715,
|
|
129
|
+
0.072 - cos * 0.072 + sin * 0.928,
|
|
130
|
+
0,
|
|
131
|
+
],
|
|
132
|
+
[
|
|
133
|
+
0.213 - cos * 0.213 + sin * 0.143,
|
|
134
|
+
0.715 + cos * 0.285 + sin * 0.14,
|
|
135
|
+
0.072 - cos * 0.072 - sin * 0.283,
|
|
136
|
+
0,
|
|
137
|
+
],
|
|
138
|
+
[
|
|
139
|
+
0.213 - cos * 0.213 - sin * 0.787,
|
|
140
|
+
0.715 - cos * 0.715 + sin * 0.715,
|
|
141
|
+
0.072 + cos * 0.928 + sin * 0.072,
|
|
142
|
+
0,
|
|
143
|
+
],
|
|
144
|
+
];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function sepia(amount: number): Affine {
|
|
148
|
+
const rest = 1 - amount;
|
|
149
|
+
return [
|
|
150
|
+
[0.393 + 0.607 * rest, 0.769 - 0.769 * rest, 0.189 - 0.189 * rest, 0],
|
|
151
|
+
[0.349 - 0.349 * rest, 0.686 + 0.314 * rest, 0.168 - 0.168 * rest, 0],
|
|
152
|
+
[0.272 - 0.272 * rest, 0.534 - 0.534 * rest, 0.131 + 0.869 * rest, 0],
|
|
153
|
+
];
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function invert(amount: number): Affine {
|
|
157
|
+
const factor = 1 - 2 * amount;
|
|
158
|
+
return [
|
|
159
|
+
[factor, 0, 0, amount],
|
|
160
|
+
[0, factor, 0, amount],
|
|
161
|
+
[0, 0, factor, amount],
|
|
162
|
+
];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function adjustment(amount: number, label: string): number {
|
|
166
|
+
const value = finite(amount, `${label}.amount`);
|
|
167
|
+
if (value < -1 || value > 1) {
|
|
168
|
+
throw invalidOptions(`${label}.amount must be between -1 and 1`);
|
|
169
|
+
}
|
|
170
|
+
return value;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function strength(amount: number | undefined, label: string): number {
|
|
174
|
+
const value = finite(amount ?? 1, `${label}.amount`);
|
|
175
|
+
if (value < 0 || value > 1) {
|
|
176
|
+
throw invalidOptions(`${label}.amount must be between 0 and 1`);
|
|
177
|
+
}
|
|
178
|
+
return value;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function finite(value: number, name: string): number {
|
|
182
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
183
|
+
throw invalidOptions(`${name} must be a finite number`);
|
|
184
|
+
}
|
|
185
|
+
return value;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// ---------------------------------------------------------------------------
|
|
189
|
+
// Types
|
|
190
|
+
// ---------------------------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
/** Three rows (R, G, B) of [r, g, b, offset]. */
|
|
193
|
+
type Affine = [AffineRow, AffineRow, AffineRow];
|
|
194
|
+
|
|
195
|
+
type AffineRow = [number, number, number, number];
|
package/src/index.tsx
CHANGED
|
@@ -10,6 +10,8 @@ export {
|
|
|
10
10
|
reverse,
|
|
11
11
|
trim,
|
|
12
12
|
} from './VideoProcessing';
|
|
13
|
+
export { filterMatrix, presets } from './filters';
|
|
14
|
+
export { ClipBuilder, ProcessingBuilder } from './ProcessingBuilder';
|
|
13
15
|
export { Trimmer, type TrimmerProps } from './Trimmer/Trimmer';
|
|
14
16
|
export type { TrimRange } from './Trimmer/trimmerMath';
|
|
15
17
|
export { VideoProcessingError } from './errors';
|
package/src/types.ts
CHANGED
|
@@ -32,6 +32,8 @@ export type ThumbnailOptions = {
|
|
|
32
32
|
format?: ThumbnailFormat;
|
|
33
33
|
/** JPEG quality between 0 and 1. Defaults to 0.8. */
|
|
34
34
|
quality?: number;
|
|
35
|
+
/** Applied to the thumbnail exactly as `process` applies them to video, for filter previews. */
|
|
36
|
+
filters?: Filter[];
|
|
35
37
|
};
|
|
36
38
|
|
|
37
39
|
export type ThumbnailTimes =
|
|
@@ -59,6 +61,19 @@ export type CropRect = {
|
|
|
59
61
|
height: number;
|
|
60
62
|
};
|
|
61
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Colour filters with CSS filter semantics. `amount` 0 leaves the video
|
|
66
|
+
* unchanged; adjustments run from -1 to 1, the others from 0 to 1.
|
|
67
|
+
*/
|
|
68
|
+
export type Filter =
|
|
69
|
+
| { type: 'brightness'; amount: number }
|
|
70
|
+
| { type: 'contrast'; amount: number }
|
|
71
|
+
| { type: 'saturation'; amount: number }
|
|
72
|
+
| { type: 'hue'; degrees: number }
|
|
73
|
+
| { type: 'grayscale'; amount?: number }
|
|
74
|
+
| { type: 'sepia'; amount?: number }
|
|
75
|
+
| { type: 'invert'; amount?: number };
|
|
76
|
+
|
|
62
77
|
export type Segment = TimeRange & {
|
|
63
78
|
source: VideoSource;
|
|
64
79
|
crop?: CropRect;
|
|
@@ -81,6 +96,8 @@ export type OutputOptions = {
|
|
|
81
96
|
*/
|
|
82
97
|
bitrate?: number;
|
|
83
98
|
removeAudio?: boolean;
|
|
99
|
+
/** Colour filters, applied in order to every segment after cropping. */
|
|
100
|
+
filters?: Filter[];
|
|
84
101
|
};
|
|
85
102
|
|
|
86
103
|
export type JobOptions = {
|
|
@@ -92,6 +109,14 @@ export type JobOptions = {
|
|
|
92
109
|
|
|
93
110
|
export type ProcessOptions = OutputOptions & JobOptions;
|
|
94
111
|
|
|
112
|
+
/**
|
|
113
|
+
* A whole edit as plain data: the clips to play back to back plus the output
|
|
114
|
+
* options. JSON-serialisable when every source is a string or `{ uri }`.
|
|
115
|
+
*/
|
|
116
|
+
export type ProcessingSpec = OutputOptions & {
|
|
117
|
+
clips: Segment[];
|
|
118
|
+
};
|
|
119
|
+
|
|
95
120
|
export type ProcessResult = {
|
|
96
121
|
/** `file://` URI of an H.264/AAC MP4 in the app's cache directory. */
|
|
97
122
|
uri: string;
|
package/src/validation.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { invalidOptions } from './errors';
|
|
2
|
+
import { filterMatrix } from './filters';
|
|
2
3
|
import { resolveSource } from './source';
|
|
3
4
|
import type {
|
|
4
5
|
CropRect,
|
|
6
|
+
Filter,
|
|
5
7
|
OutputOptions,
|
|
6
8
|
Segment,
|
|
7
9
|
ThumbnailOptions,
|
|
@@ -41,13 +43,17 @@ export function toNativeOutput(options: OutputOptions = {}): NativeOutput {
|
|
|
41
43
|
if (options.removeAudio) {
|
|
42
44
|
output.removeAudio = true;
|
|
43
45
|
}
|
|
46
|
+
const colorMatrix = toColorMatrix(options.filters);
|
|
47
|
+
if (colorMatrix) {
|
|
48
|
+
output.colorMatrix = colorMatrix;
|
|
49
|
+
}
|
|
44
50
|
return output;
|
|
45
51
|
}
|
|
46
52
|
|
|
47
53
|
export function toNativeThumbnailOptions(
|
|
48
54
|
options: ThumbnailOptions = {}
|
|
49
|
-
):
|
|
50
|
-
const native:
|
|
55
|
+
): NativeThumbnailOptions {
|
|
56
|
+
const native: NativeThumbnailOptions = { format: options.format ?? 'jpeg' };
|
|
51
57
|
|
|
52
58
|
if (native.format !== 'jpeg' && native.format !== 'png') {
|
|
53
59
|
throw invalidOptions(`format must be 'jpeg' or 'png'`);
|
|
@@ -65,6 +71,10 @@ export function toNativeThumbnailOptions(
|
|
|
65
71
|
throw invalidOptions('quality must be between 0 and 1');
|
|
66
72
|
}
|
|
67
73
|
native.quality = quality;
|
|
74
|
+
const colorMatrix = toColorMatrix(options.filters);
|
|
75
|
+
if (colorMatrix) {
|
|
76
|
+
native.colorMatrix = colorMatrix;
|
|
77
|
+
}
|
|
68
78
|
return native;
|
|
69
79
|
}
|
|
70
80
|
|
|
@@ -102,6 +112,16 @@ export function evenlySpacedTimes(
|
|
|
102
112
|
);
|
|
103
113
|
}
|
|
104
114
|
|
|
115
|
+
function toColorMatrix(filters: Filter[] | undefined): number[] | undefined {
|
|
116
|
+
if (filters === undefined) {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
if (!Array.isArray(filters)) {
|
|
120
|
+
throw invalidOptions('filters must be an array');
|
|
121
|
+
}
|
|
122
|
+
return filters.length > 0 ? filterMatrix(filters) : undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
105
125
|
function toNativeTimeRange(range: TimeRange, label: string): TimeRange {
|
|
106
126
|
const native: TimeRange = {};
|
|
107
127
|
|
|
@@ -165,4 +185,10 @@ export type NativeOutput = {
|
|
|
165
185
|
height?: number;
|
|
166
186
|
bitrate?: number;
|
|
167
187
|
removeAudio?: boolean;
|
|
188
|
+
/** 4x5 row-major; see filterMatrix. */
|
|
189
|
+
colorMatrix?: number[];
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
export type NativeThumbnailOptions = Omit<ThumbnailOptions, 'filters'> & {
|
|
193
|
+
colorMatrix?: number[];
|
|
168
194
|
};
|