Spaces:
Sleeping
Sleeping
adding app.py
Browse files- .DS_Store +0 -0
- app.py +58 -0
- requirements.txt +17 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from moviepy.editor import VideoFileClip
|
4 |
+
import speech_recognition as sr
|
5 |
+
from pydub import AudioSegment
|
6 |
+
|
7 |
+
def extract_audio_from_video(video_path, audio_path):
|
8 |
+
video = VideoFileClip(video_path)
|
9 |
+
video.audio.write_audiofile(audio_path, codec="pcm_s16le")
|
10 |
+
|
11 |
+
def transcribe_audio(audio_path):
|
12 |
+
recognizer = sr.Recognizer()
|
13 |
+
audio = AudioSegment.from_wav(audio_path)
|
14 |
+
|
15 |
+
chunk_length_ms = 60000 # 60 seconds per chunk
|
16 |
+
chunks = [audio[i:i + chunk_length_ms] for i in range(0, len(audio), chunk_length_ms)]
|
17 |
+
|
18 |
+
full_text = ""
|
19 |
+
for i, chunk in enumerate(chunks):
|
20 |
+
chunk.export(f"chunk_{i}.wav", format="wav")
|
21 |
+
with sr.AudioFile(f"chunk_{i}.wav") as source:
|
22 |
+
audio_data = recognizer.record(source)
|
23 |
+
try:
|
24 |
+
text = recognizer.recognize_google(audio_data)
|
25 |
+
full_text += text + " "
|
26 |
+
except sr.UnknownValueError:
|
27 |
+
full_text += "[Unclear speech] "
|
28 |
+
except sr.RequestError as e:
|
29 |
+
full_text += f"[API error: {e}] "
|
30 |
+
|
31 |
+
for i in range(len(chunks)):
|
32 |
+
os.remove(f"chunk_{i}.wav")
|
33 |
+
|
34 |
+
return full_text
|
35 |
+
|
36 |
+
st.title("Video to Text Transcription")
|
37 |
+
st.write("Upload a video file to transcribe its audio to text.")
|
38 |
+
|
39 |
+
uploaded_file = st.file_uploader("Choose a video file", type=["mp4", "mov", "avi", "mkv"])
|
40 |
+
|
41 |
+
if uploaded_file is not None:
|
42 |
+
with open("temp_video." + uploaded_file.name.split('.')[-1], "wb") as f:
|
43 |
+
f.write(uploaded_file.getbuffer())
|
44 |
+
audio_path = "temp_audio.wav"
|
45 |
+
|
46 |
+
extract_audio_from_video("temp_video." + uploaded_file.name.split('.')[-1], audio_path)
|
47 |
+
st.write("Extracting and transcribing audio...")
|
48 |
+
|
49 |
+
transcribed_text = transcribe_audio(audio_path)
|
50 |
+
st.write("Transcription completed. Here's the text:")
|
51 |
+
st.text_area("Transcribed Text", transcribed_text, height=300)
|
52 |
+
|
53 |
+
with open("transcribed_text.txt", "w", encoding="utf-8") as text_file:
|
54 |
+
text_file.write(transcribed_text)
|
55 |
+
st.download_button("Download Transcribed Text", data=transcribed_text, file_name="transcribed_text.txt")
|
56 |
+
|
57 |
+
os.remove("temp_video." + uploaded_file.name.split('.')[-1])
|
58 |
+
os.remove(audio_path)
|
requirements.txt
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
certifi==2024.12.14
|
2 |
+
charset-normalizer==3.4.1
|
3 |
+
decorator==4.4.2
|
4 |
+
idna==3.10
|
5 |
+
imageio==2.36.1
|
6 |
+
imageio-ffmpeg==0.5.1
|
7 |
+
moviepy==1.0.3
|
8 |
+
numpy==2.2.1
|
9 |
+
pillow==10.4.0
|
10 |
+
proglog==0.1.10
|
11 |
+
pydub==0.25.1
|
12 |
+
python-dotenv==1.0.1
|
13 |
+
requests==2.32.3
|
14 |
+
SpeechRecognition==3.13.0
|
15 |
+
tqdm==4.67.1
|
16 |
+
typing_extensions==4.12.2
|
17 |
+
urllib3==2.3.0
|