@mevdragon/vidfarm-devcli 0.2.0 → 0.2.1

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.
@@ -0,0 +1,242 @@
1
+ import sharp from "sharp";
2
+ export async function normalizeToPortraitFrame(input, target = { width: 1080, height: 1920 }) {
3
+ const buffer = Buffer.isBuffer(input) ? input : Buffer.from(input);
4
+ const oriented = sharp(buffer, { density: 144 }).rotate();
5
+ const trimmed = await trimFlatBorders(oriented);
6
+ const targetAspect = target.width / target.height;
7
+ // If the model already returned a near-native vertical frame, avoid the
8
+ // activity crop pass because it can misread low-detail edges as padding.
9
+ const shouldPreserveFraming = await isCloseToAspect(trimmed, targetAspect, 0.025);
10
+ const cropped = shouldPreserveFraming ? trimmed : await cropToActiveImageRegion(trimmed);
11
+ const portraitCrop = await cropToPortraitAspect(cropped, targetAspect);
12
+ const output = await portraitCrop
13
+ .resize(target.width, target.height, {
14
+ fit: "cover",
15
+ position: sharp.strategy.attention
16
+ })
17
+ .png()
18
+ .toBuffer();
19
+ const exact = await ensureExactPixelSize(output, target);
20
+ return {
21
+ bytes: exact,
22
+ contentType: "image/png",
23
+ width: target.width,
24
+ height: target.height
25
+ };
26
+ }
27
+ async function isCloseToAspect(image, targetAspect, tolerance) {
28
+ const metadata = await image.metadata();
29
+ const width = metadata.width ?? 0;
30
+ const height = metadata.height ?? 0;
31
+ if (!width || !height) {
32
+ return false;
33
+ }
34
+ return Math.abs(width / height - targetAspect) <= tolerance;
35
+ }
36
+ async function trimFlatBorders(image) {
37
+ const metadata = await image.metadata();
38
+ const sourceWidth = metadata.width ?? 0;
39
+ const sourceHeight = metadata.height ?? 0;
40
+ if (!sourceWidth || !sourceHeight || sourceWidth < 3 || sourceHeight < 3) {
41
+ return image;
42
+ }
43
+ const { data, info } = await image
44
+ .clone()
45
+ .trim({ threshold: 16 })
46
+ .png()
47
+ .toBuffer({ resolveWithObject: true });
48
+ const widthRatio = info.width / sourceWidth;
49
+ const heightRatio = info.height / sourceHeight;
50
+ if (widthRatio > 0.985 && heightRatio > 0.985) {
51
+ return image;
52
+ }
53
+ return sharp(data).rotate();
54
+ }
55
+ async function cropToActiveImageRegion(image) {
56
+ const metadata = await image.metadata();
57
+ const sourceWidth = metadata.width ?? 0;
58
+ const sourceHeight = metadata.height ?? 0;
59
+ if (!sourceWidth || !sourceHeight) {
60
+ return image;
61
+ }
62
+ const sampleWidth = 96;
63
+ const sampleHeight = 170;
64
+ const sample = await image
65
+ .clone()
66
+ .resize(sampleWidth, sampleHeight, { fit: "fill" })
67
+ .grayscale()
68
+ .raw()
69
+ .toBuffer();
70
+ const bounds = detectActiveBounds(sample, sampleWidth, sampleHeight);
71
+ if (!bounds) {
72
+ return image;
73
+ }
74
+ const left = Math.max(0, Math.floor((bounds.left / sampleWidth) * sourceWidth));
75
+ const top = Math.max(0, Math.floor((bounds.top / sampleHeight) * sourceHeight));
76
+ const width = Math.min(sourceWidth - left, Math.max(1, Math.ceil((bounds.width / sampleWidth) * sourceWidth)));
77
+ const height = Math.min(sourceHeight - top, Math.max(1, Math.ceil((bounds.height / sampleHeight) * sourceHeight)));
78
+ return image.extract({ left, top, width, height });
79
+ }
80
+ async function cropToPortraitAspect(image, targetAspect) {
81
+ const metadata = await image.metadata();
82
+ const sourceWidth = metadata.width ?? 0;
83
+ const sourceHeight = metadata.height ?? 0;
84
+ if (!sourceWidth || !sourceHeight) {
85
+ return image;
86
+ }
87
+ const sourceAspect = sourceWidth / sourceHeight;
88
+ if (Math.abs(sourceAspect - targetAspect) < 0.015) {
89
+ return image;
90
+ }
91
+ const focus = await detectFocusPoint(image, sourceWidth, sourceHeight);
92
+ if (sourceAspect > targetAspect) {
93
+ const cropWidth = Math.max(1, Math.min(sourceWidth, Math.round(sourceHeight * targetAspect)));
94
+ const left = clamp(Math.round(focus.x - cropWidth / 2), 0, sourceWidth - cropWidth);
95
+ return image.extract({ left, top: 0, width: cropWidth, height: sourceHeight });
96
+ }
97
+ const cropHeight = Math.max(1, Math.min(sourceHeight, Math.round(sourceWidth / targetAspect)));
98
+ const top = clamp(Math.round(focus.y - cropHeight / 2), 0, sourceHeight - cropHeight);
99
+ return image.extract({ left: 0, top, width: sourceWidth, height: cropHeight });
100
+ }
101
+ function detectActiveBounds(sample, width, height) {
102
+ const rowActivity = new Array(height).fill(0);
103
+ const colActivity = new Array(width).fill(0);
104
+ for (let y = 0; y < height; y += 1) {
105
+ for (let x = 0; x < width; x += 1) {
106
+ const index = y * width + x;
107
+ const current = sample[index] ?? 0;
108
+ const left = x > 0 ? sample[index - 1] ?? current : current;
109
+ const up = y > 0 ? sample[index - width] ?? current : current;
110
+ const right = x < width - 1 ? sample[index + 1] ?? current : current;
111
+ const down = y < height - 1 ? sample[index + width] ?? current : current;
112
+ const energy = Math.abs(current - left) +
113
+ Math.abs(current - right) +
114
+ Math.abs(current - up) +
115
+ Math.abs(current - down);
116
+ rowActivity[y] += energy;
117
+ colActivity[x] += energy;
118
+ }
119
+ }
120
+ const smoothedRows = smoothSeries(rowActivity.map((value) => value / width), 9);
121
+ const smoothedCols = smoothSeries(colActivity.map((value) => value / height), 7);
122
+ const rowBounds = findActiveRange(smoothedRows, Math.round(height * 0.18));
123
+ const colBounds = findActiveRange(smoothedCols, Math.round(width * 0.16));
124
+ if (!rowBounds || !colBounds) {
125
+ return null;
126
+ }
127
+ const topMargin = rowBounds.start / height;
128
+ const bottomMargin = (height - rowBounds.end - 1) / height;
129
+ const leftMargin = colBounds.start / width;
130
+ const rightMargin = (width - colBounds.end - 1) / width;
131
+ const croppedWidth = colBounds.end - colBounds.start + 1;
132
+ const croppedHeight = rowBounds.end - rowBounds.start + 1;
133
+ const croppedAreaRatio = (croppedWidth * croppedHeight) / (width * height);
134
+ const significantMargins = topMargin > 0.11 || bottomMargin > 0.11 || leftMargin > 0.08 || rightMargin > 0.08;
135
+ if (!significantMargins && croppedAreaRatio > 0.9) {
136
+ return null;
137
+ }
138
+ return {
139
+ left: colBounds.start,
140
+ top: rowBounds.start,
141
+ width: croppedWidth,
142
+ height: croppedHeight
143
+ };
144
+ }
145
+ async function detectFocusPoint(image, sourceWidth, sourceHeight) {
146
+ const sampleWidth = 120;
147
+ const sampleHeight = Math.max(1, Math.round((sourceHeight / Math.max(sourceWidth, 1)) * sampleWidth));
148
+ const sample = await image
149
+ .clone()
150
+ .resize(sampleWidth, sampleHeight, { fit: "fill" })
151
+ .grayscale()
152
+ .raw()
153
+ .toBuffer();
154
+ let weightedX = 0;
155
+ let weightedY = 0;
156
+ let totalWeight = 0;
157
+ for (let y = 0; y < sampleHeight; y += 1) {
158
+ for (let x = 0; x < sampleWidth; x += 1) {
159
+ const index = y * sampleWidth + x;
160
+ const current = sample[index] ?? 0;
161
+ const left = x > 0 ? sample[index - 1] ?? current : current;
162
+ const up = y > 0 ? sample[index - sampleWidth] ?? current : current;
163
+ const right = x < sampleWidth - 1 ? sample[index + 1] ?? current : current;
164
+ const down = y < sampleHeight - 1 ? sample[index + sampleWidth] ?? current : current;
165
+ const energy = Math.abs(current - left) +
166
+ Math.abs(current - right) +
167
+ Math.abs(current - up) +
168
+ Math.abs(current - down);
169
+ const centerBiasX = 1 - Math.abs((x + 0.5) / sampleWidth - 0.5) * 0.45;
170
+ const centerBiasY = 1 - Math.abs((y + 0.5) / sampleHeight - 0.5) * 0.35;
171
+ const weight = Math.max(energy, 1) * centerBiasX * centerBiasY;
172
+ weightedX += (x + 0.5) * weight;
173
+ weightedY += (y + 0.5) * weight;
174
+ totalWeight += weight;
175
+ }
176
+ }
177
+ if (totalWeight <= 0) {
178
+ return { x: sourceWidth / 2, y: sourceHeight / 2 };
179
+ }
180
+ return {
181
+ x: (weightedX / totalWeight / sampleWidth) * sourceWidth,
182
+ y: (weightedY / totalWeight / sampleHeight) * sourceHeight
183
+ };
184
+ }
185
+ function findActiveRange(values, minSpan) {
186
+ const mean = values.reduce((sum, value) => sum + value, 0) / Math.max(values.length, 1);
187
+ const max = values.reduce((best, value) => Math.max(best, value), 0);
188
+ if (max <= 0) {
189
+ return null;
190
+ }
191
+ const threshold = mean + (max - mean) * 0.18;
192
+ let start = 0;
193
+ while (start < values.length && values[start] < threshold) {
194
+ start += 1;
195
+ }
196
+ let end = values.length - 1;
197
+ while (end >= 0 && values[end] < threshold) {
198
+ end -= 1;
199
+ }
200
+ if (start >= end) {
201
+ return null;
202
+ }
203
+ const desiredSpan = Math.max(minSpan, end - start + 1);
204
+ const extra = desiredSpan - (end - start + 1);
205
+ const expandStart = Math.floor(extra / 2);
206
+ const expandEnd = extra - expandStart;
207
+ start = Math.max(0, start - expandStart - 4);
208
+ end = Math.min(values.length - 1, end + expandEnd + 4);
209
+ return { start, end };
210
+ }
211
+ function smoothSeries(values, radius) {
212
+ return values.map((_, index) => {
213
+ let total = 0;
214
+ let count = 0;
215
+ for (let offset = -radius; offset <= radius; offset += 1) {
216
+ const value = values[index + offset];
217
+ if (value === undefined) {
218
+ continue;
219
+ }
220
+ total += value;
221
+ count += 1;
222
+ }
223
+ return total / Math.max(count, 1);
224
+ });
225
+ }
226
+ function clamp(value, min, max) {
227
+ return Math.min(Math.max(value, min), max);
228
+ }
229
+ async function ensureExactPixelSize(input, target) {
230
+ const metadata = await sharp(input).metadata();
231
+ if (metadata.width === target.width && metadata.height === target.height) {
232
+ return input;
233
+ }
234
+ const exactCrop = await cropToPortraitAspect(sharp(input), target.width / target.height);
235
+ return exactCrop
236
+ .resize(target.width, target.height, {
237
+ fit: "cover",
238
+ position: sharp.strategy.attention
239
+ })
240
+ .png()
241
+ .toBuffer();
242
+ }
@@ -0,0 +1,3 @@
1
+ import { registerRoot } from "remotion";
2
+ import { RemotionRoot } from "./Root.js";
3
+ registerRoot(RemotionRoot);
@@ -0,0 +1,3 @@
1
+ export function defineTemplate(definition) {
2
+ return definition;
3
+ }