-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.tsx
116 lines (103 loc) · 3.01 KB
/
server.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import {bundle} from '@remotion/bundler';
import {
getCompositions,
renderMedia,
RenderMediaOnProgress,
} from '@remotion/renderer';
import express from 'express';
import path from 'path';
const app = express();
const port = 8000;
app.get('/', async (req, res) => {
try {
let a = await start();
console.log('a', a);
res.send('done');
} catch (error) {
console.log('err', error);
res.send(error);
}
});
app.listen(port);
console.log(
[
`The server has started on http://localhost:${port}!`,
'You can render a video by passing props as URL parameters.',
'',
'If you are running Hello World, try this:',
'',
`http://localhost:${port}?titleText=Hello,+World!&titleColor=red`,
'',
].join('\n')
);
const start = async () => {
// The composition you want to render
const compositionId = 'RedditTitleComp';
// You only have to do this once, you can reuse the bundle.
const entry = './src/index';
console.log('Creating a Webpack bundle of the video');
const bundleLocation = await bundle(path.resolve(entry), () => undefined, {
// If you have a Webpack override, make sure to add it here
webpackOverride: (config) => config,
});
// Parametrize the video by passing arbitrary props to your component.
const inputProps = {
foo: 'bar',
};
// Extract all the compositions you have defined in your project
// from the webpack bundle.
const comps = await getCompositions(bundleLocation, {
// You can pass custom input props that you can retrieve using getInputProps()
// in the composition list. Use this if you want to dynamically set the duration or
// dimensions of the video.
inputProps,
});
// Select the composition you want to render.
const composition = comps.find((c) => c.id === compositionId);
// Ensure the composition exists
if (!composition) {
throw new Error(`No composition with the ID ${compositionId} found.
Review "${entry}" for the correct ID.`);
}
const outputLocation = path.resolve('./out', `${compositionId}.mp4`);
console.log('Attempting to render:', outputLocation);
await renderMedia({
composition,
serveUrl: bundleLocation,
codec: 'h264',
outputLocation,
inputProps,
onStart: (e) => {
console.log('started', e);
},
onProgress,
});
console.log('Render done!');
};
const onProgress: RenderMediaOnProgress = ({
renderedFrames,
encodedFrames,
encodedDoneIn,
renderedDoneIn,
stitchStage,
}) => {
if (stitchStage === 'encoding') {
// First pass, parallel rendering of frames and encoding into video
console.log('Encoding...');
} else if (stitchStage === 'muxing') {
// Second pass, adding audio to the video
console.log('Muxing audio...');
}
// Amount of frames rendered into images
console.log(`${renderedFrames} rendered`);
// Amount of frame encoded into a video
console.log(`${encodedFrames} encoded`);
// Time to create images of all frames
if (renderedDoneIn !== null) {
console.log(`Rendered in ${renderedDoneIn}ms`);
}
// Time to encode video from images
if (encodedDoneIn !== null) {
console.log(`Encoded in ${encodedDoneIn}ms`);
}
};