Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import yt_dlp
|
2 |
+
import whisper
|
3 |
+
from pydub import AudioSegment
|
4 |
+
import os
|
5 |
+
import time
|
6 |
+
|
7 |
+
# Function to download audio from YouTube video
|
8 |
+
def download_audio_from_youtube(url):
|
9 |
+
ydl_opts = {
|
10 |
+
'format': 'bestaudio/best',
|
11 |
+
'outtmpl': 'video.%(ext)s',
|
12 |
+
'postprocessors': [{
|
13 |
+
'key': 'FFmpegExtractAudio',
|
14 |
+
'preferredcodec': 'mp3',
|
15 |
+
'preferredquality': '192',
|
16 |
+
}],
|
17 |
+
}
|
18 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
19 |
+
ydl.download([url])
|
20 |
+
return 'video.mp3'
|
21 |
+
|
22 |
+
# Function to split audio into chunks
|
23 |
+
def split_audio(file_path, chunk_length_ms=60000):
|
24 |
+
audio = AudioSegment.from_file(file_path)
|
25 |
+
chunks = [audio[i:i + chunk_length_ms] for i in range(0, len(audio), chunk_length_ms)]
|
26 |
+
chunk_files = []
|
27 |
+
for i, chunk in enumerate(chunks):
|
28 |
+
chunk_file = f"chunk_{i}.mp3"
|
29 |
+
chunk.export(chunk_file, format="mp3")
|
30 |
+
chunk_files.append(chunk_file)
|
31 |
+
return chunk_files
|
32 |
+
|
33 |
+
# Function to convert audio to text using Whisper model
|
34 |
+
def transcribe_audio(file_path):
|
35 |
+
model = whisper.load_model("small")
|
36 |
+
|
37 |
+
result = model.transcribe(file_path)
|
38 |
+
return result["text"]
|
39 |
+
|
40 |
+
# Example usage
|
41 |
+
# video_url = "https://youtu.be/x6KuoHihktM?si=VoqHMP8emTnXKJHa"
|
42 |
+
video_url=input()
|
43 |
+
|
44 |
+
# Step 1: Download audio from YouTube
|
45 |
+
print(f"Downloading audio from URL: {video_url}")
|
46 |
+
audio_path = download_audio_from_youtube(video_url)
|
47 |
+
|
48 |
+
# Step 2: Split the audio into chunks
|
49 |
+
print(f"Splitting audio into chunks from path: {audio_path}")
|
50 |
+
chunk_files = split_audio(audio_path)
|
51 |
+
|
52 |
+
# Step 3: Transcribe each chunk and combine results
|
53 |
+
print("Transcribing audio chunks...")
|
54 |
+
transcriptions = []
|
55 |
+
for chunk_file in chunk_files:
|
56 |
+
print(f"Transcribing chunk: {chunk_file}")
|
57 |
+
transcriptions.append(transcribe_audio(chunk_file))
|
58 |
+
os.remove(chunk_file) # Clean up chunk file after transcription
|
59 |
+
time.sleep(1) # Introduce a delay to avoid rate limiting
|
60 |
+
|
61 |
+
transcribed_text = " ".join(transcriptions)
|
62 |
+
print(f"Transcribed text: {transcribed_text}")
|