Hello! I am making a python subtitles code using OpenAI's whisper. Using it, itll be planning every word where it should be on the srt file. Problem is, I am a complete beginner at this and this is a 1 time thing so please go easy. PS. I dont have too much time to learn, I just need this as a tool for some videos.
import speech_recognition as sr
import pysrt
def generate_subtitles(audio_path, output_path):
# Print the audio path to make sure it is correct
print(f"Audio path: {audio_path}")
# Load the audio file
audio = sr.AudioFile(audio_path)
r = sr.Recognizer()
# Create an empty SubRip file
subtitles = pysrt.SubRipFile()
# Iterate through the audio by segments
with audio as source:
source = 0
for segment in r.recognize_google(source, language="en-US"):
# Get the start and end times of the segment
start_time = segment.start_time
end_time = segment.end_time
# Create a new subtitle with the recognized text and the start and end times
subtitle = pysrt.SubRipItem(index=i, start=start_time, end=end_time, text=segment.text)
# Add the subtitle to the SubRip file
subtitles.append(subtitle)
i += 1
# Save the SubRip file to the output path
subtitles.save(output_path, encoding='utf-8')
# Example usage
generate_subtitles("subtitles.wav", "subtitles.srt")