#Making subtitles bot

7 messages · Page 1 of 1 (latest)

neat hinge
#

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")
#

If anyone can tell me why its not working or fix the code that would be amazing

#

pip install SpeechRecognition

#

pip install pysrt

solid hollow
#

Can you explain what’s not working? We can’t help you if you don’t give us any information.

neat hinge
#
  File "c:\Users\Zerifa\Desktop\subtitles\subtitles.py", line 33, in <module>
    generate_subtitles("subtitles.wav", "subtitles.srt")
  File "c:\Users\Zerifa\Desktop\subtitles\subtitles.py", line 18, in generate_subtitles
    for segment in r.recognize_google(source, language="en-US"):
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Zerifa\AppData\Local\Programs\Python\Python311\Lib\site-packages\speech_recognition\__init__.py", line 875, in recognize_google
    assert isinstance(audio_data, AudioData), "``audio_data`` must be audio data"
AssertionError: ``audio_data`` must be audio data```