feat(python-subprocess): 尝试字幕显示新解决方案

- 使用 python 子进程解析字幕
- 通过 websocket 通信将字幕传递给软件
This commit is contained in:
himeditator
2025-06-15 12:43:57 +08:00
parent f10530eb67
commit 8858189bf6
18 changed files with 572 additions and 64 deletions

View File

@@ -3,6 +3,16 @@ import path from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import { PythonConnector } from './pyComm';
const pythonConnector = new PythonConnector();
setTimeout(() => {
pythonConnector.send({
command: 'process_data',
payload: { some: 'data' }
});
}, 2000);
let mainWindow: BrowserWindow | undefined
function createMainWindow(): void {
@@ -32,7 +42,7 @@ function createMainWindow(): void {
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
} else {
mainWindow.loadFile(path.join(__dirname, '../renderer/index.html'))
mainWindow.loadFile(path.join(__dirname, '../renderer/main/index.html'))
}
}

52
src/main/pyComm.ts Normal file
View File

@@ -0,0 +1,52 @@
import WebSocket from 'ws';
export class PythonConnector {
ws: WebSocket | null;
constructor() {
this.ws = null;
this.connect();
}
connect() {
this.ws = new WebSocket('ws://localhost:8765');
this.ws.on('open', () => {
console.log('Python server connected');
this.send({ message: 'Electron Initialized' });
});
this.ws.on('message', (data) => {
const message = JSON.parse(data.toString());
console.log('Get message from Python:', message);
// 在这里处理来自 Python 的消息
if (message.notification) {
this.handleNotification(message.notification);
}
});
this.ws.on('close', () => {
console.log('Connection closed. Reconnecting...');
setTimeout(() => this.connect(), 3000);
});
this.ws.on('error', (error) => {
console.error('WebSocket Error:', error);
});
}
send(data) {
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
this.ws.send(JSON.stringify(data));
} else {
console.error('WebSocket not connected');
}
}
handleNotification(notification) {
// 处理 Python 主动推送的通知
console.log('Handel notification:', notification);
// 可以在这里更新 UI 或触发其他操作
}
}

View File

@@ -0,0 +1,17 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Auto Caption Player</title>
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:"
/>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>

View File

@@ -0,0 +1,6 @@
<template>
<h1>Caption</h1>
</template>
<script setup lang="ts">
</script>

View File

@@ -0,0 +1,11 @@
<template>
</template>
<script setup lang="ts">
</script>
<style scoped>
</style>

View File

@@ -0,0 +1,5 @@
body {
margin: 0;
padding: 0;
background-color: black;
}

1
src/renderer/caption/src/env.d.ts vendored Normal file
View File

@@ -0,0 +1 @@
/// <reference types="vite/client" />

View File

@@ -0,0 +1,8 @@
import './assets/styles/reset.css'
import { createPinia } from 'pinia'
import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
app.use(createPinia())
app.mount('#app')

View File

@@ -0,0 +1,9 @@
import { ref } from 'vue'
import { defineStore } from 'pinia'
export const useCaptionStore = defineStore('caption', () => {
const captionFontFamily = ref<string>('sans-serif')
const captionFontSize = ref<number>(24)
const captionFontColor = ref<string>('#ffffff')
return { captionFontFamily, captionFontSize, captionFontColor }
})