improve ready

This commit is contained in:
Abhinav 2022-03-07 19:12:44 +05:30
parent e8a1f8b042
commit df3eaa8a00

View file

@ -5,15 +5,18 @@ import { parseFFmpegExtractedMetadata } from 'utils/ffmpeg';
class FFmpegClient { class FFmpegClient {
private ffmpeg: FFmpeg; private ffmpeg: FFmpeg;
private fileReader: FileReader; private fileReader: FileReader;
private ready: Promise<void> = null;
constructor() { constructor() {
this.ffmpeg = createFFmpeg({ this.ffmpeg = createFFmpeg({
corePath: '/js/ffmpeg/ffmpeg-core.js', corePath: '/js/ffmpeg/ffmpeg-core.js',
log: true, log: true,
mt: false, mt: false,
}); });
this.ready = this.init();
} }
private async ready() { private async init() {
if (!this.ffmpeg.isLoaded()) { if (!this.ffmpeg.isLoaded()) {
await this.ffmpeg.load(); await this.ffmpeg.load();
} }
@ -23,7 +26,7 @@ class FFmpegClient {
} }
async generateThumbnail(file: File) { async generateThumbnail(file: File) {
await this.ready(); await this.ready;
const inputFileName = `${Date.now().toString()}-${file.name}`; const inputFileName = `${Date.now().toString()}-${file.name}`;
const thumbFileName = `${Date.now().toString()}-thumb.jpeg`; const thumbFileName = `${Date.now().toString()}-thumb.jpeg`;
this.ffmpeg.FS( this.ffmpeg.FS(
@ -58,7 +61,7 @@ class FFmpegClient {
} }
async extractVideoMetadata(file: File) { async extractVideoMetadata(file: File) {
await this.ready(); await this.ready;
const inputFileName = `${Date.now().toString()}-${file.name}`; const inputFileName = `${Date.now().toString()}-${file.name}`;
const outFileName = `${Date.now().toString()}-metadata.txt`; const outFileName = `${Date.now().toString()}-metadata.txt`;
this.ffmpeg.FS( this.ffmpeg.FS(
@ -90,7 +93,7 @@ class FFmpegClient {
} }
async convertToMP4(file: Uint8Array, inputFileName: string) { async convertToMP4(file: Uint8Array, inputFileName: string) {
await this.ready(); await this.ready;
this.ffmpeg.FS('writeFile', inputFileName, file); this.ffmpeg.FS('writeFile', inputFileName, file);
await this.ffmpeg.run( await this.ffmpeg.run(
'-i', '-i',