@gehdoc/svg-to-video 0.25.0 → 0.26.0
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/dist/shared/analyzeSvgAnimation.js +54 -0
- package/dist/src/index.js +18 -37
- package/dist/src/mcp.js +10 -29
- package/package.json +3 -2
|
@@ -216,3 +216,57 @@ export const analyzeSvgAnimation = (svgContent, parserOverride) => {
|
|
|
216
216
|
}
|
|
217
217
|
return undefined;
|
|
218
218
|
};
|
|
219
|
+
/**
|
|
220
|
+
* Parses SVG content to extract width and height from width/height attributes or viewBox.
|
|
221
|
+
* Accepts an optional DOMParser override for non-browser environments.
|
|
222
|
+
*/
|
|
223
|
+
export const parseSvgDimensions = (svgContent, parserOverride) => {
|
|
224
|
+
const Parser = parserOverride ||
|
|
225
|
+
(typeof DOMParser !== 'undefined' ? DOMParser : undefined);
|
|
226
|
+
if (!Parser) {
|
|
227
|
+
throw new Error('DOMParser is not available. Provide it via parserOverride.');
|
|
228
|
+
}
|
|
229
|
+
const parser = new Parser();
|
|
230
|
+
const doc = parser.parseFromString(svgContent, 'image/svg+xml');
|
|
231
|
+
const svg = doc.querySelector('svg');
|
|
232
|
+
if (!svg)
|
|
233
|
+
throw new Error('Invalid SVG content');
|
|
234
|
+
let width = parseFloat(svg.getAttribute('width') || '');
|
|
235
|
+
let height = parseFloat(svg.getAttribute('height') || '');
|
|
236
|
+
const viewBox = svg.getAttribute('viewBox');
|
|
237
|
+
let isDimensionsDetected = !(isNaN(width) || isNaN(height));
|
|
238
|
+
if (!isDimensionsDetected && viewBox) {
|
|
239
|
+
const parts = viewBox
|
|
240
|
+
.trim()
|
|
241
|
+
.split(/[\s,]+/)
|
|
242
|
+
.map(parseFloat);
|
|
243
|
+
if (parts.length === 4 && !isNaN(parts[2]) && !isNaN(parts[3])) {
|
|
244
|
+
width = parts[2];
|
|
245
|
+
height = parts[3];
|
|
246
|
+
isDimensionsDetected = true;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (isNaN(width) || isNaN(height) || width <= 0 || height <= 0) {
|
|
250
|
+
width = 1920;
|
|
251
|
+
height = 1080;
|
|
252
|
+
isDimensionsDetected = false;
|
|
253
|
+
}
|
|
254
|
+
return { width, height, isDimensionsDetected };
|
|
255
|
+
};
|
|
256
|
+
/**
|
|
257
|
+
* Calculates aspect ratio category ('square', 'landscape', 'portrait', or 'unknown').
|
|
258
|
+
*/
|
|
259
|
+
export function calculateAspectRatio(width, height, isDimensionsDetected = true) {
|
|
260
|
+
if (!isDimensionsDetected ||
|
|
261
|
+
isNaN(width) ||
|
|
262
|
+
isNaN(height) ||
|
|
263
|
+
width <= 0 ||
|
|
264
|
+
height <= 0) {
|
|
265
|
+
return 'unknown';
|
|
266
|
+
}
|
|
267
|
+
if (width === height)
|
|
268
|
+
return 'square';
|
|
269
|
+
if (width > height)
|
|
270
|
+
return 'landscape';
|
|
271
|
+
return 'portrait';
|
|
272
|
+
}
|
package/dist/src/index.js
CHANGED
|
@@ -4,15 +4,15 @@ import child_process from 'child_process';
|
|
|
4
4
|
import puppeteer from 'puppeteer';
|
|
5
5
|
import { Command } from 'commander';
|
|
6
6
|
import path from 'path';
|
|
7
|
-
import { seekAnimations } from '
|
|
7
|
+
import { seekAnimations } from '@shared/animation-engine.js';
|
|
8
8
|
import { validateOptions } from './utils/validateOptions.js';
|
|
9
|
-
import { analyzeSvgAnimation } from '
|
|
9
|
+
import { analyzeSvgAnimation, parseSvgDimensions, calculateAspectRatio, } from '@shared/analyzeSvgAnimation.js';
|
|
10
10
|
import { formatRegistry } from './formats/registry.js';
|
|
11
11
|
import { pkg } from './utils/packageInfo.js';
|
|
12
12
|
import { JSDOM } from 'jsdom'; // For duration detection in Node environment
|
|
13
13
|
import { Logger } from './utils/logger.js';
|
|
14
14
|
import { trackEvent } from './utils/analytics.js';
|
|
15
|
-
import { ConversionTracker } from '
|
|
15
|
+
import { ConversionTracker } from '@shared/rendererTracking.js';
|
|
16
16
|
const frameFileExtension = 'png';
|
|
17
17
|
async function main() {
|
|
18
18
|
const program = new Command();
|
|
@@ -85,22 +85,24 @@ async function run(svgPath, fps, outDir, options) {
|
|
|
85
85
|
throw logger.fatal(`Input SVG file "${svgPath}" does not exist.`);
|
|
86
86
|
}
|
|
87
87
|
const svg = fs.readFileSync(svgPath, 'utf-8');
|
|
88
|
+
const dom = new JSDOM('');
|
|
89
|
+
const parsedDim = parseSvgDimensions(svg, dom.window.DOMParser);
|
|
90
|
+
const detectedDuration = analyzeSvgAnimation(svg, dom.window.DOMParser);
|
|
88
91
|
let duration = options.duration;
|
|
89
92
|
if (duration === undefined) {
|
|
90
93
|
logger.info('🔍 Duration not provided, attempting to auto-detect...');
|
|
91
|
-
|
|
92
|
-
duration = analyzeSvgAnimation(svg, dom.window.DOMParser);
|
|
94
|
+
duration = detectedDuration;
|
|
93
95
|
if (duration === undefined) {
|
|
94
96
|
throw logger.fatal('Could not detect duration. Please provide a duration using -d or --duration.');
|
|
95
97
|
}
|
|
96
98
|
logger.info(`✅ Auto-detected duration: ${duration}s`);
|
|
97
99
|
}
|
|
100
|
+
const aspectRatio = calculateAspectRatio(parsedDim.width, parsedDim.height, parsedDim.isDimensionsDetected);
|
|
98
101
|
trackEvent('file-load', {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
isDimensionsDetected: false,
|
|
102
|
+
detectedDuration: detectedDuration ?? 0,
|
|
103
|
+
hasAnimation: detectedDuration !== undefined && detectedDuration > 0,
|
|
104
|
+
aspectRatio,
|
|
105
|
+
isDimensionsDetected: parsedDim.isDimensionsDetected,
|
|
104
106
|
});
|
|
105
107
|
const puppeteerArgs = (process.env.PUPPETEER_ARGS || '')
|
|
106
108
|
.split(' ')
|
|
@@ -126,7 +128,7 @@ async function run(svgPath, fps, outDir, options) {
|
|
|
126
128
|
tracker.start();
|
|
127
129
|
try {
|
|
128
130
|
fs.mkdirSync(outDir, { recursive: true });
|
|
129
|
-
await createFrames(svg, fps, totalFrames, padWidth, outDir, puppeteerArgs, options.resolution, options.scale, options.transparent, options.bgColor, logger);
|
|
131
|
+
await createFrames(svg, parsedDim, fps, totalFrames, padWidth, outDir, puppeteerArgs, options.resolution, options.scale, options.transparent, options.bgColor, logger);
|
|
130
132
|
convertToOutput(outputFileName, format, fps, padWidth, options.hold, outDir, options.transparent, options.metadata, logger);
|
|
131
133
|
if (!options.keepFrames) {
|
|
132
134
|
cleanupFrames(totalFrames, padWidth, outDir, logger);
|
|
@@ -149,7 +151,7 @@ async function run(svgPath, fps, outDir, options) {
|
|
|
149
151
|
/**
|
|
150
152
|
* create frame images by rendering the SVG in a headless browser and advancing the animation to the correct timestamp for each frame
|
|
151
153
|
*/
|
|
152
|
-
async function createFrames(svg, fps, totalFrames, padWidth, outDir, puppeteerArgs, resolutionPreset, scaleFactor, transparent, bgColor, logger) {
|
|
154
|
+
async function createFrames(svg, parsedDim, fps, totalFrames, padWidth, outDir, puppeteerArgs, resolutionPreset, scaleFactor, transparent, bgColor, logger) {
|
|
153
155
|
logger.info('📸 Rendering frames with Puppeteer...');
|
|
154
156
|
let width = 0;
|
|
155
157
|
let height = 0;
|
|
@@ -162,32 +164,11 @@ async function createFrames(svg, fps, totalFrames, padWidth, outDir, puppeteerAr
|
|
|
162
164
|
height = 720;
|
|
163
165
|
}
|
|
164
166
|
else if (resolutionPreset === 'original') {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (!svgEl) {
|
|
168
|
-
throw new Error('Invalid SVG: No <svg> root element found.');
|
|
167
|
+
if (!parsedDim.isDimensionsDetected) {
|
|
168
|
+
logger.warn('⚠️ Warning: Could not detect SVG dimensions. Defaulting to 1920x1080.');
|
|
169
169
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
const heightAttr = svgEl.getAttribute('height');
|
|
173
|
-
if (viewBox) {
|
|
174
|
-
const parts = viewBox.trim().split(/[\s,]+/);
|
|
175
|
-
if (parts.length === 4) {
|
|
176
|
-
width = parseFloat(parts[2]);
|
|
177
|
-
height = parseFloat(parts[3]);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
if ((!width || !height) && widthAttr && heightAttr) {
|
|
181
|
-
width = parseFloat(widthAttr);
|
|
182
|
-
height = parseFloat(heightAttr);
|
|
183
|
-
}
|
|
184
|
-
if (!width || !height) {
|
|
185
|
-
logger.warn('⚠️ Warning: Could not detect SVG dimensions. Defaulting to 1280x720.');
|
|
186
|
-
width = 1280;
|
|
187
|
-
height = 720;
|
|
188
|
-
}
|
|
189
|
-
width = Math.round(width * scaleFactor);
|
|
190
|
-
height = Math.round(height * scaleFactor);
|
|
170
|
+
width = Math.round(parsedDim.width * scaleFactor);
|
|
171
|
+
height = Math.round(parsedDim.height * scaleFactor);
|
|
191
172
|
}
|
|
192
173
|
else {
|
|
193
174
|
throw new Error(`Invalid resolution preset: ${resolutionPreset}. Expected '1080p', '720p', or 'original'.`);
|
package/dist/src/mcp.js
CHANGED
|
@@ -8,7 +8,7 @@ import os from 'os';
|
|
|
8
8
|
import { execFileSync } from 'child_process';
|
|
9
9
|
import { fileURLToPath } from 'url';
|
|
10
10
|
import { JSDOM } from 'jsdom';
|
|
11
|
-
import { analyzeSvgAnimation } from '
|
|
11
|
+
import { analyzeSvgAnimation, parseSvgDimensions, calculateAspectRatio, } from '@shared/analyzeSvgAnimation.js';
|
|
12
12
|
import { isLoggerJsonOutput } from './utils/logger.js';
|
|
13
13
|
import { trackEvent } from './utils/analytics.js';
|
|
14
14
|
import { pkg } from './utils/packageInfo.js';
|
|
@@ -138,36 +138,15 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
138
138
|
],
|
|
139
139
|
};
|
|
140
140
|
}
|
|
141
|
-
const dom = new JSDOM(
|
|
141
|
+
const dom = new JSDOM('');
|
|
142
142
|
const duration = analyzeSvgAnimation(svgContent, dom.window.DOMParser);
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
let height;
|
|
146
|
-
if (svgEl) {
|
|
147
|
-
const viewBox = svgEl.getAttribute('viewBox');
|
|
148
|
-
if (viewBox) {
|
|
149
|
-
const parts = viewBox.trim().split(/[\s,]+/);
|
|
150
|
-
if (parts.length === 4) {
|
|
151
|
-
width = parseFloat(parts[2]);
|
|
152
|
-
height = parseFloat(parts[3]);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
if (!width || !height) {
|
|
156
|
-
width = parseFloat(svgEl.getAttribute('width') || '');
|
|
157
|
-
height = parseFloat(svgEl.getAttribute('height') || '');
|
|
158
|
-
}
|
|
159
|
-
}
|
|
143
|
+
const parsedDim = parseSvgDimensions(svgContent, dom.window.DOMParser);
|
|
144
|
+
const aspectRatio = calculateAspectRatio(parsedDim.width, parsedDim.height, parsedDim.isDimensionsDetected);
|
|
160
145
|
trackEvent('file-load', {
|
|
161
|
-
detectedDuration: duration,
|
|
146
|
+
detectedDuration: duration ?? 0,
|
|
162
147
|
hasAnimation: duration !== undefined && duration > 0,
|
|
163
|
-
aspectRatio
|
|
164
|
-
|
|
165
|
-
? 'square'
|
|
166
|
-
: width > height
|
|
167
|
-
? 'landscape'
|
|
168
|
-
: 'portrait'
|
|
169
|
-
: 'unknown',
|
|
170
|
-
isDimensionsDetected: !!(width && height),
|
|
148
|
+
aspectRatio,
|
|
149
|
+
isDimensionsDetected: parsedDim.isDimensionsDetected,
|
|
171
150
|
}, 'mcp');
|
|
172
151
|
return {
|
|
173
152
|
content: [
|
|
@@ -176,7 +155,9 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
176
155
|
text: JSON.stringify({
|
|
177
156
|
hasAnimation: duration !== undefined && duration > 0,
|
|
178
157
|
estimatedDurationSeconds: duration ?? null,
|
|
179
|
-
dimensions:
|
|
158
|
+
dimensions: parsedDim.isDimensionsDetected
|
|
159
|
+
? { width: parsedDim.width, height: parsedDim.height }
|
|
160
|
+
: null,
|
|
180
161
|
hasStyles: svgContent.includes('<style>'),
|
|
181
162
|
}, null, 2),
|
|
182
163
|
},
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gehdoc/svg-to-video",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Professional animated SVG to Video (MP4/WebM/MKV/MOV) and Animated Image (aPNG/GIF) converter featuring frame-accurate rendering, CLI automation, and a serverless Web Studio.",
|
|
6
6
|
"imports": {
|
|
7
|
-
"#shared/*": "./dist/shared/*"
|
|
7
|
+
"#shared/*": "./dist/shared/*",
|
|
8
|
+
"@shared/*": "./dist/shared/*"
|
|
8
9
|
},
|
|
9
10
|
"author": "GehDoc",
|
|
10
11
|
"repository": {
|