Video zu Text – Mit Python und AssemblyAI

Bei AssemblyAI bekommt man eine gute Transkription und es geht auch viel kostenlos.

import assemblyai as aai
import moviepy.editor as mp 
import os
from pathlib import Path
import shutil

aai.settings.api_key = f"foobar"
VIDEO_PATH = "C:/Users/Flo/foobar"

def delete_folder_contents(folder):
    for filename in os.listdir(folder):
        file_path = os.path.join(folder, filename)
        try:
            if os.path.isfile(file_path) or os.path.islink(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path)
        except PermissionError:
            pass

video = mp.VideoFileClip(VIDEO_PATH) 

tempfolder = "C:\\Users\\Flo\\foobar"
filename = Path(VIDEO_PATH).stem
audio_path = tempfolder + "\\" + filename + ".wav"

if not os.path.exists(audio_path):
    Path(audio_path).mkdir(parents=True, exist_ok=True) 
    delete_folder_contents(tempfolder)

    audio_file = video.audio 
    audio_file.write_audiofile(audio_path) 

transcriber = aai.Transcriber()
transcript = transcriber.transcribe(audio_path)

print(transcript.text)