Skip to content

Commit c36272b

Browse files
authored
Fix rendering in docker (#12)
* Use main node image & upgrade fabric * prettier
1 parent 09da60a commit c36272b

13 files changed

Lines changed: 287 additions & 863 deletions

File tree

Containerfile

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
FROM node:alpine
1+
FROM node:22
22

3-
ENV PYTHONUNBUFFERED=1
4-
RUN apk update
5-
RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python
6-
RUN apk add --update --no-cache \
7-
make \
8-
g++ \
9-
jpeg-dev \
10-
cairo-dev \
11-
giflib-dev \
12-
pango-dev \
13-
libtool \
14-
autoconf \
15-
automake
16-
RUN apk add --update --no-cache font-noto
3+
RUN apt-get update
4+
RUN apt-get install build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev -y
175

186
WORKDIR /usr/src/app
197

apps/backend/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
"ajv": "^8.17.1",
2222
"ajv-formats": "^3.0.1",
2323
"canvas": "^2.11.2",
24-
"cli-progress": "^3.12.0",
2524
"dotenv": "^16.4.7",
26-
"fabric": "^4.6.0",
25+
"fabric": "^6.5.4",
2726
"fastify": "^5.1.0",
2827
"ffmpeg-static": "^4.4.1",
2928
"ffprobe-static": "^3.1.0",
@@ -37,8 +36,6 @@
3736
"@swc/cli": "^0.5.2",
3837
"@swc/core": "^1.10.4",
3938
"@tsconfig/node22": "^22.0.0",
40-
"@types/cli-progress": "^3.11.6",
41-
"@types/fabric": "^5.3.9",
4239
"@types/ffmpeg-static": "^3.0.3",
4340
"@types/ffprobe-static": "^2.0.3",
4441
"@types/fluent-ffmpeg": "^2.1.27",

apps/backend/src/canvas2video/encoder.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
import ffmpeg from "fluent-ffmpeg";
22
import * as fs from "fs";
33
import * as path from "path";
4-
import * as cliProgress from "cli-progress";
54
import { type Encoder } from "./types.js";
65
import { logError } from "../logger/index.js";
76

8-
const progressBar = new cliProgress.SingleBar({
9-
format: `Processing | {bar} | {percentage}%`,
10-
barCompleteChar: "\u2588",
11-
barIncompleteChar: "\u2591",
12-
hideCursor: true,
13-
});
14-
15-
const createDir = (
16-
reject: (reason?: Error) => void,
17-
silent: boolean,
18-
output: string,
19-
) => {
7+
const createDir = (reject: (reason?: Error) => void, output: string) => {
208
try {
219
const outDir = path.dirname(output);
2210
if (!fs.existsSync(outDir)) {
@@ -49,9 +37,6 @@ const createFilter = (backgroundVideo: {
4937
];
5038
};
5139

52-
const percent: (percent?: number) => number = (percent) =>
53-
percent ? parseFloat((percent as number).toFixed(2)) : 0;
54-
5540
const outputOptions = [
5641
"-preset veryfast",
5742
"-crf 24",
@@ -63,8 +48,8 @@ const outputOptions = [
6348

6449
const encoder: Encoder = (config) =>
6550
new Promise((resolve, reject) => {
66-
const { frameStream, output, backgroundVideo, fps, silent = true } = config;
67-
createDir(reject, silent, output);
51+
const { frameStream, output, backgroundVideo, fps } = config;
52+
createDir(reject, output);
6853

6954
const outputStream = fs.createWriteStream(output);
7055
const command = ffmpeg();
@@ -87,25 +72,12 @@ const encoder: Encoder = (config) =>
8772
command.complexFilter([...createFilter(backgroundVideo)], "tmp");
8873

8974
command.output(outputStream);
90-
// command.size(`${width}x${height}`);
91-
92-
command.on("start", () => {
93-
if (!silent) progressBar.start(100, 0);
94-
});
9575

9676
command.on("end", () => {
97-
if (!silent) progressBar.stop();
98-
if (!silent) console.log("Processing complete...");
9977
resolve({ path: output, stream: outputStream });
10078
});
10179

102-
command.on("progress", (progress) => {
103-
if (!silent) progressBar.update(percent(progress.percent));
104-
});
105-
10680
command.on("error", (err: Error) => {
107-
if (!silent)
108-
console.log("An error occured while processing,", err.message);
10981
reject(new Error(err.message));
11082
});
11183

apps/backend/src/canvas2video/renderer.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import ffmpeg from "fluent-ffmpeg";
2-
import { fabric } from "fabric";
2+
import * as fabric from "fabric/node";
33
import { Readable } from "stream";
4-
import * as cliProgress from "cli-progress";
54

65
import ffmpegPath from "ffmpeg-static";
76
import * as ffprobe from "ffprobe-static";
@@ -12,17 +11,10 @@ import { gsap } from "gsap";
1211
ffmpeg.setFfmpegPath(ffmpegPath);
1312
ffmpeg.setFfprobePath(ffprobe.path);
1413

15-
const progressBar = new cliProgress.SingleBar({
16-
format: `Rendering | {bar} | {percentage}%`,
17-
barCompleteChar: "\u2588",
18-
barIncompleteChar: "\u2591",
19-
hideCursor: true,
20-
});
21-
2214
const renderer: Renderer = (config) =>
2315
new Promise((resolve, reject) => {
2416
try {
25-
const { width, height, fps, makeScene, silent = true } = config;
17+
const { width, height, fps, makeScene } = config;
2618
const canvas = new fabric.StaticCanvas(null, { width, height });
2719
const anim = gsap.timeline({ paused: true });
2820
const stream = new Readable();
@@ -34,8 +26,6 @@ const renderer: Renderer = (config) =>
3426
const renderFrames = () => {
3527
anim.progress(currentFrame++ / totalFrames);
3628
if (currentFrame <= totalFrames) {
37-
if (!silent) progressBar.update(currentFrame);
38-
3929
canvas.renderAll();
4030
const buffer = Buffer.from(
4131
canvas.toDataURL().replace(/^data:\w+\/\w+;base64,/, ""),
@@ -44,8 +34,6 @@ const renderer: Renderer = (config) =>
4434
stream.push(buffer);
4535
renderFrames();
4636
} else {
47-
if (!silent) console.log("\nRendering complete...");
48-
if (!silent) progressBar.stop();
4937
stream.push(null);
5038
resolve(stream);
5139
}
@@ -55,7 +43,6 @@ const renderer: Renderer = (config) =>
5543
const duration = anim.duration();
5644
totalFrames = Math.max(1, Math.ceil((duration / 1) * fps));
5745

58-
if (!silent) progressBar.start(totalFrames, 0);
5946
renderFrames();
6047
});
6148
} catch (e) {

apps/backend/src/canvas2video/types.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { Readable, Writable } from "stream";
2-
import fabric from "fabric";
2+
import * as fabric from "fabric/node";
33

44
type mediaPath = string;
55

6-
interface BaseConfig {
7-
silent?: boolean;
8-
}
9-
10-
export interface EncoderConfig extends BaseConfig {
6+
export interface EncoderConfig {
117
frameStream: Readable;
128
output: mediaPath;
139
width: number;
@@ -32,13 +28,13 @@ interface EncoderOutput {
3228
}
3329

3430
type makeSceneFunction = (
35-
fabricInstance: typeof fabric.fabric,
36-
canvas: fabric.fabric.StaticCanvas,
31+
fabricInstance: typeof fabric,
32+
canvas: fabric.StaticCanvas,
3733
anim: gsap.core.Timeline,
3834
compose: () => void,
3935
) => void;
4036

41-
export interface RendererConfig extends BaseConfig {
37+
export interface RendererConfig {
4238
width: number;
4339
height: number;
4440
fps: number;

apps/backend/src/fonts/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export abstract class Fonts {
113113
weight: properties.weight ?? "normal",
114114
style: properties.style ?? "normal",
115115
};
116-
registerFont(fontFilePath, parsedProperties);
116+
await registerFont(fontFilePath, parsedProperties);
117117
if (!this._fonts[parsed.family]) this._fonts[parsed.family] = [];
118118
this._fonts[parsed.family]?.push(parsedProperties);
119119
logInfo(`Successfully registered font from ${fontFilePath}`);

apps/backend/src/video-generator/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ export const makeVideo = async (
131131
const height = 1080;
132132

133133
const stream = await renderer({
134-
silent: true,
135134
width,
136135
height,
137136
fps: 1,
@@ -147,7 +146,6 @@ export const makeVideo = async (
147146
});
148147

149148
const baseEncoderConfig: EncoderConfig = {
150-
silent: true,
151149
width: 1920,
152150
height: 1080,
153151
frameStream: stream,

0 commit comments

Comments
 (0)