mirror of
https://github.com/HiMeditator/auto-caption.git
synced 2026-02-11 10:04:47 +08:00
feat(main): 修改 python 子进程与主程序的通信方式,由 WebSocket 改为 IPC 通信
This commit is contained in:
@@ -5,6 +5,8 @@ from dashscope.audio.asr import (
|
||||
TranslationRecognizerRealtime
|
||||
)
|
||||
from datetime import datetime
|
||||
import json
|
||||
import sys
|
||||
|
||||
class Callback(TranslationRecognizerCallback):
|
||||
"""
|
||||
@@ -17,11 +19,10 @@ class Callback(TranslationRecognizerCallback):
|
||||
self.time_str = ''
|
||||
|
||||
def on_open(self) -> None:
|
||||
print("INFO gummy translation start...")
|
||||
pass
|
||||
|
||||
def on_close(self) -> None:
|
||||
print(f"INFO tokens useage: {self.usage}")
|
||||
print(f"INFO translation end...")
|
||||
pass
|
||||
|
||||
def on_event(
|
||||
self,
|
||||
@@ -32,10 +33,10 @@ class Callback(TranslationRecognizerCallback):
|
||||
) -> None:
|
||||
caption = {}
|
||||
if transcription_result is not None:
|
||||
caption['id'] = transcription_result.sentence_id
|
||||
caption['index'] = transcription_result.sentence_id
|
||||
caption['text'] = transcription_result.text
|
||||
if caption['id'] != self.cur_id:
|
||||
self.cur_id = caption['id']
|
||||
if caption['index'] != self.cur_id:
|
||||
self.cur_id = caption['index']
|
||||
cur_time = datetime.now().strftime('%H:%M:%S')
|
||||
caption['time_s'] = cur_time
|
||||
self.time_str = cur_time
|
||||
@@ -50,7 +51,20 @@ class Callback(TranslationRecognizerCallback):
|
||||
|
||||
if usage:
|
||||
self.usage += usage['duration']
|
||||
print(caption)
|
||||
|
||||
# print(caption)
|
||||
self.send_to_node(caption)
|
||||
|
||||
def send_to_node(self, data):
|
||||
"""
|
||||
将数据发送到 Node.js 进程
|
||||
"""
|
||||
try:
|
||||
json_data = json.dumps(data) + '\n'
|
||||
sys.stdout.write(json_data)
|
||||
sys.stdout.flush()
|
||||
except Exception as e:
|
||||
print(f"Error sending data to Node.js: {e}", file=sys.stderr)
|
||||
|
||||
class GummyTranslator:
|
||||
def __init__(self, rate, source, target):
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
import asyncio
|
||||
from sysaudio.win import LoopbackStream, mergeStreamChannels
|
||||
from audio2text.gummy import GummyTranslator
|
||||
import sys
|
||||
|
||||
def main():
|
||||
sys.stdout.reconfigure(line_buffering=True)
|
||||
loopback = LoopbackStream()
|
||||
loopback.openStream()
|
||||
|
||||
gummy = GummyTranslator(loopback.RATE, "zh", "en")
|
||||
gummy.translator.start()
|
||||
|
||||
try:
|
||||
for _ in range(0, 400):
|
||||
if not loopback.stream: continue
|
||||
data = loopback.stream.read(loopback.CHUNK)
|
||||
data = mergeStreamChannels(data, loopback.CHANNELS)
|
||||
gummy.translator.send_audio_frame(data)
|
||||
finally:
|
||||
gummy.translator.stop()
|
||||
loopback.closeStream()
|
||||
for _ in range(0, 400):
|
||||
if not loopback.stream: continue
|
||||
data = loopback.stream.read(loopback.CHUNK)
|
||||
data = mergeStreamChannels(data, loopback.CHANNELS)
|
||||
gummy.translator.send_audio_frame(data)
|
||||
|
||||
gummy.translator.stop()
|
||||
loopback.closeStream()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
BIN
python-subprocess/requirements.txt
Normal file
BIN
python-subprocess/requirements.txt
Normal file
Binary file not shown.
Reference in New Issue
Block a user