Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -55,16 +55,22 @@ def get_video_duration(video_id, api_key):
|
|
55 |
|
56 |
def download_and_transcribe_with_whisper(youtube_url):
|
57 |
try:
|
58 |
-
# Temporary directory for storing the downloaded audio
|
59 |
-
with tempfile.TemporaryDirectory() as temp_dir:
|
60 |
-
temp_audio_file = os.path.join(temp_dir, "audio.mp4") # Pytube downloads in mp4 format
|
61 |
-
|
62 |
-
# Download audio using pytube
|
63 |
-
yt = YouTube(youtube_url)
|
64 |
-
audio_stream = yt.streams.filter(only_audio=True).first() # Get the first available audio stream
|
65 |
-
audio_stream.download(output_path=temp_dir, filename="audio.mp4") # Download audio to temp dir
|
66 |
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
audio = AudioSegment.from_file(temp_audio_file)
|
69 |
wav_file = os.path.join(temp_dir, "audio.wav")
|
70 |
audio.export(wav_file, format="wav")
|
|
|
55 |
|
56 |
def download_and_transcribe_with_whisper(youtube_url):
|
57 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
60 |
+
temp_audio_file = os.path.join(temp_dir, "audio.mp3")
|
61 |
+
|
62 |
+
ydl_opts = {
|
63 |
+
'format': 'bestaudio/best',
|
64 |
+
'outtmpl': temp_audio_file,
|
65 |
+
'extractaudio': True,
|
66 |
+
'audioquality': 1,
|
67 |
+
}
|
68 |
+
|
69 |
+
# Download audio using yt-dlp
|
70 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
71 |
+
ydl.download([youtube_url])
|
72 |
+
|
73 |
+
# Convert to wav for Whisper
|
74 |
audio = AudioSegment.from_file(temp_audio_file)
|
75 |
wav_file = os.path.join(temp_dir, "audio.wav")
|
76 |
audio.export(wav_file, format="wav")
|