refactor transcode method
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { ipcMain, IpcMainEvent } from "electron";
|
||||
import { CacheObject } from "@main/db/models";
|
||||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
import db from "@main/db";
|
||||
import settings from "@main/settings";
|
||||
|
||||
class CacheObjectsHandler {
|
||||
private async get(event: IpcMainEvent, key: string) {
|
||||
@@ -61,11 +64,23 @@ class CacheObjectsHandler {
|
||||
});
|
||||
}
|
||||
|
||||
private async writeFile(
|
||||
_event: IpcMainEvent,
|
||||
filename: string,
|
||||
data: ArrayBuffer
|
||||
) {
|
||||
const output = path.join(settings.cachePath(), filename);
|
||||
fs.writeFileSync(output, Buffer.from(data));
|
||||
|
||||
return output.replace(settings.libraryPath(), "enjoy://library");
|
||||
}
|
||||
|
||||
register() {
|
||||
ipcMain.handle("cache-objects-get", this.get);
|
||||
ipcMain.handle("cache-objects-set", this.set);
|
||||
ipcMain.handle("cache-objects-delete", this.delete);
|
||||
ipcMain.handle("cache-objects-clear", this.clear);
|
||||
ipcMain.handle("cache-objects-write-file", this.writeFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -426,6 +426,9 @@ contextBridge.exposeInMainWorld("__ENJOY_APP__", {
|
||||
clear: () => {
|
||||
return ipcRenderer.invoke("cache-objects-clear");
|
||||
},
|
||||
writeFile: (filename: string, data: ArrayBuffer) => {
|
||||
return ipcRenderer.invoke("cache-objects-write-file", filename, data);
|
||||
},
|
||||
},
|
||||
transcriptions: {
|
||||
findOrCreate: (params: any) => {
|
||||
|
||||
@@ -118,7 +118,7 @@ const RecordButtonPopover = (props: {
|
||||
onRecordEnd: (blob: Blob, duration: number) => void;
|
||||
}) => {
|
||||
const containerRef = useRef<HTMLDivElement>();
|
||||
const { transcodeUsingWasm } = useTranscribe();
|
||||
const { transcode } = useTranscribe();
|
||||
|
||||
useEffect(() => {
|
||||
if (!containerRef.current) return;
|
||||
@@ -140,7 +140,7 @@ const RecordButtonPopover = (props: {
|
||||
|
||||
record.on("record-end", async (blob: Blob) => {
|
||||
const duration = Date.now() - startAt;
|
||||
const output = await transcodeUsingWasm(blob);
|
||||
const output = await transcode(blob);
|
||||
props.onRecordEnd(output, duration);
|
||||
});
|
||||
|
||||
|
||||
@@ -81,7 +81,10 @@ export const AppSettingsProvider = ({
|
||||
const prepareFfmpeg = async () => {
|
||||
const valid = await EnjoyApp.ffmpeg.check();
|
||||
setFfmpegValid(valid);
|
||||
loadFfmpegWASM();
|
||||
|
||||
if (!valid) {
|
||||
loadFfmpegWASM();
|
||||
}
|
||||
};
|
||||
|
||||
const loadFfmpegWASM = async () => {
|
||||
|
||||
@@ -24,8 +24,15 @@ export const useTranscribe = () => {
|
||||
);
|
||||
const { whisperConfig, openai } = useContext(AISettingsProviderContext);
|
||||
|
||||
const transcode = async (src: string, options?: string[]) => {
|
||||
const transcode = async (src: string | Blob, options?: string[]) => {
|
||||
if (ffmpegValid) {
|
||||
if (src instanceof Blob) {
|
||||
src = await EnjoyApp.cacheObjects.writeFile(
|
||||
`${Date.now()}.${src.type.split("/")[1]}`,
|
||||
await src.arrayBuffer()
|
||||
);
|
||||
}
|
||||
|
||||
const output = `enjoy://library/cache/${src.split("/").pop()}.wav`;
|
||||
await EnjoyApp.ffmpeg.transcode(src, output, options);
|
||||
const data = await fetchFile(output);
|
||||
@@ -284,7 +291,6 @@ export const useTranscribe = () => {
|
||||
|
||||
return {
|
||||
transcode,
|
||||
transcodeUsingWasm,
|
||||
transcribe,
|
||||
};
|
||||
};
|
||||
|
||||
1
enjoy/src/types/enjoy-app.d.ts
vendored
1
enjoy/src/types/enjoy-app.d.ts
vendored
@@ -247,6 +247,7 @@ type EnjoyAppType = {
|
||||
set: (key: string, value: any, ttl?: number) => Promise<void>;
|
||||
delete: (key: string) => Promise<void>;
|
||||
clear: () => Promise<void>;
|
||||
writeFile: (filename: string, data: ArrayBuffer) => Promise<string>;
|
||||
};
|
||||
transcriptions: {
|
||||
findOrCreate: (params: any) => Promise<TranscriptionType>;
|
||||
|
||||
Reference in New Issue
Block a user