Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import moviepy.editor as mp
|
| 3 |
+
import speech_recognition as sr
|
| 4 |
+
from pydub import AudioSegment
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Function to convert video to audio
|
| 9 |
+
def video_to_audio(video_file):
|
| 10 |
+
# Load the video using moviepy
|
| 11 |
+
video = mp.VideoFileClip(video_file)
|
| 12 |
+
|
| 13 |
+
# Extract audio
|
| 14 |
+
audio = video.audio
|
| 15 |
+
temp_audio_path = tempfile.mktemp(suffix=".mp3")
|
| 16 |
+
|
| 17 |
+
# Write the audio to a file
|
| 18 |
+
audio.write_audiofile(temp_audio_path)
|
| 19 |
+
return temp_audio_path
|
| 20 |
+
|
| 21 |
+
# Function to transcribe audio to text
|
| 22 |
+
def transcribe_audio(audio_file):
|
| 23 |
+
# Initialize recognizer
|
| 24 |
+
recognizer = sr.Recognizer()
|
| 25 |
+
|
| 26 |
+
# Load the audio file using speech_recognition
|
| 27 |
+
audio = sr.AudioFile(audio_file)
|
| 28 |
+
|
| 29 |
+
with audio as source:
|
| 30 |
+
audio_data = recognizer.record(source)
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# Transcribe the audio data to text using Google Web Speech API
|
| 34 |
+
text = recognizer.recognize_google(audio_data)
|
| 35 |
+
return text
|
| 36 |
+
except sr.UnknownValueError:
|
| 37 |
+
return "Audio could not be understood."
|
| 38 |
+
except sr.RequestError:
|
| 39 |
+
return "Could not request results from Google Speech Recognition service."
|
| 40 |
+
|
| 41 |
+
# Streamlit app layout
|
| 42 |
+
st.title("Video to Audio to Text Transcription")
|
| 43 |
+
st.write("Upload a video file, and it will be converted to audio and transcribed into text.")
|
| 44 |
+
|
| 45 |
+
# File uploader for video
|
| 46 |
+
uploaded_video = st.file_uploader("Upload Video", type=["mp4", "mov", "avi"])
|
| 47 |
+
|
| 48 |
+
if uploaded_video is not None:
|
| 49 |
+
# Save the uploaded video file temporarily
|
| 50 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp_video:
|
| 51 |
+
tmp_video.write(uploaded_video.read())
|
| 52 |
+
tmp_video_path = tmp_video.name
|
| 53 |
+
|
| 54 |
+
# Convert video to audio
|
| 55 |
+
st.write("Converting video to audio...")
|
| 56 |
+
audio_file = video_to_audio(tmp_video_path)
|
| 57 |
+
|
| 58 |
+
# Provide the audio file to the user for download
|
| 59 |
+
st.audio(audio_file, format='audio/mp3')
|
| 60 |
+
|
| 61 |
+
# Transcribe audio to text
|
| 62 |
+
st.write("Transcribing audio to text...")
|
| 63 |
+
transcription = transcribe_audio(audio_file)
|
| 64 |
+
|
| 65 |
+
# Show the transcription
|
| 66 |
+
st.text_area("Transcription", transcription, height=300)
|
| 67 |
+
|
| 68 |
+
# Cleanup temporary files
|
| 69 |
+
os.remove(tmp_video_path)
|
| 70 |
+
os.remove(audio_file)
|