mirror of
https://github.com/HiMeditator/auto-caption.git
synced 2026-04-14 09:57:31 +08:00
feat(python-subprocess): 尝试字幕显示新解决方案
- 使用 python 子进程解析字幕 - 通过 websocket 通信将字幕传递给软件
This commit is contained in:
@@ -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
52
src/main/pyComm.ts
Normal 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 或触发其他操作
|
||||
}
|
||||
}
|
||||
17
src/renderer/caption/index.html
Normal file
17
src/renderer/caption/index.html
Normal 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>
|
||||
6
src/renderer/caption/src/App.vue
Normal file
6
src/renderer/caption/src/App.vue
Normal file
@@ -0,0 +1,6 @@
|
||||
<template>
|
||||
<h1>Caption</h1>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
</script>
|
||||
11
src/renderer/caption/src/assets/Template.vue
Normal file
11
src/renderer/caption/src/assets/Template.vue
Normal file
@@ -0,0 +1,11 @@
|
||||
<template>
|
||||
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
||||
5
src/renderer/caption/src/assets/styles/reset.css
Normal file
5
src/renderer/caption/src/assets/styles/reset.css
Normal file
@@ -0,0 +1,5 @@
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background-color: black;
|
||||
}
|
||||
1
src/renderer/caption/src/env.d.ts
vendored
Normal file
1
src/renderer/caption/src/env.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/// <reference types="vite/client" />
|
||||
8
src/renderer/caption/src/main.ts
Normal file
8
src/renderer/caption/src/main.ts
Normal 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')
|
||||
9
src/renderer/caption/src/stores/caption.ts
Normal file
9
src/renderer/caption/src/stores/caption.ts
Normal 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 }
|
||||
})
|
||||
Reference in New Issue
Block a user