Open EloiStree opened 4 months ago
import moviepy.editor as mp
import speech_recognition as sr
def convert_video_to_srt(video_path, output_path):
# Load video file
video = mp.VideoFileClip(video_path)
# Extract audio from the video
audio = video.audio
# Write the audio to a temporary WAV file
audio_temp_path = "temp.wav"
audio.write_audiofile(audio_temp_path)
# Recognize speech from the audio
recognizer = sr.Recognizer()
with sr.AudioFile(audio_temp_path) as source:
audio_data = recognizer.record(source)
recognized_text = recognizer.recognize_google(audio_data)
# Generate SRT format from recognized text
srt_content = "1\n"
sentences = recognized_text.split('.')
for i, sentence in enumerate(sentences, start=1):
srt_content += f"{i}\n"
srt_content += f"{sentence.strip()}\n"
srt_content += "\n"
# Write SRT content to output file
with open(output_path, 'w') as f:
f.write(srt_content)
# Clean up temporary audio file
if os.path.exists(audio_temp_path):
os.remove(audio_temp_path)
if __name__ == "__main__":
input_video_path = "input_video.mp4"
output_srt_path = "output_subtitle.srt"
convert_video_to_srt(input_video_path, output_srt_path)
SubRip https://www.videohelp.com/software/Subrip