Revert "update to use single use ffmpeg st"

This reverts commit 65667ac416.
This commit is contained in:
Abhinav 2022-03-07 13:07:25 +05:30
parent b8bd8acc28
commit ab64c84f64
3 changed files with 109 additions and 87 deletions

View file

@ -14,6 +14,7 @@
},
"dependencies": {
"@ente-io/next-with-workbox": "^1.0.3",
"ffmpeg-wasm": "file:./thirdparty/ffmpeg-wasm",
"@sentry/nextjs": "^6.7.1",
"@stripe/stripe-js": "^1.13.2",
"@typescript-eslint/eslint-plugin": "^4.25.0",
@ -30,7 +31,6 @@
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react-hooks": "^4.2.0",
"exifr": "^7.1.3",
"ffmpeg-wasm": "file:./thirdparty/ffmpeg-wasm",
"file-type": "^16.5.3",
"formik": "^2.1.5",
"heic-convert": "^1.2.4",

View file

@ -8,11 +8,44 @@ import { getUint8ArrayView } from './upload/readFileService';
import { parseFFmpegExtractedMetadata } from './upload/videoMetadataService';
class FFmpegService {
private ffmpeg: FFmpeg = null;
private isLoading = null;
private fileReader: FileReader = null;
private ffmpegTaskQueue = new QueueProcessor<any>(1);
async init() {
try {
this.ffmpeg = createFFmpeg({
corePath: '/js/ffmpeg/ffmpeg-core.js',
});
this.isLoading = this.ffmpeg.load();
await this.isLoading;
this.isLoading = null;
} catch (e) {
logError(e, 'ffmpeg load failed');
this.ffmpeg = null;
this.isLoading = null;
throw e;
}
}
async generateThumbnail(file: File): Promise<Uint8Array> {
if (!this.ffmpeg) {
await this.init();
}
if (!this.fileReader) {
this.fileReader = new FileReader();
}
if (this.isLoading) {
await this.isLoading;
}
const response = this.ffmpegTaskQueue.queueUpRequest(
generateThumbnailHelper.bind(null, file)
generateThumbnailHelper.bind(
null,
this.ffmpeg,
this.fileReader,
file
)
);
try {
return await response.promise;
@ -28,8 +61,22 @@ class FFmpegService {
}
async extractMetadata(file: File): Promise<ParsedExtractedMetadata> {
if (!this.ffmpeg) {
await this.init();
}
if (!this.fileReader) {
this.fileReader = new FileReader();
}
if (this.isLoading) {
await this.isLoading;
}
const response = this.ffmpegTaskQueue.queueUpRequest(
extractVideoMetadataHelper.bind(null, file)
extractVideoMetadataHelper.bind(
null,
this.ffmpeg,
this.fileReader,
file
)
);
try {
return await response.promise;
@ -44,55 +91,41 @@ class FFmpegService {
}
}
// async convertToMP4(
// file: Uint8Array,
// fileName: string
// ): Promise<Uint8Array> {
// if (!this.ffmpeg) {
// await this.init();
// }
// if (this.isLoading) {
// await this.isLoading;
// }
async convertToMP4(
file: Uint8Array,
fileName: string
): Promise<Uint8Array> {
if (!this.ffmpeg) {
await this.init();
}
if (this.isLoading) {
await this.isLoading;
}
// const response = this.ffmpegTaskQueue.queueUpRequest(
// convertToMP4Helper.bind(null, this.ffmpeg, file, fileName)
// );
const response = this.ffmpegTaskQueue.queueUpRequest(
convertToMP4Helper.bind(null, this.ffmpeg, file, fileName)
);
// try {
// return await response.promise;
// } catch (e) {
// if (e.message === CustomError.REQUEST_CANCELLED) {
// // ignore
// return null;
// } else {
// logError(e, 'ffmpeg MP4 conversion failed');
// throw e;
// }
// }
// }
}
async function getFFmpegInstance(): Promise<FFmpeg> {
try {
const ffmpeg = createFFmpeg({
corePath: '/js/ffmpeg/ffmpeg-core.js',
mt: false,
// log: true,
});
await ffmpeg.load();
return ffmpeg;
} catch (e) {
logError(e, 'ffmpeg load failed');
this.ffmpeg = null;
this.isLoading = null;
throw e;
try {
return await response.promise;
} catch (e) {
if (e.message === CustomError.REQUEST_CANCELLED) {
// ignore
return null;
} else {
logError(e, 'ffmpeg MP4 conversion failed');
throw e;
}
}
}
}
async function generateThumbnailHelper(file: File) {
const ffmpeg = await getFFmpegInstance();
async function generateThumbnailHelper(
ffmpeg: FFmpeg,
reader: FileReader,
file: File
) {
try {
const reader = new FileReader();
const inputFileName = `${Date.now().toString()}-${file.name}`;
const thumbFileName = `${Date.now().toString()}-thumb.jpeg`;
ffmpeg.FS(
@ -123,24 +156,19 @@ async function generateThumbnailHelper(file: File) {
}
}
ffmpeg.FS('unlink', inputFileName);
return thumb;
} catch (e) {
logError(e, 'ffmpeg thumbnail generation failed');
throw e;
} finally {
try {
ffmpeg.exit();
} catch (e) {
// console.log(e);
}
}
}
async function extractVideoMetadataHelper(file: File) {
const ffmpeg = await getFFmpegInstance();
async function extractVideoMetadataHelper(
ffmpeg: FFmpeg,
reader: FileReader,
file: File
) {
try {
const reader = new FileReader();
const inputFileName = `${Date.now().toString()}-${file.name}`;
const outFileName = `${Date.now().toString()}-metadata.txt`;
ffmpeg.FS(
@ -172,37 +200,31 @@ async function extractVideoMetadataHelper(file: File) {
} catch (e) {
logError(e, 'ffmpeg metadata extraction failed');
throw e;
} finally {
try {
ffmpeg.exit();
} catch (e) {
// console.log(e);
}
}
}
// async function convertToMP4Helper(
// ffmpeg: FFmpeg,
// file: Uint8Array,
// inputFileName: string
// ) {
// try {
// ffmpeg.FS('writeFile', inputFileName, file);
// await ffmpeg.run(
// '-i',
// inputFileName,
// '-preset',
// 'ultrafast',
// 'output.mp4'
// );
// const convertedFile = ffmpeg.FS('readFile', 'output.mp4');
// ffmpeg.FS('unlink', inputFileName);
// ffmpeg.FS('unlink', 'output.mp4');
// return convertedFile;
// } catch (e) {
// logError(e, 'ffmpeg MP4 conversion failed');
// throw e;
// }
// }
async function convertToMP4Helper(
ffmpeg: FFmpeg,
file: Uint8Array,
inputFileName: string
) {
try {
ffmpeg.FS('writeFile', inputFileName, file);
await ffmpeg.run(
'-i',
inputFileName,
'-preset',
'ultrafast',
'output.mp4'
);
const convertedFile = ffmpeg.FS('readFile', 'output.mp4');
ffmpeg.FS('unlink', inputFileName);
ffmpeg.FS('unlink', 'output.mp4');
return convertedFile;
} catch (e) {
logError(e, 'ffmpeg MP4 conversion failed');
throw e;
}
}
export default new FFmpegService();

@ -1 +1 @@
Subproject commit e6d21bb88f38deaca568a5e9985214232de7db63
Subproject commit 321fd909f62924e3d94035846fa33b95155e3dba