-
-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b01eb5
commit 6622c65
Showing
15 changed files
with
1,307 additions
and
410 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "@motion-canvas/renderer", | ||
"version": "3.16.0", | ||
"type": "module", | ||
"main": "lib/index.js", | ||
"bin": { | ||
"motion-canvas-render": "lib/index.js" | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"dev": "tsc -w" | ||
}, | ||
"dependencies": { | ||
"@motion-canvas/vite-plugin": "^3.15.1", | ||
"cli-progress": "^3.12.0", | ||
"commander": "^12.0.0", | ||
"kleur": "^4.1.5", | ||
"ora": "^8.0.1", | ||
"puppeteer": "^22.9.0" | ||
}, | ||
"devDependencies": { | ||
"@motion-canvas/core": "^3.16.0", | ||
"@types/cli-progress": "^3.11.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#!/usr/bin/env node | ||
|
||
import type {RendererSettings} from '@motion-canvas/core'; | ||
import {program} from 'commander'; | ||
import * as path from 'path'; | ||
import * as process from 'process'; | ||
import { | ||
parseJSONFile, | ||
parseJSONString, | ||
parseRecord, | ||
parseTuple, | ||
} from './parsing.js'; | ||
import {render} from './puppeteer.js'; | ||
|
||
program | ||
.name('motion-canvas-render') | ||
.description( | ||
'CLI tool for rendering Motion Canvas projects in headless mode.', | ||
); | ||
|
||
program | ||
.argument('<path>', 'path to the project') | ||
.option('-p, --project <name>', 'name of the project to render') | ||
.option('-o, --output <path>', 'path to the output directory') | ||
.option('--product <name>', 'browser to use', 'firefox') | ||
.option('--debug', 'enable debug mode') | ||
.option('--background <color>', 'background color') | ||
.option('--range <from>:<to>', 'time range in seconds', parseTuple) | ||
.option( | ||
'--size <width>:<height>', | ||
'size of the project in pixels', | ||
parseTuple, | ||
) | ||
.option('--fps <number>', 'frames per second', parseInt) | ||
.option('--scale <number>', 'resolution scale', parseFloat) | ||
.option('--exporter <name>', 'name of the exporter') | ||
.option( | ||
'--exportOptions <key=value...>', | ||
'key-value pairs of export options', | ||
parseRecord, | ||
) | ||
.option( | ||
'--variables <key=value...>', | ||
'key-value paris of project variables', | ||
parseRecord, | ||
) | ||
.option( | ||
'--variablesJSON <json>', | ||
'JSON string with project variables', | ||
parseJSONString, | ||
) | ||
.option( | ||
'--variablesFile <json>', | ||
'Path to a JSON file with project variables', | ||
parseJSONFile, | ||
) | ||
.action((projectPath, options) => { | ||
const settings: Partial<RendererSettings> = {}; | ||
if (options.background) { | ||
settings.background = options.background; | ||
} | ||
if (options.range) { | ||
settings.range = options.range; | ||
} | ||
if (options.size) { | ||
settings.size = options.size; | ||
} | ||
if (options.fps) { | ||
settings.fps = options.fps; | ||
} | ||
if (options.scale) { | ||
settings.resolutionScale = options.scale; | ||
} | ||
|
||
settings.exporter = { | ||
name: options.exporter ?? '', | ||
options: options.exportOptions ?? {}, | ||
}; | ||
|
||
let variables: Record<string, string> = {}; | ||
if (options.variables) { | ||
variables = options.variables; | ||
} | ||
if (options.variablesJSON) { | ||
variables = { | ||
...variables, | ||
...options.variablesJSON, | ||
}; | ||
} | ||
if (options.variablesFile) { | ||
variables = { | ||
...variables, | ||
...options.variablesFile, | ||
}; | ||
} | ||
settings.variables = variables; | ||
|
||
const output = options.output | ||
? path.resolve(process.cwd(), options.output) | ||
: undefined; | ||
process.chdir(path.resolve(process.cwd(), projectPath)); | ||
return render( | ||
{ | ||
output, | ||
project: options.project, | ||
product: options.product, | ||
debug: options.debug, | ||
}, | ||
settings, | ||
); | ||
}); | ||
|
||
program.parse(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {InvalidOptionArgumentError} from 'commander'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as process from 'process'; | ||
|
||
export function parseTuple(value: string) { | ||
const [from, to] = value.split(':'); | ||
if (to === undefined) { | ||
throw new InvalidOptionArgumentError(`Missing colon.`); | ||
} | ||
|
||
const parsedFrom = parseFloat(from); | ||
if (isNaN(parsedFrom)) { | ||
throw new InvalidOptionArgumentError(`"${from}" is not a number.`); | ||
} | ||
|
||
const parsedTo = parseFloat(to); | ||
if (isNaN(parsedTo)) { | ||
throw new InvalidOptionArgumentError(`"${to}" is not a number.`); | ||
} | ||
|
||
return [parsedFrom, parsedTo]; | ||
} | ||
|
||
export function parseRecord( | ||
value: string, | ||
previous: Record<string, string> = {}, | ||
) { | ||
const [key, val] = value.split('='); | ||
if (val === undefined) { | ||
throw new InvalidOptionArgumentError(`Missing equals sign.`); | ||
} | ||
|
||
return { | ||
...previous, | ||
[key]: val, | ||
}; | ||
} | ||
|
||
export function parseJSONString( | ||
value: string, | ||
previous: Record<string, string> = {}, | ||
) { | ||
let parsed: Record<string, string>; | ||
try { | ||
parsed = JSON.parse(value); | ||
} catch (error) { | ||
throw new InvalidOptionArgumentError(error as string); | ||
} | ||
|
||
if (!parsed && typeof parsed !== 'object') { | ||
throw new InvalidOptionArgumentError('Not a valid JSON object.'); | ||
} | ||
|
||
return { | ||
...previous, | ||
...parsed, | ||
}; | ||
} | ||
|
||
export function parseJSONFile( | ||
file: string, | ||
previous: Record<string, string> = {}, | ||
) { | ||
const filePath = path.resolve(process.cwd(), file); | ||
let value: string; | ||
try { | ||
value = fs.readFileSync(filePath, 'utf-8'); | ||
} catch (error) { | ||
throw new InvalidOptionArgumentError(error as string); | ||
} | ||
|
||
return parseJSONString(value, previous); | ||
} |
Oops, something went wrong.