mirror of
https://github.com/HiMeditator/auto-caption.git
synced 2026-02-13 19:23:26 +08:00
feat(control): 重构项目,增加字幕引擎配置
This commit is contained in:
72
src/main/utils/config.ts
Normal file
72
src/main/utils/config.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { Styles, CaptionItem, Controls } from '../types'
|
||||
import { BrowserWindow } from 'electron'
|
||||
|
||||
export const styles: Styles = {
|
||||
fontFamily: 'sans-serif',
|
||||
fontSize: 24,
|
||||
fontColor: '#000000',
|
||||
background: '#dbe2ef',
|
||||
opacity: 80,
|
||||
transDisplay: true,
|
||||
transFontFamily: 'sans-serif',
|
||||
transFontSize: 24,
|
||||
transFontColor: '#000000'
|
||||
}
|
||||
|
||||
export const captionLog: CaptionItem[] = []
|
||||
|
||||
export const controls: Controls = {
|
||||
sourceLang: 'en',
|
||||
targetLang: 'zh',
|
||||
engine: 'gummy',
|
||||
translation: true,
|
||||
customized: false,
|
||||
customizedApp: '',
|
||||
customizedCommand: ''
|
||||
}
|
||||
|
||||
export function setStyles(args: any) {
|
||||
styles.fontFamily = args.fontFamily
|
||||
styles.fontSize = args.fontSize
|
||||
styles.fontColor = args.fontColor
|
||||
styles.background = args.background
|
||||
styles.opacity = args.opacity
|
||||
styles.transDisplay = args.transDisplay
|
||||
styles.transFontFamily = args.transFontFamily
|
||||
styles.transFontSize = args.transFontSize
|
||||
styles.transFontColor = args.transFontColor
|
||||
console.log('[INFO] Set Styles:', styles)
|
||||
}
|
||||
|
||||
export function sendStyles(window: BrowserWindow) {
|
||||
window.webContents.send('caption.style.set', styles)
|
||||
console.log('[INFO] Send Styles:', styles)
|
||||
}
|
||||
|
||||
export function sendCaptionLog(window: BrowserWindow) {
|
||||
window.webContents.send('both.log.set', captionLog)
|
||||
}
|
||||
|
||||
export function addCaptionLog(log: CaptionItem) {
|
||||
if(captionLog.length && captionLog[captionLog.length - 1].index === log.index) {
|
||||
captionLog.splice(captionLog.length - 1, 1)
|
||||
captionLog.push(log)
|
||||
}
|
||||
else {
|
||||
captionLog.push(log)
|
||||
}
|
||||
for(const window of BrowserWindow.getAllWindows()){
|
||||
sendCaptionLog(window)
|
||||
}
|
||||
}
|
||||
|
||||
export function setControls(args: any) {
|
||||
controls.sourceLang = args.sourceLang
|
||||
controls.targetLang = args.targetLang
|
||||
controls.engine = args.engine
|
||||
controls.translation = args.translation
|
||||
controls.customized = args.customized
|
||||
controls.customizedApp = args.customizedApp
|
||||
controls.customizedCommand = args.customizedCommand
|
||||
console.log('[INFO] Set Controls:', controls)
|
||||
}
|
||||
42
src/main/utils/pythonProcess.ts
Normal file
42
src/main/utils/pythonProcess.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { spawn } from 'child_process'
|
||||
import { app } from 'electron'
|
||||
import path from 'path'
|
||||
import { addCaptionLog } from './config'
|
||||
|
||||
export class PythonProcess {
|
||||
public start() {
|
||||
const basePath = app.getAppPath()
|
||||
const pythonPath = path.join(
|
||||
basePath,
|
||||
'python-subprocess', 'subenv', 'Scripts', 'python.exe'
|
||||
)
|
||||
const targetPath = path.join(basePath, 'python-subprocess', 'main.py')
|
||||
|
||||
console.log(pythonPath)
|
||||
console.log(targetPath)
|
||||
|
||||
const pythonProcess = spawn(pythonPath, [targetPath])
|
||||
|
||||
pythonProcess.stdout.on('data', (data) => {
|
||||
const lines = data.toString().split('\n');
|
||||
lines.forEach( (line: string) => {
|
||||
if (line.trim()) {
|
||||
try {
|
||||
const caption = JSON.parse(line);
|
||||
addCaptionLog(caption);
|
||||
} catch (e) {
|
||||
console.error('Error parsing JSON:', e);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
pythonProcess.stderr.on('data', (data) => {
|
||||
console.error(`Python Error: ${data}`);
|
||||
});
|
||||
|
||||
pythonProcess.on('close', (code) => {
|
||||
console.log(`Python process exited with code ${code}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user