import streamlit as st import moviepy.editor as mp import speech_recognition as sr from pydub import AudioSegment import tempfile import os # Function to convert video to audio def video_to_audio(video_file): # Load the video using moviepy video = mp.VideoFileClip(video_file) # Extract audio audio = video.audio temp_audio_path = tempfile.mktemp(suffix=".mp3") # Write the audio to a file audio.write_audiofile(temp_audio_path) return temp_audio_path # Function to convert MP3 audio to WAV def convert_mp3_to_wav(mp3_file): # Load the MP3 file using pydub audio = AudioSegment.from_mp3(mp3_file) # Create a temporary WAV file temp_wav_path = tempfile.mktemp(suffix=".wav") # Export the audio to the temporary WAV file audio.export(temp_wav_path, format="wav") return temp_wav_path # Function to transcribe audio to text def transcribe_audio(audio_file): # Initialize recognizer recognizer = sr.Recognizer() # Load the audio file using speech_recognition audio = sr.AudioFile(audio_file) with audio as source: audio_data = recognizer.record(source) try: # Transcribe the audio data to text using Google Web Speech API 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." # Streamlit app layout 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.") # File uploader for video uploaded_video = st.file_uploader("Upload Video", type=["mp4", "mov", "avi"]) if uploaded_video is not None: # Save the uploaded video file temporarily with tempfile.NamedTemporaryFile(delete=False) as tmp_video: tmp_video.write(uploaded_video.read()) tmp_video_path = tmp_video.name # Add an "Analyze Video" button if st.button("Analyze Video"): with st.spinner("Processing video... Please wait."): # Convert video to audio audio_file = video_to_audio(tmp_video_path) # Convert the extracted MP3 audio to WAV wav_audio_file = convert_mp3_to_wav(audio_file) # Transcribe audio to text transcription = transcribe_audio(wav_audio_file) # Show the transcription st.text_area("Transcription", transcription, height=300) # Store transcription and audio file in session state st.session_state.transcription = transcription st.session_state.wav_audio_file = wav_audio_file # Cleanup temporary files os.remove(tmp_video_path) os.remove(audio_file) # Check if transcription and audio file are stored in session state if 'transcription' in st.session_state and 'wav_audio_file' in st.session_state: # Provide the audio file to the user for download st.audio(st.session_state.wav_audio_file, format='audio/wav') # Add download buttons for the transcription and audio # Downloadable transcription file st.download_button( label="Download Transcription", data=st.session_state.transcription, file_name="transcription.txt", mime="text/plain" ) # Downloadable audio file with open(st.session_state.wav_audio_file, "rb") as audio_file_data: st.download_button( label="Download Audio", data=audio_file_data, file_name="converted_audio.wav", mime="audio/wav" )