Skip to content

Instantly share code, notes, and snippets.

@nukotsuka
Created December 3, 2021 12:09
Show Gist options
  • Save nukotsuka/a2cffa07df6809f6652f1876a11e7cf9 to your computer and use it in GitHub Desktop.
Save nukotsuka/a2cffa07df6809f6652f1876a11e7cf9 to your computer and use it in GitHub Desktop.
ffmpeg dart code generated by dart_js_facade_gen with @ffmpeg/ffmpeg
@JS()
library node_modules._ffmpeg.ffmpeg.src;
import "package:js/js.dart";
import "package:js/js_util.dart" show promiseToFuture;
@JS()
external dynamic
/*{
writeFile: (fileName: string, binaryData: Uint8Array) => void,
readFile: (fileName: string) => Uint8Array,
unlink: (fileName: string) => void,
}*/
get FS; /*type FSMethodNames = { [K in keyof typeof FS]: (typeof FS)[K] extends (...args: any[]) => any ? K : never }[keyof typeof FS];*/
/*
Warning: Mapped types are not supported in Dart. Uses of this type will be replaced by dynamic.
type FSMethodArgs = { [K in FSMethodNames]: Parameters<(typeof FS)[K]> };
*/
/*
Warning: Mapped types are not supported in Dart. Uses of this type will be replaced by dynamic.
type FSMethodReturn = { [K in FSMethodNames]: ReturnType<(typeof FS)[K]> };
*/
typedef dynamic LogCallback(
dynamic /*{ type: string; message: string }*/ logParams);
typedef dynamic ProgressCallback(dynamic /*{ ratio: number }*/ progressParams);
@anonymous
@JS()
abstract class CreateFFmpegOptions {
/// path for ffmpeg-core.js script
external String get corePath;
external set corePath(String v);
/// a boolean to turn on all logs, default is false
external bool get log;
external set log(bool v);
/// a function to get log messages, a quick example is ({ message }) => console.log(message)
external LogCallback get logger;
external set logger(LogCallback v);
/// a function to trace the progress, a quick example is p => console.log(p)
external ProgressCallback get progress;
external set progress(ProgressCallback v);
external factory CreateFFmpegOptions(
{String corePath,
bool log,
LogCallback logger,
ProgressCallback progress});
}
@anonymous
@JS()
abstract class FFmpeg {
/// Determine whether the Core is loaded.
external bool isLoaded();
/// 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
external dynamic /*dynamic/*FSMethodReturn*/[Method]*/ FS/*<Method extends dynamic[keyof dynamic]>*/(
dynamic /*Method*/ method,
[args1,
args2,
args3,
args4,
args5]);
external void setProgress(ProgressCallback progress);
external void setLogger(LogCallback log);
external void setLogging(bool logging);
external void exit();
}
@anonymous
@JS()
abstract class _FFmpeg {
external Promise<void> load();
external Promise<void> run(
[String args1, String args2, String args3, String args4, String args5]);
}
extension FFmpegExtensions on FFmpeg {
Future<void> load() {
final Object t = this;
final _FFmpeg tt = t;
return promiseToFuture(tt.load());
}
Future<void> run(
[String args1, String args2, String args3, String args4, String args5]) {
final Object t = this;
final _FFmpeg tt = t;
return promiseToFuture(tt.run(args));
}
}
/// 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
@JS()
external FFmpeg createFFmpeg([CreateFFmpegOptions options]);
@JS()
abstract class Promise<T> {
external factory Promise(
void executor(void resolve(T result), Function reject));
external Promise then(void onFulfilled(T result), [Function onRejected]);
}
@nukotsuka
Copy link
Author

Usage

Add js package to pubspec.yaml.

dependencies:
  js: ^0.6.2

Add script to index.html.

<!DOCTYPE html>
<html>
<head>
  ...
  <script src="https://unpkg.com/@ffmpeg/ffmpeg@0.10.0/dist/ffmpeg.min.js"></script>
  ...
</head>
...
</html>

And add ffmpeg.dart listed above to your project, then you can use liff.

final ffmpeg = createFFmpeg();
await ffmpeg.load();
ffmpeg.FS('writeFile', 'test.avi', await fetchFile('./test.avi'));
await ffmpeg.run('-i', 'test.avi', 'test.mp4');
await fs.promises.writeFile('./test.mp4', ffmpeg.FS('readFile', 'test.mp4'));

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment