admin管理员组

文章数量:1388929

I am trying to run a script which generate srt files. I moved the script from one laptop to another and installed the same packages versions but in my new laptop when I run it, it gives me an error

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

I am trying to run it in anaconda prompt which is a separated environment.

operating system --> windows 10

python --> 3.9.7
openai-whisper --> 20231117
argostranslate --> 1.9.6

I tried to provide full path. or renaming files to simple strings to guarantee that no special characters are in the names of the files. I also tried to use newer versions of openai-whisper.

while the same script works fine in my other laptop.

my script:

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 fet 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}")

本文标签: pythonWhy can39t whisper find filesStack Overflow