OpenAI Whisper在Windows 10环境下运行脚本提示找不到文件的问题求助
OpenAI Whisper在Windows 10环境下运行脚本提示找不到文件的问题求助
我最近把一个用来生成SRT字幕文件的脚本从旧笔记本迁移到了新笔记本上,两边安装的依赖包版本完全一致,但在新笔记本的Anaconda独立环境里运行脚本时,却一直报找不到文件的错误,旧笔记本上运行完全正常,实在搞不懂哪里出问题了,希望大家能帮我看看。
错误回溯信息
Traceback (most recent call last): File "D:\Moives\OP\test.py", line 56, in <module> transcribe_audio(output_srt_path, file_name) File "D:\Moives\OP\test.py", line 11, in transcribe_audio transcribe_result = model.transcribe(audio=f"{file_name}", language="de") File "C:\Users\admin\anaconda3\lib\site-packages\whisper\transcribe.py", line 122, in transcribe mel = log_mel_spectrogram(audio, model.dims.n_mels, padding=N_SAMPLES) File "C:\Users\admin\anaconda3\lib\site-packages\whisper\audio.py", line 140, in log_mel_spectrogram audio = load_audio(audio) File "C:\Users\admin\anaconda3\lib\site-packages\whisper\audio.py", line 58, in load_audio out = run(cmd, capture_output=True, check=True).stdout File "C:\Users\admin\anaconda3\lib\subprocess.py", line 505, in run with Popen(*popenargs, **kwargs) as process: File "C:\Users\admin\anaconda3\lib\subprocess.py", line 951, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\admin\anaconda3\lib\subprocess.py", line 1420, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified
我的环境配置
- 操作系统:Windows 10
- Python版本:
3.9.7 - openai-whisper版本:
20231117 - argostranslate版本:
1.9.6
我是在Anaconda创建的独立虚拟环境里运行的脚本。
已尝试的解决方法
- 给音频文件指定了完整路径
- 将文件名改成了无特殊字符的简单字符串
- 尝试升级了openai-whisper到更新的版本
但这些方法都没解决问题,旧笔记本上同样的脚本却能正常运行。
我的脚本代码
from datetime import timedelta import os import whisper import argostranslate.package import argostranslate.translate def transcribe_audio(output_srt_path, file_name): model = whisper.load_model("small") # Change this to your desired model transcribe_result = model.transcribe(audio=f"{file_name}", language="de") segments = transcribe_result['segments'] germans = [segment['text'][1:] if segment['text'][0] == ' ' else segment['text'] for segment in segments ] translations = [argostranslate.translate.translate(sentence, "de", "en") for sentence in germans] with open(output_srt_path, 'w', encoding='utf-8') as srt_file: for idx, segment in enumerate(segments): try: start_time = str(timedelta(seconds=int(segment['start']))) + ',000' end_time = str(timedelta(seconds=int(segment['end']))) + ',000' text = segment['text'] segment_id = segment['id'] + 1 srt_segment = f"{segment_id}\n{start_time} --> {end_time}\n{text[1:] if text[0] == ' ' else text}\n" srt_file.write(srt_segment) text = ''.join(['-' for _ in range(min(80, len(segment['text']) + 10))]) srt_segment = f"{text}\n" srt_file.write(srt_segment) english_translation = translations[segment['id']] srt_segment = f"{english_translation}\n\n" srt_file.write(srt_segment) except Exception as e: print(e) return output_srt_path translated = set() for file_name in os.listdir('./'): if file_name.endswith(".srt"): translated.add(f"{file_name[:-4]}") for file_name in os.listdir('./'): if not file_name.endswith(".mp4"): continue if file_name in translated: continue # Example usage: Convert "audio.mp3" to an SRT file # don't forget to replace \n with a real new line output_srt_path = f"{file_name}.srt" transcribe_audio(output_srt_path, file_name) print(f"Transcription saved to {output_srt_path}")
备注:内容来源于stack exchange,提问作者laith drebaty




