Spaces:
Running
Running
File size: 1,870 Bytes
1f235c3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | # ToDo 将 whisper.cpp 移动到此
import os
import subprocess
import sys
from dataclasses import dataclass
from pathlib import Path
from videotrans.configure.config import tr, settings, logger
from videotrans.configure.excepts import SpeechToTextError
from videotrans.recognition._base import BaseRecogn
from videotrans.util import tools
@dataclass
class CPPRecogn(BaseRecogn):
def _exec(self):
cpp_path = settings.get('Whisper_cpp', 'whisper-cli')
cmd = [
cpp_path,
"-f",
self.audio_file,
"-osrt",
"-np"
]
cmd += ["-l", self.detect_language.split('-')[0]]
prompt = settings.get(f'initial_prompt_{self.detect_language}')
if prompt:
cmd += ['--prompt', prompt]
cpp_folder = Path(cpp_path).parent.resolve().as_posix()
if not Path(f'{cpp_folder}/models/{self.model_name}').is_file():
raise RuntimeError(tr('The model does not exist. Please download the model to the {} directory first.',
f'{cpp_folder}/models'))
txt_file = cpp_folder + '/pyvideotrans.txt'
if Path(txt_file).exists():
cmd.extend(Path(txt_file).read_text(encoding='utf-8').strip().split(' '))
_sub=f'{self.cache_folder}/cpp_srt'
cmd.extend(['-m', f'models/{self.model_name}', '-of', _sub])
logger.debug(f'Whisper.cpp: {cmd=}')
try:
subprocess.run(cmd, capture_output=True, text=True, check=True, encoding='utf-8', errors='replace', creationflags=subprocess.CREATE_NO_WINDOW if sys.platform == 'win32' else 0, cwd=os.path.dirname(cpp_path))
return tools.get_subtitle_from_srt(_sub+".srt", is_file=True)
except subprocess.CalledProcessError as e:
raise SpeechToTextError(e.stderr+f"\n{e.stdout}")
|