1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] WhisperDesktop can only handle a single file at a time, so I tried to make a script...

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024 às 10:52.

  1. Stack

    Stack Membro Participativo

    WhisperDesktop is a OpenAI's Whisper models for speech recognition tasks.

    WhisperDeskTop.exe on Windows10 GPU used

    WhisperDeskTop.exe .mkv to .srt, but only one at a time

    So, if I want to convert all videos, I could only keep clicking. Then, I tried to write a script.

    In Power Shell, Python 3.8 installed, create a Python Virtual Environment by python -m venv thename('thename' is just a name of the environment).

    Then, activate the environment,used .\Scripts\Activate.ps1. As the picture,

    I also install NVIDIA CUDA, CUDNN, ffmpeg, pytorch, and download OpenAI model such as large, medium to my notebook.

    All of them have been checked.

    What's the problem?

    import os
    import subprocess
    import time
    from tqdm import tqdm
    from concurrent.futures import ThreadPoolExecutor

    video_directory = ""

    ffmpeg_command = 'ffmpeg -i "{}" -f wav -vn "{}"'

    whisper_command = 'whispercli -gpu -nt -m "D:\\Whisper\\model\\ggml-large-v3-turbo.bin" -l en -nt -osrt -f "{}"'

    def convert_video_to_audio(video_path, audio_path, video_name):
    ffmpeg_output = subprocess.check_output(
    ffmpeg_command.format(video_path, audio_path),
    shell=True,
    stderr=subprocess.DEVNULL,
    )


    def gen_audio_srt(audio_path, video_name):
    whisper_output = subprocess.check_output(
    whisper_command.format(audio_path), shell=True, encoding="utf-8"
    )


    def process_video(video_file):
    video_path = os.path.join(video_directory, video_file)
    video_name = os.path.splitext(video_file)[0]
    audio_path = os.path.join(video_directory, video_name + ".wav")
    srt_path = os.path.join(video_directory, video_name + ".srt")

    if os.path.exists(srt_path):
    print(f"srt is exisits.")
    return 0

    convert_video_to_audio(video_path, audio_path, video_name)
    gen_audio_srt(audio_path, video_name)
    os.remove(audio_path)
    return 1

    def process_videos():
    start_time = time.time()
    n = 0
    video_files = [
    f
    for f in os.listdir(video_directory)
    if f.endswith((".mp4", ".avi", ".mkv", ".flv", ".mov"))
    ]

    with ThreadPoolExecutor() as executor:
    results = list(tqdm(executor.map(process_video, video_files), total=len(video_files), desc="processing... "))

    n = sum(results)
    end_time = time.time()
    print("{:d}videos,token times: {:.2f}s".format(n, end_time - start_time))

    if __name__ == "__main__":
    path = ""
    while True:
    path = input("dir: ")
    if os.path.exists(path):
    break
    else:
    print(f"{path}path maybe wrong...")
    video_directory = path
    process_videos()


    Please help me finish the Python script.

    Continue reading...

Compartilhe esta Página