|
import streamlit as st |
|
import moviepy.editor as mp |
|
import speech_recognition as sr |
|
from pydub import AudioSegment |
|
import tempfile |
|
import os |
|
|
|
|
|
def video_to_audio(video_file): |
|
|
|
video = mp.VideoFileClip(video_file) |
|
|
|
|
|
audio = video.audio |
|
temp_audio_path = tempfile.mktemp(suffix=".mp3") |
|
|
|
|
|
audio.write_audiofile(temp_audio_path) |
|
return temp_audio_path |
|
|
|
|
|
def transcribe_audio(audio_file): |
|
|
|
recognizer = sr.Recognizer() |
|
|
|
|
|
audio = sr.AudioFile(audio_file) |
|
|
|
with audio as source: |
|
audio_data = recognizer.record(source) |
|
|
|
try: |
|
|
|
text = recognizer.recognize_google(audio_data) |
|
return text |
|
except sr.UnknownValueError: |
|
return "Audio could not be understood." |
|
except sr.RequestError: |
|
return "Could not request results from Google Speech Recognition service." |
|
|
|
|
|
st.title("Video to Audio to Text Transcription") |
|
st.write("Upload a video file, and it will be converted to audio and transcribed into text.") |
|
|
|
|
|
uploaded_video = st.file_uploader("Upload Video", type=["mp4", "mov", "avi"]) |
|
|
|
if uploaded_video is not None: |
|
|
|
with tempfile.NamedTemporaryFile(delete=False) as tmp_video: |
|
tmp_video.write(uploaded_video.read()) |
|
tmp_video_path = tmp_video.name |
|
|
|
|
|
st.write("Converting video to audio...") |
|
audio_file = video_to_audio(tmp_video_path) |
|
|
|
|
|
st.audio(audio_file, format='audio/mp3') |
|
|
|
|
|
st.write("Transcribing audio to text...") |
|
transcription = transcribe_audio(audio_file) |
|
|
|
|
|
st.text_area("Transcription", transcription, height=300) |
|
|
|
|
|
os.remove(tmp_video_path) |
|
os.remove(audio_file) |
|
|