Skip to content

Commit

Permalink
feat: support windows package Closes #3 (#6)
Browse files Browse the repository at this point in the history
* feat: support windows package

* feat: update readme

---------

Co-authored-by: linxiaodong <[email protected]>
  • Loading branch information
buxuku and linxiaodong authored Jun 10, 2024
1 parent 392a34a commit 60442e3
Show file tree
Hide file tree
Showing 19 changed files with 64 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![preview](./resources/preview.png)

> [!NOTE]
> 当前 release 包在 mac 环境下测试通过, window 环境暂未测试
> 当前 release 包在 mac 环境下测试通过, window 仅在虚拟机测试通过,如果大家在运行过程中遇到问题,欢迎提 Issue 反馈。
## 💥特性

Expand Down
7 changes: 7 additions & 0 deletions electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ mac:
- arm64
- x64
artifactName: "${productName}_${version}_${arch}.${ext}"
win: {
target: nsis,
requestedExecutionLevel: asInvoker
}
extraResources:
- from: ./extraResources/
to: ./extraResources/
asarUnpack:
- "node_modules/ffmpeg-static/bin/${os}/${arch}/ffmpeg"
- "node_modules/ffmpeg-static/index.js"
Expand Down
Binary file added extraResources/whisper-bin-x64/SDL2.dll
Binary file not shown.
Binary file added extraResources/whisper-bin-x64/bench.exe
Binary file not shown.
Binary file added extraResources/whisper-bin-x64/command.exe
Binary file not shown.
Binary file added extraResources/whisper-bin-x64/lsp.exe
Binary file not shown.
Binary file added extraResources/whisper-bin-x64/main.exe
Binary file not shown.
Binary file added extraResources/whisper-bin-x64/quantize.exe
Binary file not shown.
Binary file added extraResources/whisper-bin-x64/server.exe
Binary file not shown.
Binary file added extraResources/whisper-bin-x64/stream.exe
Binary file not shown.
Binary file added extraResources/whisper-bin-x64/talk-llama.exe
Binary file not shown.
Binary file added extraResources/whisper-bin-x64/talk.exe
Binary file not shown.
Binary file added extraResources/whisper-bin-x64/wchess.exe
Binary file not shown.
Binary file added extraResources/whisper-bin-x64/whisper.dll
Binary file not shown.
20 changes: 12 additions & 8 deletions main/background.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "path";
import { app, dialog, ipcMain, shell } from "electron";
import { exec } from "child_process";
import { exec } from "child_process";
import serve from "electron-serve";
import { createWindow } from "./helpers";
import {
Expand All @@ -12,15 +12,15 @@ import {
} from "./helpers/whisper";
import { extractAudio } from "./helpers/ffmpeg";
import translate from "./helpers/translate";
import { renderTemplate } from "./helpers/utils";
import { getExtraResourcesPath, isWin32, renderTemplate } from "./helpers/utils";
import fs from "fs";

const isProd = process.env.NODE_ENV === "production";

if (isProd) {
serve({ directory: "app" });
} else {
app.setPath("userData", `${app.getPath("userData")} (development)`);
app.setPath("userData", `${app.getPath("userData")}-dev`);
}

(async () => {
Expand Down Expand Up @@ -96,8 +96,12 @@ ipcMain.on("handleTask", async (event, { files, formData }) => {
const whisperModel = model?.toLowerCase();
await extractAudio(filePath, audioFile);
event.sender.send("extractAudio-completed", file);
let mainPath = `${whisperPath}main`;
if(isWin32()){
mainPath = path.join(getExtraResourcesPath(), 'whisper-bin-x64', 'main.exe');
}
exec(
`"${whisperPath}main" -m "${whisperPath}models/ggml-${whisperModel}.bin" -f "${audioFile}" -osrt -of "${srtFile}"`,
`"${mainPath}" -m "${whisperPath}models/ggml-${whisperModel}.bin" -f "${audioFile}" -osrt -of "${srtFile}"`,
async (error, stdout, stderr) => {
if (error) {
event.sender.send("message", error);
Expand All @@ -108,7 +112,7 @@ ipcMain.on("handleTask", async (event, { files, formData }) => {
console.log(err);
}
});
if (translateProvider !== '-1') {
if (translateProvider !== "-1") {
await translate(
event,
directory,
Expand Down Expand Up @@ -147,10 +151,10 @@ ipcMain.on("makeWhisper", (event) => {
makeWhisper(event);
});

ipcMain.on("downModel", (event, {model, source}) => {
ipcMain.on("downModel", (event, { model, source }) => {
downModel(event, model, source);
});

ipcMain.on('openUrl', (event, url) => {
shell.openExternal(url);
ipcMain.on("openUrl", (event, url) => {
shell.openExternal(url);
});
1 change: 0 additions & 1 deletion main/helpers/model-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default async function replaceModelSource(filePath, target) {
const data = await fs.readFile(filePath, 'utf8');
let result = target === 'hf-mirror.com' ? data.replace(/huggingface\.co/g, 'hf-mirror.com') : data.replace(/hf-mirror\.com/g, 'hf-mirror.com');
// 写入文件
console.log(result, 'result');
await fs.writeFile(filePath, result, 'utf8');
}

13 changes: 13 additions & 0 deletions main/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import path from "path";
import { app } from "electron";
import os from "os";

// 将字符串转成模板字符串
export const renderTemplate = (template, data) => {
const names = Object.keys(data);
Expand All @@ -6,3 +10,12 @@ export const renderTemplate = (template, data) => {
};


export const isDarwin = () => os.platform() === 'darwin';

export const isWin32 = () => os.platform() === 'win32';


export const getExtraResourcesPath = () => {
const isProd = process.env.NODE_ENV === "production";
return isProd ? path.join(process.resourcesPath, 'extraResources') : path.join(app.getAppPath(), 'extraResources');
}
47 changes: 30 additions & 17 deletions main/helpers/whisper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs from "fs";
import git from "isomorphic-git";
import http from "isomorphic-git/http/node";
import replaceModelSource from "./model-source";
import { isDarwin, isWin32 } from "./utils";

export const getPath = (key?: string) => {
const userDataPath = app.getPath("userData");
Expand Down Expand Up @@ -81,31 +82,43 @@ export const downModel = async (
event.sender.send("message", "whisper.cpp 未下载,请先下载 whisper.cpp");
}
try {
await replaceModelSource(`${modelsPath}/download-ggml-model.sh`, source);
let downShellPath;
let shell: string;
if(isDarwin()){
downShellPath = path.join(modelsPath, 'download-ggml-model.sh');
shell = 'bash';
} else if(isWin32()){
downShellPath = path.join(modelsPath, 'download-ggml-model.cmd');
shell = 'cmd.exe /c'
} else {
throw Error("platform does not support! ");
}
await replaceModelSource(`${downShellPath}`, source);
console.log("完成模型下载地址替换", modelName);
exec(
`bash "${modelsPath}/download-ggml-model.sh" ${modelName}`,
(err, stdout) => {
if (err) {
event.sender.send("message", err);
} else {
event.sender.send("message", `模型 ${modelName} 下载完成`);
}
event.sender.send("downModelComplete", !err);
event.sender.send("getSystemInfoComplete", {
whisperInstalled: checkWhisperInstalled(),
modelsInstalled: getModelsInstalled(),
});
},
);
console.log("正在安装 whisper.cpp 模型");
exec(`${shell} "${downShellPath}" ${modelName}`, (err, stdout) => {
if (err) {
event.sender.send("message", err);
} else {
event.sender.send("message", `模型 ${modelName} 下载完成`);
}
event.sender.send("downModelComplete", !err);
event.sender.send("getSystemInfoComplete", {
whisperInstalled: checkWhisperInstalled(),
modelsInstalled: getModelsInstalled(),
});
});
} catch (error) {
event.sender.send("message", error);
}
};

export const makeWhisper = (event) => {
const { whisperPath, mainPath } = getPath();
if (fs.existsSync(mainPath)) return;
if (fs.existsSync(mainPath) || isWin32()) {
event.sender.send("makeWhisperComplete", true);
return;
};
if (!checkWhisperInstalled()) {
event.sender.send("message", "whisper.cpp 未下载,请先下载 whisper.cpp");
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"name": "video-subtitle-master",
"description": "视频转字幕,字幕翻译软件",
"version": "1.0.2",
"version": "1.0.3",
"author": "buxuku <[email protected]>",
"main": "app/background.js",
"scripts": {
Expand Down

0 comments on commit 60442e3

Please sign in to comment.