tavern-talk/tavern_talk/playback.py

70 lines
1.7 KiB
Python

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)