@gehdoc/svg-to-video 0.23.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/LICENSE +21 -0
- package/README.md +158 -0
- package/dist/shared/analyzeSvgAnimation.js +218 -0
- package/dist/shared/animation-engine.js +36 -0
- package/dist/shared/apngMetadataInjector.js +45 -0
- package/dist/shared/crc32.js +28 -0
- package/dist/shared/gifMetadataInjector.js +32 -0
- package/dist/shared/metadata.js +6 -0
- package/dist/shared/types.js +16 -0
- package/dist/src/formats/generators/ApngFormatGenerator.js +70 -0
- package/dist/src/formats/generators/GifFormatGenerator.js +76 -0
- package/dist/src/formats/generators/MkvFormatGenerator.js +49 -0
- package/dist/src/formats/generators/MovFormatGenerator.js +49 -0
- package/dist/src/formats/generators/Mp4FormatGenerator.js +43 -0
- package/dist/src/formats/generators/WebmFormatGenerator.js +45 -0
- package/dist/src/formats/registry.js +71 -0
- package/dist/src/formats/types.js +1 -0
- package/dist/src/index.js +291 -0
- package/dist/src/mcp.js +314 -0
- package/dist/src/utils/logger.js +85 -0
- package/dist/src/utils/packageInfo.js +16 -0
- package/dist/src/utils/validateOptions.js +16 -0
- package/package.json +124 -0
- package/skills/svg-to-video/SKILL.md +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 GehDoc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="./docs/assets/social-preview.svg" alt="SVG to Video Logo" width="600">
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
# SVG to Video
|
|
6
|
+
|
|
7
|
+
[](https://github.com/GehDoc/svg-to-video/actions/workflows/ci.yml)
|
|
8
|
+
[](https://github.com/GehDoc/svg-to-video/releases)
|
|
9
|
+
[](https://gehdoc.github.io/svg-to-video/commits/main)
|
|
10
|
+
|
|
11
|
+
A high-fidelity tool to transform your CSS-animated SVGs into high-quality videos (MP4, WebM, MKV, MOV) or lightweight animated formats (aPNG, GIF) with perfect alpha-channel transparency directly in your browser or via CLI/MCP server.
|
|
12
|
+
|
|
13
|
+
<div align="center">
|
|
14
|
+
<a href="https://gehdoc.github.io/svg-to-video/"><img src="https://gehdoc.github.io/svg-to-video/assets/demo.gif" width="800" alt="SVG to Video Demo"></a>
|
|
15
|
+
<p><i>Automated demo generated by our CI/CD pipeline</i></p>
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## 🌟 Why SVG to Video?
|
|
21
|
+
|
|
22
|
+
- **Transparent Backgrounds**: Export your animations with a full alpha channel using **WebM or aPNG**, and optimized indexed transparency with **GIF89a**. Perfect for overlays in video editing tools or web use.
|
|
23
|
+
- **Privacy-First**: The Web Studio runs entirely in your browser—your SVG files never leave your computer.
|
|
24
|
+
- **Frame-Accurate**: Our engine scrubs the **Web Animations API**, ensuring every frame is captured exactly as rendered.
|
|
25
|
+
- **AI Agent Native**: Native **Model Context Protocol (MCP)** server and **Agent Skill (`SKILL.md`)** support to let AI assistants render their generated SVG animations into MP4/GIF automatically.
|
|
26
|
+
- **Universal Animated Formats**: Beyond video, generate lightweight **Animated PNGs (aPNG)** and **GIFs** perfect for documentation, Slack, or GitHub, with full control over background transparency.
|
|
27
|
+
- **Copy to Clipboard**: Instant export to **Base64 Data URL**—copy your video and embed it directly into your HTML, CSS, or JS code without manual downloads. Perfect for rapid prototyping and developers.
|
|
28
|
+
- **Metadata Injection**: Support for custom titles and comments across video (MP4, WebM, MKV, MOV) and animated image (aPNG, GIF) formats.
|
|
29
|
+
- **Versatile**: Whether you need an accessible [Web Studio](#-web-studio) for quick conversions or a powerful [CLI tool](#-cli--docker-tool) for batch automation and CI/CD pipelines, this project has you covered.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## 🚀 Getting Started
|
|
34
|
+
|
|
35
|
+
Choose the entry point that matches your needs:
|
|
36
|
+
|
|
37
|
+
- **[Web Studio](https://gehdoc.github.io/svg-to-video/)**: The easiest way to convert SVGs to video, **high-fidelity aPNG, or optimized GIF** in your browser without any installation.
|
|
38
|
+
- **[CLI / Docker Tool](#-cli--docker-tool)**: For batch processing, server-side automation, and CI/CD integration.
|
|
39
|
+
- **[AI Agent & MCP Server](#-ai-agent--mcp-integration)**: For Claude Desktop, Cursor, Antigravity, and autonomous LLM workflows.
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## 🌐 Web Studio
|
|
44
|
+
|
|
45
|
+
**[🚀 Try the Web Studio](https://gehdoc.github.io/svg-to-video/)**
|
|
46
|
+
|
|
47
|
+
Our **Web Studio** is a serverless, client-side rendering tool. It runs entirely in your browser using **WebCodecs**—your SVG files never leave your computer, ensuring absolute privacy. The interface is optimized for high-productivity on screens starting from **720p resolution**.
|
|
48
|
+
|
|
49
|
+
> **Automated Demo**: The demo video at the top of this README is automatically generated and updated by our CI/CD pipeline using Playwright and Driver.js, ensuring it always reflects the latest UI changes.
|
|
50
|
+
|
|
51
|
+
> **Privacy Note**: We use [Umami Analytics](https://umami.is/) to collect anonymous usage data (e.g., number of conversions) to help us improve the tool. This tracking is cookie-less, respects "Do Not Track" settings, and never collects personal information.
|
|
52
|
+
|
|
53
|
+
Explore our **[Visual Gallery (Storybook)](https://gehdoc.github.io/svg-to-video/storybook/)** to see how the engine handles complex CSS and fonts.
|
|
54
|
+
|
|
55
|
+
### Quick Start
|
|
56
|
+
|
|
57
|
+
1. Open the [Web Studio](https://gehdoc.github.io/svg-to-video/).
|
|
58
|
+
2. Drag and drop your `.svg` file.
|
|
59
|
+
3. Adjust resolution, duration, and FPS.
|
|
60
|
+
4. Select your format, toggle **Transparent Background** if needed, and ensure **High-Fidelity Capture** is enabled for best results.
|
|
61
|
+
5. Click **Export**.
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## 🚀 CLI / Docker Tool
|
|
66
|
+
|
|
67
|
+
For automated or batch processing, use the CLI tool. It is built to run in a headless environment, making it perfect for CI/CD pipelines or server-side automation.
|
|
68
|
+
|
|
69
|
+
### Quick Start
|
|
70
|
+
|
|
71
|
+
Ensure [Node.js](https://nodejs.org/) and [FFmpeg](https://ffmpeg.org/) are installed.
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
# Node.js (auto-detected duration)
|
|
75
|
+
npx @gehdoc/svg-to-video input.svg 60 ./out-dir
|
|
76
|
+
|
|
77
|
+
# Node.js (explicit duration & GIF output)
|
|
78
|
+
npx @gehdoc/svg-to-video input.svg 60 ./out-dir -d 5 --format gif --transparent
|
|
79
|
+
|
|
80
|
+
# Node.js (animated PNG output)
|
|
81
|
+
npx @gehdoc/svg-to-video input.svg 60 ./out-dir -d 5 --format apng
|
|
82
|
+
|
|
83
|
+
# Docker (zero local dependencies)
|
|
84
|
+
docker run --rm -v $(pwd):/data gehdoc/svg-to-video input.svg 60 ./out-dir -d 5 --format gif
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
See [docs/CLI.md](./docs/CLI.md) for full usage, arguments, and options.
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## 🤖 AI Agent & MCP Integration
|
|
92
|
+
|
|
93
|
+
Connect `svg-to-video` to AI Assistants (Claude Desktop, Cursor, Antigravity, AutoGPT) via Model Context Protocol:
|
|
94
|
+
|
|
95
|
+
1. **Add to `mcpServers` config**:
|
|
96
|
+
```json
|
|
97
|
+
{
|
|
98
|
+
"mcpServers": {
|
|
99
|
+
"svg-to-video": {
|
|
100
|
+
"command": "npx",
|
|
101
|
+
"args": ["-y", "@gehdoc/svg-to-video", "mcp"]
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
2. **Prompt your AI Assistant**:
|
|
107
|
+
> _"Convert `examples/example.svg` into a 60fps transparent WebM video."_
|
|
108
|
+
|
|
109
|
+
See **[docs/MCP.md](./docs/MCP.md)** for full setup instructions, tool schemas, Docker execution, and agent prompting guides.
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## 📦 Installation Options
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
# Run on-demand via npx (no global installation required)
|
|
117
|
+
npx @gehdoc/svg-to-video input.svg 60 ./out-dir
|
|
118
|
+
|
|
119
|
+
# Install globally via npm
|
|
120
|
+
npm install -g @gehdoc/svg-to-video
|
|
121
|
+
|
|
122
|
+
# Pull official Docker image
|
|
123
|
+
docker pull gehdoc/svg-to-video:latest
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## 🛠 Technical Details
|
|
129
|
+
|
|
130
|
+
For a deep dive into the rendering engine, algorithms, and infrastructure, see [docs/ARCHITECTURE.md](./docs/ARCHITECTURE.md).
|
|
131
|
+
|
|
132
|
+
The tool works by isolating the SVG, scrubbing the Web Animations API, and capturing frames via WebCodecs or FFmpeg.
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## 🤝 Contributing
|
|
137
|
+
|
|
138
|
+
Contributions are welcome! This project follows a **Spec-Driven Development (SDD)** workflow to ensure clear requirements and high quality. Please open an issue or pull request.
|
|
139
|
+
|
|
140
|
+
Check out our [Active Roadmap](./specs/pending/) to see what we're working on next.
|
|
141
|
+
|
|
142
|
+
For instructions on contributing, build commands, security standards, and the technical roadmap, please see [CONTRIBUTING.md](./CONTRIBUTING.md).
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## 💖 Support the Project
|
|
147
|
+
|
|
148
|
+
If you find this tool helpful, please consider supporting its development:
|
|
149
|
+
|
|
150
|
+
[](https://paypal.me/GehDoc)
|
|
151
|
+
|
|
152
|
+
Your support helps cover maintenance and further development of the tool. Thank you!
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
## 📜 License
|
|
157
|
+
|
|
158
|
+
This project is licensed under the **MIT License**.
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility to analyze SVG content and detect its animation duration.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Parses an SVG clock value (e.g., "5s", "100ms", "1:30") into seconds.
|
|
6
|
+
*/
|
|
7
|
+
export function parseClockValue(value) {
|
|
8
|
+
if (!value)
|
|
9
|
+
return null;
|
|
10
|
+
const trimmed = value.trim();
|
|
11
|
+
if (trimmed === 'indefinite')
|
|
12
|
+
return Infinity;
|
|
13
|
+
// hh:mm:ss.sss or mm:ss.sss
|
|
14
|
+
const parts = trimmed.split(':');
|
|
15
|
+
if (parts.length > 1) {
|
|
16
|
+
let seconds = 0;
|
|
17
|
+
if (parts.length === 3) {
|
|
18
|
+
seconds += parseFloat(parts[0]) * 3600; // hours
|
|
19
|
+
seconds += parseFloat(parts[1]) * 60; // minutes
|
|
20
|
+
seconds += parseFloat(parts[2]); // seconds
|
|
21
|
+
}
|
|
22
|
+
else if (parts.length === 2) {
|
|
23
|
+
seconds += parseFloat(parts[0]) * 60; // minutes
|
|
24
|
+
seconds += parseFloat(parts[1]); // seconds
|
|
25
|
+
}
|
|
26
|
+
return isNaN(seconds) ? null : seconds;
|
|
27
|
+
}
|
|
28
|
+
// 5s, 100ms, 1min, 1h
|
|
29
|
+
const match = trimmed.match(/^([\d.]+)(h|min|s|ms)?$/);
|
|
30
|
+
if (!match)
|
|
31
|
+
return null;
|
|
32
|
+
const num = parseFloat(match[1]);
|
|
33
|
+
const unit = (match[2] || 's').toLowerCase();
|
|
34
|
+
switch (unit) {
|
|
35
|
+
case 'h':
|
|
36
|
+
return num * 3600;
|
|
37
|
+
case 'min':
|
|
38
|
+
return num * 60;
|
|
39
|
+
case 's':
|
|
40
|
+
return num;
|
|
41
|
+
case 'ms':
|
|
42
|
+
return num / 1000;
|
|
43
|
+
default:
|
|
44
|
+
return num;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Computes the Greatest Common Divisor of two numbers.
|
|
49
|
+
* Used for calculating the Least Common Multiple (LCM).
|
|
50
|
+
*/
|
|
51
|
+
export function gcd(a, b) {
|
|
52
|
+
a = Math.abs(Math.round(a));
|
|
53
|
+
b = Math.abs(Math.round(b));
|
|
54
|
+
while (b) {
|
|
55
|
+
a %= b;
|
|
56
|
+
[a, b] = [b, a];
|
|
57
|
+
}
|
|
58
|
+
return a;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Computes the Least Common Multiple of two numbers.
|
|
62
|
+
* Used to find the synchronization point of multiple looping animations.
|
|
63
|
+
*/
|
|
64
|
+
export function lcm(a, b) {
|
|
65
|
+
if (a === 0 || b === 0)
|
|
66
|
+
return 0;
|
|
67
|
+
return Math.abs(a * (b / gcd(a, b)));
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Computes the Least Common Multiple of an array of numbers.
|
|
71
|
+
* Caps the result at 1 hour (3,600,000 ms) to avoid infinite loops or astronomical durations.
|
|
72
|
+
*/
|
|
73
|
+
export function calculateLCM(numbers) {
|
|
74
|
+
if (numbers.length === 0)
|
|
75
|
+
return 0;
|
|
76
|
+
let result = numbers[0];
|
|
77
|
+
if (result > 3600000)
|
|
78
|
+
return 3600000;
|
|
79
|
+
for (let i = 1; i < numbers.length; i++) {
|
|
80
|
+
result = lcm(result, numbers[i]);
|
|
81
|
+
if (result > 3600000)
|
|
82
|
+
return 3600000;
|
|
83
|
+
}
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Extracts all time values (seconds or milliseconds) from a CSS property value string.
|
|
88
|
+
*/
|
|
89
|
+
export function extractTimes(str) {
|
|
90
|
+
const matches = str.matchAll(/\b([\d.]+)(s|ms)\b/gi);
|
|
91
|
+
return Array.from(matches).map((m) => {
|
|
92
|
+
const val = parseFloat(m[1]);
|
|
93
|
+
const unit = m[2].toLowerCase();
|
|
94
|
+
return unit === 'ms' ? val / 1000 : val;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Analyzes the provided SVG content (string) and returns a recommended duration.
|
|
99
|
+
* Accepts an optional DOMParser to use if the environment doesn't have a global one.
|
|
100
|
+
*/
|
|
101
|
+
export const analyzeSvgAnimation = (svgContent, parserOverride) => {
|
|
102
|
+
const Parser = parserOverride ||
|
|
103
|
+
(typeof DOMParser !== 'undefined' ? DOMParser : undefined);
|
|
104
|
+
if (!Parser) {
|
|
105
|
+
throw new Error('DOMParser is not available. Provide it via parserOverride.');
|
|
106
|
+
}
|
|
107
|
+
const parser = new Parser();
|
|
108
|
+
const root = parser.parseFromString(svgContent, 'image/svg+xml');
|
|
109
|
+
const animations = [];
|
|
110
|
+
// 1. Manual SMIL Parsing
|
|
111
|
+
const smilElements = root.querySelectorAll('animate, animateTransform, animateMotion, animateColor, set');
|
|
112
|
+
smilElements.forEach((el) => {
|
|
113
|
+
const dur = parseClockValue(el.getAttribute('dur')) || 0;
|
|
114
|
+
const begin = parseClockValue(el.getAttribute('begin')) || 0;
|
|
115
|
+
const repeatCount = el.getAttribute('repeatCount');
|
|
116
|
+
const repeatDur = parseClockValue(el.getAttribute('repeatDur'));
|
|
117
|
+
const isLooping = repeatCount === 'indefinite' || repeatDur === Infinity;
|
|
118
|
+
let totalDur = dur;
|
|
119
|
+
if (!isLooping) {
|
|
120
|
+
if (repeatCount) {
|
|
121
|
+
const count = parseFloat(repeatCount);
|
|
122
|
+
if (!isNaN(count))
|
|
123
|
+
totalDur *= count;
|
|
124
|
+
}
|
|
125
|
+
if (repeatDur !== null && repeatDur < Infinity) {
|
|
126
|
+
totalDur = Math.max(totalDur, repeatDur);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
animations.push({
|
|
130
|
+
duration: isLooping ? dur : begin + totalDur,
|
|
131
|
+
isLooping,
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
// 2. CSS Animation Detection (Heuristic)
|
|
135
|
+
const styleTags = Array.from(root.querySelectorAll('style'));
|
|
136
|
+
const elementsWithStyle = Array.from(root.querySelectorAll('[style]'));
|
|
137
|
+
// Extract all CSS rules (from style tags)
|
|
138
|
+
const styleRules = styleTags
|
|
139
|
+
.map((s) => s.textContent)
|
|
140
|
+
.join('\n')
|
|
141
|
+
.match(/[^{}]+\{[^{}]+\}/g) || [];
|
|
142
|
+
// Extract all inline styles (pretend they are rules)
|
|
143
|
+
const inlineRules = elementsWithStyle.map((el) => `* { ${el.getAttribute('style')} }`);
|
|
144
|
+
const allRules = [...styleRules, ...inlineRules];
|
|
145
|
+
for (const rule of allRules) {
|
|
146
|
+
const content = rule.substring(rule.indexOf('{') + 1, rule.lastIndexOf('}'));
|
|
147
|
+
const props = content
|
|
148
|
+
.split(';')
|
|
149
|
+
.map((p) => p.trim())
|
|
150
|
+
.filter(Boolean);
|
|
151
|
+
// Track durations and delays per rule to handle separate properties
|
|
152
|
+
let maxRuleDur = 0;
|
|
153
|
+
let maxRuleDelay = 0;
|
|
154
|
+
const ruleHasInfinite = /infinite/i.test(content);
|
|
155
|
+
for (const prop of props) {
|
|
156
|
+
const [name, val] = prop.split(':').map((s) => s.trim().toLowerCase());
|
|
157
|
+
if (!name || !val)
|
|
158
|
+
continue;
|
|
159
|
+
if (name === 'animation' || name === 'transition') {
|
|
160
|
+
const subs = val.split(',');
|
|
161
|
+
for (const sub of subs) {
|
|
162
|
+
const times = extractTimes(sub);
|
|
163
|
+
const isLooping = /infinite/i.test(sub);
|
|
164
|
+
if (times.length > 0) {
|
|
165
|
+
if (isLooping) {
|
|
166
|
+
animations.push({ duration: times[0], isLooping: true });
|
|
167
|
+
// Also track total effective time (dur + delay) for the first iteration
|
|
168
|
+
animations.push({
|
|
169
|
+
duration: times.reduce((a, b) => a + b, 0),
|
|
170
|
+
isLooping: false,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
animations.push({
|
|
175
|
+
duration: times.reduce((a, b) => a + b, 0),
|
|
176
|
+
isLooping: false,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
else if (name.includes('duration')) {
|
|
183
|
+
const times = extractTimes(val);
|
|
184
|
+
maxRuleDur = Math.max(maxRuleDur, ...times);
|
|
185
|
+
}
|
|
186
|
+
else if (name.includes('delay')) {
|
|
187
|
+
const times = extractTimes(val);
|
|
188
|
+
maxRuleDelay = Math.max(maxRuleDelay, ...times);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (maxRuleDur > 0 || maxRuleDelay > 0) {
|
|
192
|
+
animations.push({
|
|
193
|
+
duration: maxRuleDur + maxRuleDelay,
|
|
194
|
+
isLooping: ruleHasInfinite,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (animations.length === 0)
|
|
199
|
+
return undefined;
|
|
200
|
+
const looping = animations.filter((a) => a.isLooping).map((a) => a.duration);
|
|
201
|
+
const nonLooping = animations
|
|
202
|
+
.filter((a) => !a.isLooping)
|
|
203
|
+
.map((a) => a.duration);
|
|
204
|
+
if (looping.length > 0) {
|
|
205
|
+
const loopingMs = looping.map((d) => Math.round(d * 1000));
|
|
206
|
+
const lcmMs = calculateLCM(loopingMs);
|
|
207
|
+
const maxNonLoopingS = nonLooping.length > 0 ? Math.max(...nonLooping) : 0;
|
|
208
|
+
let totalMs = lcmMs;
|
|
209
|
+
if (maxNonLoopingS * 1000 > totalMs) {
|
|
210
|
+
totalMs = Math.ceil((maxNonLoopingS * 1000) / lcmMs) * lcmMs;
|
|
211
|
+
}
|
|
212
|
+
return totalMs / 1000;
|
|
213
|
+
}
|
|
214
|
+
else if (nonLooping.length > 0) {
|
|
215
|
+
return Math.max(...nonLooping);
|
|
216
|
+
}
|
|
217
|
+
return undefined;
|
|
218
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-side function to seek all animations to a specific time.
|
|
3
|
+
* Supports both CSS Animations (Web Animations API) and SMIL.
|
|
4
|
+
* This is the core logic shared between the CLI (Puppeteer) and the Web (Iframe) versions.
|
|
5
|
+
* @param timeInMilliseconds
|
|
6
|
+
*/
|
|
7
|
+
export function seekAnimations(timeInMilliseconds) {
|
|
8
|
+
// 1. Seek CSS Animations (Web Animations API)
|
|
9
|
+
const animations = document.getAnimations();
|
|
10
|
+
animations.forEach((animation) => {
|
|
11
|
+
try {
|
|
12
|
+
animation.pause();
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
/* ignored */
|
|
16
|
+
}
|
|
17
|
+
animation.currentTime = timeInMilliseconds;
|
|
18
|
+
});
|
|
19
|
+
// 2. Seek SMIL Animations
|
|
20
|
+
const svgs = document.querySelectorAll('svg');
|
|
21
|
+
svgs.forEach((svg) => {
|
|
22
|
+
try {
|
|
23
|
+
if (svg instanceof SVGSVGElement) {
|
|
24
|
+
if (typeof svg.pauseAnimations === 'function') {
|
|
25
|
+
svg.pauseAnimations();
|
|
26
|
+
}
|
|
27
|
+
if (typeof svg.setCurrentTime === 'function') {
|
|
28
|
+
svg.setCurrentTime(timeInMilliseconds / 1000);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
/* ignored */
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { mergeMetadataComments } from './metadata.js';
|
|
2
|
+
import { crc32 } from './crc32.js';
|
|
3
|
+
export function injectApngMetadata(buffer, metadata, version) {
|
|
4
|
+
if (!metadata || (!metadata.title && !metadata.comment)) {
|
|
5
|
+
return buffer;
|
|
6
|
+
}
|
|
7
|
+
const chunks = [];
|
|
8
|
+
const addTextChunk = (keyword, text) => {
|
|
9
|
+
const textBytes = new TextEncoder().encode(text);
|
|
10
|
+
const keywordBytes = new TextEncoder().encode(keyword);
|
|
11
|
+
const chunkData = new Uint8Array(keywordBytes.length + 1 + textBytes.length);
|
|
12
|
+
chunkData.set(keywordBytes, 0);
|
|
13
|
+
chunkData.set([0], keywordBytes.length);
|
|
14
|
+
chunkData.set(textBytes, keywordBytes.length + 1);
|
|
15
|
+
const chunkType = new TextEncoder().encode('tEXt');
|
|
16
|
+
const chunkLength = chunkData.length;
|
|
17
|
+
const chunk = new Uint8Array(12 + chunkLength);
|
|
18
|
+
const view = new DataView(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
|
19
|
+
view.setUint32(0, chunkLength, false);
|
|
20
|
+
chunk.set(chunkType, 4);
|
|
21
|
+
chunk.set(chunkData, 8);
|
|
22
|
+
const crcInput = new Uint8Array(chunkType.length + chunkLength);
|
|
23
|
+
crcInput.set(chunkType);
|
|
24
|
+
crcInput.set(chunkData, chunkType.length);
|
|
25
|
+
view.setUint32(8 + chunkLength, crc32(crcInput), false);
|
|
26
|
+
chunks.push(chunk);
|
|
27
|
+
};
|
|
28
|
+
if (metadata.title)
|
|
29
|
+
addTextChunk('Title', metadata.title.trim());
|
|
30
|
+
const descriptionText = mergeMetadataComments(metadata.comment?.trim(), version);
|
|
31
|
+
addTextChunk('Description', descriptionText);
|
|
32
|
+
if (buffer.length < 33)
|
|
33
|
+
return buffer;
|
|
34
|
+
const ihdrLength = new DataView(buffer.buffer, buffer.byteOffset + 8, 4).getUint32(0, false);
|
|
35
|
+
const ihdrEnd = 8 + 4 + 4 + ihdrLength + 4;
|
|
36
|
+
const newBuffer = new Uint8Array(buffer.length + chunks.reduce((acc, c) => acc + c.length, 0));
|
|
37
|
+
newBuffer.set(buffer.subarray(0, ihdrEnd), 0);
|
|
38
|
+
let offset = ihdrEnd;
|
|
39
|
+
for (const chunk of chunks) {
|
|
40
|
+
newBuffer.set(chunk, offset);
|
|
41
|
+
offset += chunk.length;
|
|
42
|
+
}
|
|
43
|
+
newBuffer.set(buffer.subarray(ihdrEnd), offset);
|
|
44
|
+
return newBuffer;
|
|
45
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
let crcTable = null;
|
|
2
|
+
function makeCrcTable() {
|
|
3
|
+
const table = new Uint32Array(256);
|
|
4
|
+
for (let entryIndex = 0; entryIndex < 256; entryIndex++) {
|
|
5
|
+
let currentByte = entryIndex;
|
|
6
|
+
for (let bitIndex = 0; bitIndex < 8; bitIndex++) {
|
|
7
|
+
currentByte =
|
|
8
|
+
currentByte & 1 ? 0xedb88320 ^ (currentByte >>> 1) : currentByte >>> 1;
|
|
9
|
+
}
|
|
10
|
+
table[entryIndex] = currentByte;
|
|
11
|
+
}
|
|
12
|
+
return table;
|
|
13
|
+
}
|
|
14
|
+
function getCrcTable() {
|
|
15
|
+
if (!crcTable) {
|
|
16
|
+
crcTable = makeCrcTable();
|
|
17
|
+
}
|
|
18
|
+
return crcTable;
|
|
19
|
+
}
|
|
20
|
+
export const crc32 = (buffer) => {
|
|
21
|
+
const table = getCrcTable();
|
|
22
|
+
let crcChecksum = 0xffffffff;
|
|
23
|
+
for (let byteIndex = 0; byteIndex < buffer.length; byteIndex++) {
|
|
24
|
+
crcChecksum =
|
|
25
|
+
(crcChecksum >>> 8) ^ table[(crcChecksum ^ buffer[byteIndex]) & 0xff];
|
|
26
|
+
}
|
|
27
|
+
return (crcChecksum ^ 0xffffffff) >>> 0;
|
|
28
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { mergeMetadataComments } from './metadata.js';
|
|
2
|
+
export function injectGifMetadata(gifBytes, metadata, version) {
|
|
3
|
+
if (!metadata || (!metadata.title && !metadata.comment)) {
|
|
4
|
+
return gifBytes;
|
|
5
|
+
}
|
|
6
|
+
const title = metadata.title?.trim();
|
|
7
|
+
const rawComment = metadata.comment?.trim();
|
|
8
|
+
const commentWithAttribution = mergeMetadataComments(rawComment, version);
|
|
9
|
+
const commentText = title
|
|
10
|
+
? `${title} - ${commentWithAttribution}`
|
|
11
|
+
: commentWithAttribution;
|
|
12
|
+
const textBytes = new TextEncoder().encode(commentText);
|
|
13
|
+
const commentBlocks = [0x21, 0xfe];
|
|
14
|
+
let offset = 0;
|
|
15
|
+
while (offset < textBytes.length) {
|
|
16
|
+
const blockSize = Math.min(255, textBytes.length - offset);
|
|
17
|
+
commentBlocks.push(blockSize);
|
|
18
|
+
for (let i = 0; i < blockSize; i++) {
|
|
19
|
+
commentBlocks.push(textBytes[offset + i]);
|
|
20
|
+
}
|
|
21
|
+
offset += blockSize;
|
|
22
|
+
}
|
|
23
|
+
commentBlocks.push(0x00);
|
|
24
|
+
const commentExtension = new Uint8Array(commentBlocks);
|
|
25
|
+
const trailerIndex = gifBytes.lastIndexOf(0x3b);
|
|
26
|
+
const insertOffset = trailerIndex !== -1 ? trailerIndex : gifBytes.length;
|
|
27
|
+
const finalBuffer = new Uint8Array(gifBytes.length + commentExtension.length);
|
|
28
|
+
finalBuffer.set(gifBytes.subarray(0, insertOffset), 0);
|
|
29
|
+
finalBuffer.set(commentExtension, insertOffset);
|
|
30
|
+
finalBuffer.set(gifBytes.subarray(insertOffset), insertOffset + commentExtension.length);
|
|
31
|
+
return finalBuffer;
|
|
32
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const mergeMetadataComments = (userComment, version) => {
|
|
2
|
+
const toolName = 'svg-to-video';
|
|
3
|
+
const versionStr = version ? ` v${version}` : '';
|
|
4
|
+
const attribution = `Converted from SVG by ${toolName}${versionStr} (https://gehdoc.github.io/svg-to-video/)`;
|
|
5
|
+
return userComment ? `${userComment} | ${attribution}` : attribution;
|
|
6
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export * from './metadata.js';
|
|
2
|
+
export function isRendererMessage(data) {
|
|
3
|
+
return (!!data &&
|
|
4
|
+
typeof data === 'object' &&
|
|
5
|
+
'type' in data &&
|
|
6
|
+
typeof data.type === 'string' &&
|
|
7
|
+
[
|
|
8
|
+
'LOAD_SVG',
|
|
9
|
+
'SEEK',
|
|
10
|
+
'CAPTURE',
|
|
11
|
+
'READY',
|
|
12
|
+
'SEEKED',
|
|
13
|
+
'CAPTURE_RESULT',
|
|
14
|
+
'SCRIPT_LOADED',
|
|
15
|
+
].includes(data.type));
|
|
16
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { mergeMetadataComments } from '#shared/metadata.js';
|
|
4
|
+
import { injectApngMetadata } from '#shared/apngMetadataInjector.js';
|
|
5
|
+
export class ApngFormatGenerator {
|
|
6
|
+
id = 'apng';
|
|
7
|
+
extensions = ['.apng', '.png'];
|
|
8
|
+
supportsAlpha = true;
|
|
9
|
+
buildFfmpegArgs(options) {
|
|
10
|
+
const { fps, hold, outDir, transparent, metadata, inputPattern, pkgVersion, outputFileName, } = options;
|
|
11
|
+
const args = [
|
|
12
|
+
'-hide_banner',
|
|
13
|
+
'-loglevel',
|
|
14
|
+
'warning',
|
|
15
|
+
'-y',
|
|
16
|
+
'-framerate',
|
|
17
|
+
String(fps),
|
|
18
|
+
'-i',
|
|
19
|
+
inputPattern,
|
|
20
|
+
];
|
|
21
|
+
let userComment;
|
|
22
|
+
if (metadata) {
|
|
23
|
+
metadata.forEach((m) => {
|
|
24
|
+
if (m.startsWith('comment=')) {
|
|
25
|
+
userComment = m.split('=')[1];
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
args.push('-metadata', m);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
const finalComment = mergeMetadataComments(userComment, pkgVersion);
|
|
33
|
+
const filters = [];
|
|
34
|
+
if (hold && hold > 0) {
|
|
35
|
+
filters.push(`tpad=stop_mode=clone:stop_duration=${hold}`);
|
|
36
|
+
}
|
|
37
|
+
if (filters.length) {
|
|
38
|
+
args.push('-vf', filters.join(','));
|
|
39
|
+
}
|
|
40
|
+
args.push('-f', 'apng', '-plays', '0');
|
|
41
|
+
if (transparent) {
|
|
42
|
+
args.push('-pix_fmt', 'rgba');
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
args.push('-pix_fmt', 'rgb24');
|
|
46
|
+
}
|
|
47
|
+
args.push('-metadata', `comment=${finalComment}`);
|
|
48
|
+
const outputFullPath = path.join(outDir, outputFileName);
|
|
49
|
+
args.push(outputFullPath);
|
|
50
|
+
return args;
|
|
51
|
+
}
|
|
52
|
+
postProcess(outputFilePath, options) {
|
|
53
|
+
const { metadata, pkgVersion } = options;
|
|
54
|
+
let userComment;
|
|
55
|
+
let title;
|
|
56
|
+
if (metadata) {
|
|
57
|
+
metadata.forEach((m) => {
|
|
58
|
+
if (m.startsWith('comment=')) {
|
|
59
|
+
userComment = m.split('=')[1];
|
|
60
|
+
}
|
|
61
|
+
else if (m.startsWith('title=')) {
|
|
62
|
+
title = m.split('=')[1];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
const fileBuf = fs.readFileSync(outputFilePath);
|
|
67
|
+
const updatedBuf = injectApngMetadata(fileBuf, { title, comment: userComment }, pkgVersion);
|
|
68
|
+
fs.writeFileSync(outputFilePath, updatedBuf);
|
|
69
|
+
}
|
|
70
|
+
}
|