Added typing for ffmpeg.js (#40276)

* added type for ffmpeg.js

* corrected usage

* corrected formatting

* fixed linter errors

* added fix for npm-naming error

* reverted back commit

* corrected exports

* reworked ffmpeg

* fixed according to linting errors

* fix

* declared function inside namespace; replaced back type

* rewrote package to match examples from package documentation
This commit is contained in:
Max Boguslavsky 2019-11-20 02:46:27 +03:00 committed by Pranav Senthilnathan
parent d1439a815f
commit c8e9c5a677
9 changed files with 343 additions and 0 deletions

39
types/ffmpeg.js/ffmpeg-mp4/index.d.ts vendored Normal file
View File

@ -0,0 +1,39 @@
// Project: https://github.com/Kagami/ffmpeg.js
// Definitions by: Vladimir Grenaderov <https://github.com/VladimirGrenaderov>,
// Max Boguslavskiy <https://github.com/maxbogus>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace ffmpeg {
interface Options {
arguments: string[];
MEMFS?: Video[];
print?(data: any): void;
printErr?(data: any): void;
onExit?(code: unknown): void;
stdin?(data: any): void;
mounts?: Mount[];
}
interface Opts {
root: string;
}
interface Mount {
type: string;
opts: Opts;
mountpoint: string;
}
interface Result {
MEMFS: Video[];
}
interface Video {
data: Uint8Array;
name: string;
}
}
declare function ffmpeg(opts: ffmpeg.Options): ffmpeg.Result;
export = ffmpeg;

39
types/ffmpeg.js/ffmpeg-webm/index.d.ts vendored Normal file
View File

@ -0,0 +1,39 @@
// Project: https://github.com/Kagami/ffmpeg.js
// Definitions by: Vladimir Grenaderov <https://github.com/VladimirGrenaderov>,
// Max Boguslavskiy <https://github.com/maxbogus>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace ffmpeg {
interface Options {
arguments: string[];
MEMFS?: Video[];
print?(data: any): void;
printErr?(data: any): void;
onExit?(code: unknown): void;
stdin?(data: any): void;
mounts?: Mount[];
}
interface Opts {
root: string;
}
interface Mount {
type: string;
opts: Opts;
mountpoint: string;
}
interface Result {
MEMFS: Video[];
}
interface Video {
data: Uint8Array;
name: string;
}
}
declare function ffmpeg(opts: ffmpeg.Options): ffmpeg.Result;
export = ffmpeg;

View File

@ -0,0 +1,30 @@
// Project: https://github.com/Kagami/ffmpeg.js
// Definitions by: Vladimir Grenaderov <https://github.com/VladimirGrenaderov>,
// Max Boguslavskiy <https://github.com/maxbogus>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace Worker {
interface Data {
type: string;
data: string;
}
interface PostMessageOptions {
type: string;
arguments: string[];
}
interface OnMessageOptions {
data: Data;
}
}
declare class Worker {
constructor(someParam?: string);
onmessage(opts: Worker.OnMessageOptions): void;
postMessage(opts: Worker.PostMessageOptions): void;
terminate(): void;
}
export = Worker;

View File

@ -0,0 +1,30 @@
// Project: https://github.com/Kagami/ffmpeg.js
// Definitions by: Vladimir Grenaderov <https://github.com/VladimirGrenaderov>,
// Max Boguslavskiy <https://github.com/maxbogus>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace Worker {
interface Data {
type: string;
data: string;
}
interface PostMessageOptions {
type: string;
arguments: string[];
}
interface OnMessageOptions {
data: Data;
}
}
declare class Worker {
constructor(someParam?: string);
onmessage(opts: Worker.OnMessageOptions): void;
postMessage(opts: Worker.PostMessageOptions): void;
terminate(): void;
}
export = Worker;

68
types/ffmpeg.js/index.d.ts vendored Normal file
View File

@ -0,0 +1,68 @@
// Type definitions for package ffmpeg.js 3.1
// Project: https://github.com/Kagami/ffmpeg.js
// Definitions by: Vladimir Grenaderov <https://github.com/VladimirGrenaderov>,
// Max Boguslavskiy <https://github.com/maxbogus>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
declare namespace ffmpeg {
interface Options {
arguments: string[];
MEMFS?: Video[];
print?(data: any): void;
printErr?(data: any): void;
onExit?(code: unknown): void;
stdin?(data: any): void;
mounts?: Mount[];
TOTAL_MEMORY?: number;
}
interface Opts {
root: string;
}
interface Mount {
type: string;
opts: Opts;
mountpoint: string;
}
interface Result {
MEMFS: Video[];
}
interface Video {
data: Uint8Array;
name: string;
}
namespace Worker {
interface Data {
type: string;
data: string;
}
interface PostMessageOptions {
type: string;
arguments: string[];
}
interface OnMessageOptions {
data: Data;
}
}
function ffmpeg(opts: Options): Result;
class Worker {
constructor(someParam?: string);
onmessage(opts: Worker.OnMessageOptions): void;
postMessage(opts: Worker.PostMessageOptions): void;
terminate(): void;
}
}
declare function ffmpeg(opts: ffmpeg.Options): ffmpeg.Result;
export = ffmpeg;

View File

@ -0,0 +1,55 @@
import ffmpeg, { Worker } from 'ffmpeg.js';
// test data
let stdout = "";
let stderr = "";
const worker = new Worker();
const testData = new Uint8Array(0);
// test cases
ffmpeg({
arguments: ["-version"],
print: (data: string) => { stdout += data + "\n"; },
printErr: (data: string) => { stderr += data + "\n"; },
onExit: (code: unknown) => {},
});
ffmpeg({
MEMFS: [{name: "test.webm", data: testData}],
arguments: ["-i", "test.webm", "-c:v", "libvpx", "-an", "out.webm"],
stdin: () => {},
});
ffmpeg({
mounts: [{type: "NODEFS", opts: {root: "."}, mountpoint: "/data"}],
arguments: ["-i", "/data/test.webm", "-c:v", "libvpx", "-an", "/data/out.webm"],
stdin: () => {},
});
const result = ffmpeg({
MEMFS: [{name: "test.webm", data: testData}],
TOTAL_MEMORY: 512 * 1024 * 1024,
arguments: ['-framerate', '-i'],
stdin: () => {},
print: () => {},
printErr: () => {},
onExit: (c: number) => {}
}).MEMFS[0];
worker.onmessage = (e: any) => {
const msg = e.data;
switch (msg.type) {
case "ready":
worker.postMessage({type: "run", arguments: ["-version"]});
break;
case "stdout":
stdout += msg.data + "\n";
break;
case "stderr":
stderr += msg.data + "\n";
break;
case "exit":
worker.terminate();
break;
}
};

View File

@ -0,0 +1,47 @@
import ffmpeg from 'ffmpeg.js/ffmpeg-mp4';
import Worker from 'ffmpeg.js/ffmpeg-worker-webm';
import OnMessageOptions = Worker.OnMessageOptions;
// test data
let stdout = "";
let stderr = "";
const worker = new Worker();
const testData = new Uint8Array(0);
// test cases
ffmpeg({
arguments: ["-version"],
print: (data: string) => { stdout += data + "\n"; },
printErr: (data: string) => { stderr += data + "\n"; },
onExit: () => {},
});
ffmpeg({
MEMFS: [{name: "test.webm", data: testData}],
arguments: ["-i", "test.webm", "-c:v", "libvpx", "-an", "out.webm"],
stdin: () => {},
});
ffmpeg({
mounts: [{type: "NODEFS", opts: {root: "."}, mountpoint: "/data"}],
arguments: ["-i", "/data/test.webm", "-c:v", "libvpx", "-an", "/data/out.webm"],
stdin: () => {},
});
worker.onmessage = (e: OnMessageOptions) => {
const msg = e.data;
switch (msg.type) {
case "ready":
worker.postMessage({type: "run", arguments: ["-version"]});
break;
case "stdout":
stdout += msg.data + "\n";
break;
case "stderr":
stderr += msg.data + "\n";
break;
case "exit":
worker.terminate();
break;
}
};

View File

@ -0,0 +1,29 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"esModuleInterop": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"ffmpeg-mp4/index.d.ts",
"ffmpeg-webm/index.d.ts",
"ffmpeg-worker-mp4/index.d.ts",
"ffmpeg-worker-webm/index.d.ts",
"test/ffmpeg.js-tests.ts",
"test/ffmpeg.js-direct-tests.ts"
]
}

View File

@ -0,0 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
"npm-naming": false
}
}