From ac58b53ebaca31bacac24101a8903ba8e8aa5b41 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Sun, 10 Nov 2024 08:15:40 +0100 Subject: [PATCH] Simple test script for whisper transcript --- tavern_talk/playback.py | 70 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tavern_talk/playback.py diff --git a/tavern_talk/playback.py b/tavern_talk/playback.py new file mode 100644 index 0000000..b1f547b --- /dev/null +++ b/tavern_talk/playback.py @@ -0,0 +1,70 @@ +import json + +from pydub import AudioSegment +from pydub.playback import play + +PATH_TEXT = "./2024-07-29_audio.json" +PATH_AUDIO = "./2024-07-29_audio.wav" + + +def play_audio_segment(file_path, start_time, duration): + """ + Plays a segment of an audio file starting at `start_time` for `duration` seconds. + + Parameters: + - file_path (str): Path to the audio file. + - start_time (float): The starting point in seconds. + - duration (float): The duration of playback in seconds. + """ + # Load only the necessary segment of the audio file + audio_segment = AudioSegment.from_file(file_path) + start_ms = start_time * 1000 # Convert seconds to milliseconds + end_ms = start_ms + (duration * 1000) + + # Extract the desired portion of the audio + segment = audio_segment[start_ms:end_ms] + + # Play the extracted audio segment + play(segment) + + +segments = [] +with open(PATH_TEXT, "r") as fp: + data = json.load(fp) + segments = data["segments"] + +print(f"Transcript {PATH_TEXT} loaded.") + +while True: + snippet = input("What are you looking for: ") + + if snippet == "": + exit(0) + + print(f"Looking for '{snippet}' ...") + targets = [] + for seg in segments: + if snippet.lower() in seg["text"].lower(): + targets.append(seg) + + # if len(targets) >= 5: + # break + + print(f"Found {len(targets)} targets.") + if len(targets) == 0: + continue + + for i, seg in enumerate(targets): + print(f"{i} | {seg['text']}") + + while True: + index = input("Index to play: ") + if index == "": + break + + try: + index = int(index) + except ValueError: + continue + + play_audio_segment(PATH_AUDIO, targets[index]["start"] - 0.5, 5)