Create abc.txt
Browse files
abc.txt
ADDED
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import io
|
8 |
+
|
9 |
+
# Function to convert video to audio
|
10 |
+
def video_to_audio(video_file):
|
11 |
+
# Load the video using moviepy
|
12 |
+
video = mp.VideoFileClip(video_file)
|
13 |
+
|
14 |
+
# Extract audio
|
15 |
+
audio = video.audio
|
16 |
+
temp_audio_path = tempfile.mktemp(suffix=".mp3")
|
17 |
+
|
18 |
+
# Write the audio to a file
|
19 |
+
audio.write_audiofile(temp_audio_path)
|
20 |
+
return temp_audio_path
|
21 |
+
|
22 |
+
# Function to convert MP3 audio to WAV
|
23 |
+
def convert_mp3_to_wav(mp3_file):
|
24 |
+
# Load the MP3 file using pydub
|
25 |
+
audio = AudioSegment.from_mp3(mp3_file)
|
26 |
+
|
27 |
+
# Create a temporary WAV file
|
28 |
+
temp_wav_path = tempfile.mktemp(suffix=".wav")
|
29 |
+
|
30 |
+
# Export the audio to the temporary WAV file
|
31 |
+
audio.export(temp_wav_path, format="wav")
|
32 |
+
return temp_wav_path
|
33 |
+
|
34 |
+
# Function to transcribe audio to text
|
35 |
+
def transcribe_audio(audio_file):
|
36 |
+
# Initialize recognizer
|
37 |
+
recognizer = sr.Recognizer()
|
38 |
+
|
39 |
+
# Load the audio file using speech_recognition
|
40 |
+
audio = sr.AudioFile(audio_file)
|
41 |
+
|
42 |
+
with audio as source:
|
43 |
+
audio_data = recognizer.record(source)
|
44 |
+
|
45 |
+
try:
|
46 |
+
# Transcribe the audio data to text using Google Web Speech API
|
47 |
+
text = recognizer.recognize_google(audio_data)
|
48 |
+
return text
|
49 |
+
except sr.UnknownValueError:
|
50 |
+
return "Audio could not be understood."
|
51 |
+
except sr.RequestError:
|
52 |
+
return "Could not request results from Google Speech Recognition service."
|
53 |
+
|
54 |
+
# Streamlit app layout
|
55 |
+
st.title("Video and Audio to Text Transcription")
|
56 |
+
st.write("Upload a video or audio file to convert it to transcription.")
|
57 |
+
|
58 |
+
# Create tabs to separate video and audio uploads
|
59 |
+
tab = st.selectbox("Select the type of file to upload", ["Video", "Audio"])
|
60 |
+
|
61 |
+
if tab == "Video":
|
62 |
+
# File uploader for video
|
63 |
+
uploaded_video = st.file_uploader("Upload Video", type=["mp4", "mov", "avi"])
|
64 |
+
|
65 |
+
if uploaded_video is not None:
|
66 |
+
# Save the uploaded video file temporarily
|
67 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp_video:
|
68 |
+
tmp_video.write(uploaded_video.read())
|
69 |
+
tmp_video_path = tmp_video.name
|
70 |
+
|
71 |
+
# Add an "Analyze Video" button
|
72 |
+
if st.button("Analyze Video"):
|
73 |
+
with st.spinner("Processing video... Please wait."):
|
74 |
+
# Convert video to audio
|
75 |
+
audio_file = video_to_audio(tmp_video_path)
|
76 |
+
|
77 |
+
# Convert the extracted MP3 audio to WAV
|
78 |
+
wav_audio_file = convert_mp3_to_wav(audio_file)
|
79 |
+
|
80 |
+
# Transcribe audio to text
|
81 |
+
transcription = transcribe_audio(wav_audio_file)
|
82 |
+
|
83 |
+
# Show the transcription
|
84 |
+
st.text_area("Transcription", transcription, height=300)
|
85 |
+
|
86 |
+
# Store transcription and audio file in session state
|
87 |
+
st.session_state.transcription = transcription
|
88 |
+
|
89 |
+
# Store the audio file as a BytesIO object in memory
|
90 |
+
with open(wav_audio_file, "rb") as f:
|
91 |
+
audio_data = f.read()
|
92 |
+
st.session_state.wav_audio_file = io.BytesIO(audio_data)
|
93 |
+
|
94 |
+
# Cleanup temporary files
|
95 |
+
os.remove(tmp_video_path)
|
96 |
+
os.remove(audio_file)
|
97 |
+
|
98 |
+
# Check if transcription and audio file are stored in session state
|
99 |
+
if 'transcription' in st.session_state and 'wav_audio_file' in st.session_state:
|
100 |
+
# Provide the audio file to the user for download
|
101 |
+
st.audio(st.session_state.wav_audio_file, format='audio/wav')
|
102 |
+
|
103 |
+
# Add download buttons for the transcription and audio
|
104 |
+
# Downloadable transcription file
|
105 |
+
st.download_button(
|
106 |
+
label="Download Transcription",
|
107 |
+
data=st.session_state.transcription,
|
108 |
+
file_name="transcription.txt",
|
109 |
+
mime="text/plain"
|
110 |
+
)
|
111 |
+
|
112 |
+
# Downloadable audio file
|
113 |
+
st.download_button(
|
114 |
+
label="Download Audio",
|
115 |
+
data=st.session_state.wav_audio_file,
|
116 |
+
file_name="converted_audio.wav",
|
117 |
+
mime="audio/wav"
|
118 |
+
)
|
119 |
+
|
120 |
+
elif tab == "Audio":
|
121 |
+
# File uploader for audio
|
122 |
+
uploaded_audio = st.file_uploader("Upload Audio", type=["wav", "mp3"])
|
123 |
+
|
124 |
+
if uploaded_audio is not None:
|
125 |
+
# Save the uploaded audio file temporarily
|
126 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp_audio:
|
127 |
+
tmp_audio.write(uploaded_audio.read())
|
128 |
+
tmp_audio_path = tmp_audio.name
|
129 |
+
|
130 |
+
# Add an "Analyze Audio" button
|
131 |
+
if st.button("Analyze Audio"):
|
132 |
+
with st.spinner("Processing audio... Please wait."):
|
133 |
+
# Convert audio to WAV if it's in MP3 format
|
134 |
+
if uploaded_audio.type == "audio/mpeg":
|
135 |
+
wav_audio_file = convert_mp3_to_wav(tmp_audio_path)
|
136 |
+
else:
|
137 |
+
wav_audio_file = tmp_audio_path
|
138 |
+
|
139 |
+
# Transcribe audio to text
|
140 |
+
transcription = transcribe_audio(wav_audio_file)
|
141 |
+
|
142 |
+
# Show the transcription
|
143 |
+
st.text_area("Transcription", transcription, height=300)
|
144 |
+
|
145 |
+
# Store transcription in session state
|
146 |
+
st.session_state.transcription_audio = transcription
|
147 |
+
|
148 |
+
# Store the audio file as a BytesIO object in memory
|
149 |
+
with open(wav_audio_file, "rb") as f:
|
150 |
+
audio_data = f.read()
|
151 |
+
st.session_state.wav_audio_file_audio = io.BytesIO(audio_data)
|
152 |
+
|
153 |
+
# Cleanup temporary audio file
|
154 |
+
os.remove(tmp_audio_path)
|
155 |
+
|
156 |
+
# Check if transcription and audio file are stored in session state
|
157 |
+
if 'transcription_audio' in st.session_state and 'wav_audio_file_audio' in st.session_state:
|
158 |
+
# Provide the audio file to the user for download
|
159 |
+
st.audio(st.session_state.wav_audio_file_audio, format='audio/wav')
|
160 |
+
|
161 |
+
# Add download buttons for the transcription and audio
|
162 |
+
# Downloadable transcription file
|
163 |
+
st.download_button(
|
164 |
+
label="Download Transcription",
|
165 |
+
data=st.session_state.transcription_audio,
|
166 |
+
file_name="transcription_audio.txt",
|
167 |
+
mime="text/plain"
|
168 |
+
)
|
169 |
+
|
170 |
+
# Downloadable audio file
|
171 |
+
st.download_button(
|
172 |
+
label="Download Audio",
|
173 |
+
data=st.session_state.wav_audio_file_audio,
|
174 |
+
file_name="converted_audio_audio.wav",
|
175 |
+
mime="audio/wav"
|
176 |
+
)
|
177 |
+
|