|
| 1 | +type FSMethodNames = { [K in keyof typeof FS]: (typeof FS)[K] extends (...args: any[]) => any ? K : never }[keyof typeof FS]; |
| 2 | +type FSMethodArgs = { [K in FSMethodNames]: Parameters<(typeof FS)[K]> }; |
| 3 | +type FSMethodReturn = { [K in FSMethodNames]: ReturnType<(typeof FS)[K]> }; |
| 4 | + |
| 5 | +type LogCallback = (logParams: { type: string; message: string }) => any; |
| 6 | +type ProgressCallback = (progressParams: { ratio: number }) => any; |
| 7 | + |
| 8 | +export interface CreateFFmpegOptions { |
| 9 | + /** path for ffmpeg-core.js script */ |
| 10 | + corePath?: string; |
| 11 | + /** a boolean to turn on all logs, default is false */ |
| 12 | + log?: boolean; |
| 13 | + /** a function to get log messages, a quick example is ({ message }) => console.log(message) */ |
| 14 | + logger?: LogCallback; |
| 15 | + /** a function to trace the progress, a quick example is p => console.log(p) */ |
| 16 | + progress?: ProgressCallback; |
| 17 | +} |
| 18 | + |
| 19 | +export interface FFmpeg { |
| 20 | + /* |
| 21 | + * Load ffmpeg.wasm-core script. |
| 22 | + * In browser environment, the ffmpeg.wasm-core script is fetch from |
| 23 | + * CDN and can be assign to a local path by assigning `corePath`. |
| 24 | + * In node environment, we use dynamic require and the default `corePath` |
| 25 | + * is `$ffmpeg/core`. |
| 26 | + * |
| 27 | + * Typically the load() func might take few seconds to minutes to complete, |
| 28 | + * better to do it as early as possible. |
| 29 | + * |
| 30 | + */ |
| 31 | + load(): Promise<void>; |
| 32 | + /* |
| 33 | + * Determine whether the Core is loaded. |
| 34 | + */ |
| 35 | + isLoaded(): boolean; |
| 36 | + /* |
| 37 | + * Run ffmpeg command. |
| 38 | + * This is the major function in ffmpeg.wasm, you can just imagine it |
| 39 | + * as ffmpeg native cli and what you need to pass is the same. |
| 40 | + * |
| 41 | + * For example, you can convert native command below: |
| 42 | + * |
| 43 | + * ``` |
| 44 | + * $ ffmpeg -i video.avi -c:v libx264 video.mp4 |
| 45 | + * ``` |
| 46 | + * |
| 47 | + * To |
| 48 | + * |
| 49 | + * ``` |
| 50 | + * await ffmpeg.run('-i', 'video.avi', '-c:v', 'libx264', 'video.mp4'); |
| 51 | + * ``` |
| 52 | + * |
| 53 | + */ |
| 54 | + run(...args: string[]): Promise<void>; |
| 55 | + /* |
| 56 | + * Run FS operations. |
| 57 | + * For input/output file of ffmpeg.wasm, it is required to save them to MEMFS |
| 58 | + * first so that ffmpeg.wasm is able to consume them. Here we rely on the FS |
| 59 | + * methods provided by Emscripten. |
| 60 | + * |
| 61 | + * Common methods to use are: |
| 62 | + * ffmpeg.FS('writeFile', 'video.avi', new Uint8Array(...)): writeFile writes |
| 63 | + * data to MEMFS. You need to use Uint8Array for binary data. |
| 64 | + * ffmpeg.FS('readFile', 'video.mp4'): readFile from MEMFS. |
| 65 | + * ffmpeg.FS('unlink', 'video.map'): delete file from MEMFS. |
| 66 | + * |
| 67 | + * For more info, check https://emscripten.org/docs/api_reference/Filesystem-API.html |
| 68 | + * |
| 69 | + */ |
| 70 | + FS<Method extends FSMethodNames>(method: Method, ...args: FSMethodArgs[Method]): FSMethodReturn[Method]; |
| 71 | + setProgress(progress: ProgressCallback): void; |
| 72 | + setLogger(log: LogCallback): void; |
| 73 | + setLogging(logging: boolean): void; |
| 74 | +} |
| 75 | + |
| 76 | +/* |
| 77 | + * Create ffmpeg instance. |
| 78 | + * Each ffmpeg instance owns an isolated MEMFS and works |
| 79 | + * independently. |
| 80 | + * |
| 81 | + * For example: |
| 82 | + * |
| 83 | + * ``` |
| 84 | + * const ffmpeg = createFFmpeg({ |
| 85 | + * log: true, |
| 86 | + * logger: () => {}, |
| 87 | + * progress: () => {}, |
| 88 | + * corePath: '', |
| 89 | + * }) |
| 90 | + * ``` |
| 91 | + * |
| 92 | + * For the usage of these four arguments, check config.js |
| 93 | + * |
| 94 | + */ |
| 95 | +export function createFFmpeg(options?: CreateFFmpegOptions): FFmpeg; |
| 96 | +/* |
| 97 | + * Helper function for fetching files from various resource. |
| 98 | + * Sometimes the video/audio file you want to process may located |
| 99 | + * in a remote URL and somewhere in your local file system. |
| 100 | + * |
| 101 | + * This helper function helps you to fetch to file and return an |
| 102 | + * Uint8Array variable for ffmpeg.wasm to consume. |
| 103 | + * |
| 104 | + */ |
| 105 | +export function fetchFile(data: string | Buffer | Blob | File): Uint8Array; |
0 commit comments