Skip to content

Commit

Permalink
Merge pull request #50 from Lumen5/feat/remove-canvas-deps
Browse files Browse the repository at this point in the history
feat(*): remove canvas dependency
  • Loading branch information
stepancar authored Jan 4, 2024
2 parents 689b02b + 5464578 commit 520eaa4
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 22 deletions.
1 change: 0 additions & 1 deletion examples/mini-framefusion-example/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default defineConfig({
'path',
'fs',
'@pixi/node',
'canvas',
'framefusion',
'child_process',
'rimraf',
Expand Down
2 changes: 1 addition & 1 deletion framefusion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ImageData } from 'canvas';
import type { ImageData } from './src/types';
import { BeamcoderExtractor } from './src/backends/beamcoder.js';

export type Frame = {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lumen5/framefusion",
"version": "0.0.26",
"version": "1.0.0",
"type": "module",
"scripts": {
"docs": "typedoc framefusion.ts",
Expand All @@ -20,7 +20,6 @@
},
"dependencies": {
"@antoinemopa/beamcoder": "^0.7.4",
"canvas": "^2.9.1",
"fs-extra": "^11.1.1",
"tmp": "^0.2.1"
},
Expand All @@ -36,6 +35,7 @@
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"@vitest/ui": "^0.29.8",
"canvas": "^2.9.1",
"eslint": "^8.38.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.27.5",
Expand Down
4 changes: 2 additions & 2 deletions src/BaseExtractor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { ImageData } from 'canvas';

import type {
ExtractorArgs,
Frame,
Extractor
} from '../framefusion';
import type { ImageData } from './types';


export class BaseExtractor implements Extractor {
static async create(args: ExtractorArgs): Promise<Extractor> {
Expand Down
15 changes: 7 additions & 8 deletions src/backends/beamcoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import type {
Frame
} from '@antoinemopa/beamcoder';
import beamcoder from '@antoinemopa/beamcoder';
import type { ImageData } from 'canvas';
import { createImageData } from 'canvas';
import type { ImageData } from '../types';
import { BaseExtractor } from '../BaseExtractor';
import type { Extractor, ExtractorArgs, InterpolateMode } from '../../framefusion';
import { DownloadVideoURL } from '../DownloadVideoURL';
Expand Down Expand Up @@ -256,12 +255,12 @@ export class BeamcoderExtractor extends BaseExtractor implements Extractor {
return null;
}
const rawData = this._resizeFrameData(frame);
const image = createImageData(
rawData,
frame.width,
frame.height
) as ImageData;
return image;

return {
data: rawData,
width: frame.width,
height: frame.height,
};
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type ImageData = {
data: Uint8ClampedArray;
width: number;
height: number;
};
23 changes: 15 additions & 8 deletions test/framefusion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
beforeAll
} from 'vitest';
import { toMatchImageSnapshot } from 'jest-image-snapshot';
import { createCanvas } from 'canvas';
import { createCanvas, createImageData } from 'canvas';
import httpServer from 'http-server';
import { BeamcoderExtractor } from '../src/backends/beamcoder';

Expand Down Expand Up @@ -171,12 +171,13 @@ describe('FrameFusion', () => {

for (let i = 0; i < times.length; i++) {
const imageData = await extractor.getImageDataAtTime(times[i]); // 3
const canvasImageData = createImageData(imageData.data, imageData.width, imageData.height);
if (!imageData) {
throw new Error(`Failed to get image data for time ${times[i]}`);
}
const canvas = createCanvas(imageData.width, imageData.height);
const ctx = canvas.getContext('2d');
ctx.putImageData(imageData, 0, 0);
ctx.putImageData(canvasImageData, 0, 0);
expect(canvas.toBuffer('image/png')).toMatchImageSnapshot();
}

Expand All @@ -202,9 +203,10 @@ describe('FrameFusion', () => {
if (!imageData) {
throw new Error(`Failed to get image data for time ${times[i]}`);
}
const canvasImageData = createImageData(imageData.data, imageData.width, imageData.height);
const canvas = createCanvas(imageData.width, imageData.height);
const ctx = canvas.getContext('2d');
ctx.putImageData(imageData, 0, 0);
ctx.putImageData(canvasImageData, 0, 0);
expect(canvas.toBuffer('image/png')).toMatchImageSnapshot();
}

Expand Down Expand Up @@ -297,9 +299,10 @@ describe('FrameFusion', () => {
if (!imageData) {
continue;
}
const canvasImageData = createImageData(imageData.data, imageData.width, imageData.height);
const canvas = createCanvas(imageData.width, imageData.height);
const ctx = canvas.getContext('2d');
ctx.putImageData(imageData, 0, 0);
ctx.putImageData(canvasImageData, 0, 0);
expect(canvas.toBuffer('image/png')).toMatchImageSnapshot();
}

Expand Down Expand Up @@ -333,9 +336,10 @@ describe('FrameFusion', () => {
if (!imageData) {
continue;
}
const canvasImageData = createImageData(imageData.data, imageData.width, imageData.height);
const canvas = createCanvas(imageData.width, imageData.height);
const ctx = canvas.getContext('2d');
ctx.putImageData(imageData, 0, 0);
ctx.putImageData(canvasImageData, 0, 0);
expect(canvas.toBuffer('image/png')).toMatchImageSnapshot();
}

Expand All @@ -359,9 +363,10 @@ describe('FrameFusion', () => {
if (!imageData) {
continue;
}
const canvasImageData = createImageData(imageData.data, imageData.width, imageData.height);
const canvas = createCanvas(imageData.width, imageData.height);
const ctx = canvas.getContext('2d');
ctx.putImageData(imageData, 0, 0);
ctx.putImageData(canvasImageData, 0, 0);
expect(canvas.toBuffer('image/png')).toMatchImageSnapshot();
}

Expand Down Expand Up @@ -389,9 +394,10 @@ describe('FrameFusion', () => {
if (!imageData) {
continue;
}
const canvasImageData = createImageData(imageData.data, imageData.width, imageData.height);
const canvas = createCanvas(imageData.width, imageData.height);
const ctx = canvas.getContext('2d');
ctx.putImageData(imageData, 0, 0);
ctx.putImageData(canvasImageData, 0, 0);
expect(canvas.toBuffer('image/png')).toMatchImageSnapshot();
}

Expand All @@ -415,9 +421,10 @@ describe('FrameFusion', () => {
if (!imageData) {
continue;
}
const canvasImageData = createImageData(imageData.data, imageData.width, imageData.height);
const canvas = createCanvas(imageData.width, imageData.height);
const ctx = canvas.getContext('2d');
ctx.putImageData(imageData, 0, 0);
ctx.putImageData(canvasImageData, 0, 0);
expect(canvas.toBuffer('image/png')).toMatchImageSnapshot();
}

Expand Down

0 comments on commit 520eaa4

Please sign in to comment.