From b89adcfbacff90ca486323ce4df1eb7d7b06b885 Mon Sep 17 00:00:00 2001 From: Narazaka Date: Wed, 4 Nov 2020 18:32:47 +0900 Subject: [PATCH] add TypeScript definition --- package-lock.json | 6 +++ package.json | 2 + src/index.d.ts | 105 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 src/index.d.ts diff --git a/package-lock.json b/package-lock.json index 1dee1ef..1be2206 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2240,6 +2240,12 @@ "integrity": "sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ==", "dev": true }, + "@types/emscripten": { + "version": "1.39.4", + "resolved": "https://registry.npmjs.org/@types/emscripten/-/emscripten-1.39.4.tgz", + "integrity": "sha512-k3LLVMFrdNA9UCvMDPWMbFrGPNb+GcPyw29ktJTo1RCN7RmxFG5XzPZcPKRlnLuLT/FRm8wp4ohvDwNY7GlROQ==", + "dev": true + }, "@types/eslint": { "version": "7.2.4", "resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-7.2.4.tgz", diff --git a/package.json b/package.json index d2f46d1..89260f9 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.9.3", "description": "FFmpeg WebAssembly version", "main": "src/index.js", + "types": "src/index.d.ts", "directories": { "example": "examples" }, @@ -50,6 +51,7 @@ "@babel/core": "^7.12.3", "@babel/preset-env": "^7.12.1", "@ffmpeg/core": "^0.8.2", + "@types/emscripten": "^1.39.4", "babel-loader": "^8.1.0", "chai": "^4.2.0", "cors": "^2.8.5", diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..acaef31 --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,105 @@ +type FSMethodNames = { [K in keyof typeof FS]: (typeof FS)[K] extends (...args: any[]) => any ? K : never }[keyof typeof FS]; +type FSMethodArgs = { [K in FSMethodNames]: Parameters<(typeof FS)[K]> }; +type FSMethodReturn = { [K in FSMethodNames]: ReturnType<(typeof FS)[K]> }; + +type LogCallback = (logParams: { type: string; message: string }) => any; +type ProgressCallback = (progressParams: { ratio: number }) => any; + +export interface CreateFFmpegOptions { + /** path for ffmpeg-core.js script */ + corePath?: string; + /** a boolean to turn on all logs, default is false */ + log?: boolean; + /** a function to get log messages, a quick example is ({ message }) => console.log(message) */ + logger?: LogCallback; + /** a function to trace the progress, a quick example is p => console.log(p) */ + progress?: ProgressCallback; +} + +export interface FFmpeg { + /* + * Load ffmpeg.wasm-core script. + * In browser environment, the ffmpeg.wasm-core script is fetch from + * CDN and can be assign to a local path by assigning `corePath`. + * In node environment, we use dynamic require and the default `corePath` + * is `$ffmpeg/core`. + * + * Typically the load() func might take few seconds to minutes to complete, + * better to do it as early as possible. + * + */ + load(): Promise; + /* + * Determine whether the Core is loaded. + */ + isLoaded(): boolean; + /* + * Run ffmpeg command. + * This is the major function in ffmpeg.wasm, you can just imagine it + * as ffmpeg native cli and what you need to pass is the same. + * + * For example, you can convert native command below: + * + * ``` + * $ ffmpeg -i video.avi -c:v libx264 video.mp4 + * ``` + * + * To + * + * ``` + * await ffmpeg.run('-i', 'video.avi', '-c:v', 'libx264', 'video.mp4'); + * ``` + * + */ + run(...args: string[]): Promise; + /* + * Run FS operations. + * For input/output file of ffmpeg.wasm, it is required to save them to MEMFS + * first so that ffmpeg.wasm is able to consume them. Here we rely on the FS + * methods provided by Emscripten. + * + * Common methods to use are: + * ffmpeg.FS('writeFile', 'video.avi', new Uint8Array(...)): writeFile writes + * data to MEMFS. You need to use Uint8Array for binary data. + * ffmpeg.FS('readFile', 'video.mp4'): readFile from MEMFS. + * ffmpeg.FS('unlink', 'video.map'): delete file from MEMFS. + * + * For more info, check https://emscripten.org/docs/api_reference/Filesystem-API.html + * + */ + FS(method: Method, ...args: FSMethodArgs[Method]): FSMethodReturn[Method]; + setProgress(progress: ProgressCallback): void; + setLogger(log: LogCallback): void; + setLogging(logging: boolean): void; +} + +/* + * Create ffmpeg instance. + * Each ffmpeg instance owns an isolated MEMFS and works + * independently. + * + * For example: + * + * ``` + * const ffmpeg = createFFmpeg({ + * log: true, + * logger: () => {}, + * progress: () => {}, + * corePath: '', + * }) + * ``` + * + * For the usage of these four arguments, check config.js + * + */ +export function createFFmpeg(options?: CreateFFmpegOptions): FFmpeg; +/* + * Helper function for fetching files from various resource. + * Sometimes the video/audio file you want to process may located + * in a remote URL and somewhere in your local file system. + * + * This helper function helps you to fetch to file and return an + * Uint8Array variable for ffmpeg.wasm to consume. + * + */ +export function fetchFile(data: string | Buffer | Blob | File): Uint8Array;