-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR introduces the renderer package, which makes it possible to render videos via a function call (using a headless browser).
- Loading branch information
1 parent
d0f72b6
commit 724582b
Showing
24 changed files
with
15,581 additions
and
32,592 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ module.exports = { | |
'examples', | ||
'legacy', | ||
'player', | ||
'renderer', | ||
'ui', | ||
'vite-plugin', | ||
], | ||
|
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
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 @@ | ||
/// <reference types="@motion-canvas/core/project" /> |
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,20 @@ | ||
import type {Project} from '@motion-canvas/core'; | ||
import {Renderer} from '@motion-canvas/core'; | ||
|
||
declare global { | ||
interface Window { | ||
onRenderComplete: () => void; | ||
} | ||
} | ||
|
||
export const render = async (project: Project) => { | ||
try { | ||
const renderer = new Renderer(project); | ||
await renderer.render({ | ||
...project.meta.getFullRenderingSettings(), | ||
name: project.name, | ||
}); | ||
} finally { | ||
window.onRenderComplete(); | ||
} | ||
}; |
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,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": "src", | ||
"outDir": "../dist/client", | ||
"strict": true, | ||
"module": "es2020", | ||
"target": "es2020", | ||
"moduleResolution": "node", | ||
"declaration": true, | ||
"inlineSourceMap": true, | ||
"allowSyntheticDefaultImports": true | ||
}, | ||
"include": ["*"] | ||
} |
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.14.1", | ||
"description": "A headless renderer for Motion Canvas", | ||
"main": "dist/index.js", | ||
"author": "motion-canvas", | ||
"homepage": "https://motioncanvas.io/", | ||
"bugs": "https://github.com/motion-canvas/motion-canvas/issues", | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "npm run client:build && npm run server:build", | ||
"client:build": "tsc --project client/tsconfig.json", | ||
"client:dev": "tsc -w --project client/tsconfig.json", | ||
"server:build": "tsc --project server/tsconfig.json", | ||
"server:dev": "tsc -w --project server/tsconfig.json" | ||
}, | ||
"files": [ | ||
"dist", | ||
"types" | ||
], | ||
"devDependencies": { | ||
"@motion-canvas/core": "*", | ||
"puppeteer": "^22.3.0" | ||
} | ||
} |
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,12 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Motion Canvas Renderer</title> | ||
</head> | ||
<body> | ||
<h1>RENDERING!</h1> | ||
<script type="module" src="{{source}}"></script> | ||
</body> | ||
</html> |
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,2 @@ | ||
export * from './main'; | ||
export * from './plugin'; |
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,35 @@ | ||
import * as path from 'path'; | ||
import puppeteer from 'puppeteer'; | ||
import {createServer} from 'vite'; | ||
|
||
export const renderVideo = async (configFile: string) => { | ||
console.log('Rendering...'); | ||
|
||
const resolvedConfigPath = path.resolve(process.cwd(), configFile); | ||
|
||
const [browser, server] = await Promise.all([ | ||
puppeteer.launch({headless: true}), | ||
createServer({ | ||
configFile: resolvedConfigPath, | ||
server: { | ||
port: 9000, | ||
}, | ||
}).then(server => server.listen()), | ||
]); | ||
|
||
const page = await browser.newPage(); | ||
if (!server.httpServer) { | ||
throw new Error('HTTP server is not initialized'); | ||
} | ||
const address = server.httpServer.address(); | ||
const port = address && typeof address === 'object' ? address.port : null; | ||
if (port === null) { | ||
throw new Error('Server address is null'); | ||
} | ||
|
||
await page.goto(`http://localhost:${port}/render`); | ||
await page.exposeFunction('onRenderComplete', async () => { | ||
await Promise.all([browser.close(), server.close()]); | ||
console.log('Rendering complete.'); | ||
}); | ||
}; |
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 @@ | ||
/// <reference types="@motion-canvas/core/project" /> |
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,34 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import {Plugin} from 'vite'; | ||
|
||
const RendererPath = path.resolve(__dirname, '../renderer.html'); | ||
const Content = fs.readFileSync(RendererPath, 'utf-8'); | ||
const HtmlParts = Content.toString().split('{{source}}'); | ||
|
||
function createHtml(src: string) { | ||
return HtmlParts[0] + src + HtmlParts[1]; | ||
} | ||
|
||
export function rendererPlugin(): Plugin { | ||
return { | ||
name: 'motion-canvas-renderer-plugin', | ||
|
||
async load(id) { | ||
if (id.startsWith('\x00virtual:renderer')) { | ||
return `\ | ||
import {render} from '@motion-canvas/renderer/client/render'; | ||
import project from './src/project.ts?project'; | ||
render(project); | ||
`; | ||
} | ||
}, | ||
|
||
configureServer(server) { | ||
server.middlewares.use('/render', (_req, res) => { | ||
res.setHeader('Content-Type', 'text/html'); | ||
res.end(createHtml(`/@id/__x00__virtual:renderer`)); | ||
}); | ||
}, | ||
}; | ||
} |
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,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": "src", | ||
"outDir": "../dist", | ||
"strict": true, | ||
"module": "CommonJS", | ||
"target": "es2020", | ||
"moduleResolution": "node", | ||
"declaration": true, | ||
"inlineSourceMap": true, | ||
"allowSyntheticDefaultImports": true | ||
}, | ||
"include": ["*", "../index.ts", "../main.ts", "../motion-canvas.d.ts"] | ||
} |
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,3 @@ | ||
import {renderVideo} from '@motion-canvas/renderer'; | ||
|
||
renderVideo('./vite.config.ts'); |
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
{ | ||
"extends": "@motion-canvas/2d/tsconfig.project.json", | ||
"compilerOptions": { | ||
"types": ["node"] | ||
"types": ["node"], | ||
"noEmit": false, | ||
"outDir": "dist", | ||
"module": "CommonJS" | ||
}, | ||
"include": ["src"] | ||
} |
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