-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp4totext.py
37 lines (31 loc) · 1.19 KB
/
mp4totext.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from moviepy.editor import VideoFileClip
import speech_recognition as sr
from pydub import AudioSegment
def mp4_to_text(mp4_file):
# Step 1: Convert MP4 to Audio
video = VideoFileClip(mp4_file)
audio_path = "audio.wav"
video.audio.write_audiofile(audio_path, codec="pcm_s16le")
# Step 2: Transcribe Audio to Text
recognizer = sr.Recognizer()
audio = AudioSegment.from_wav(audio_path)
# Split audio for better accuracy if needed
audio_chunks = audio[::60000] # 60 seconds each chunk
full_text = ""
for i, chunk in enumerate(audio_chunks):
chunk.export("chunk.wav", format="wav")
with sr.AudioFile("chunk.wav") as source:
audio_data = recognizer.record(source)
try:
# Recognize speech using Google Web Speech API (free, but limited usage)
text = recognizer.recognize_google(audio_data)
full_text += text + " "
except sr.UnknownValueError:
print("Audio not clear")
except sr.RequestError:
print("API unavailable")
return full_text
# Usage example
mp4_file = "Bro.Amnual01.mp4"
text = mp4_to_text(mp4_file)
print(text)