Spaces:
Running
Running
Editing code for youtube error
Browse files- summarizer.py +48 -0
summarizer.py
CHANGED
@@ -1,8 +1,42 @@
|
|
1 |
import yt_dlp
|
2 |
from faster_whisper import WhisperModel
|
3 |
from ollama import Client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def download_audio(url):
|
|
|
|
|
6 |
ydl_opts = {
|
7 |
'format': 'bestaudio/best',
|
8 |
'postprocessors': [{
|
@@ -11,12 +45,26 @@ def download_audio(url):
|
|
11 |
'preferredquality': '192',
|
12 |
}],
|
13 |
'outtmpl': 'audio.%(ext)s',
|
|
|
|
|
14 |
}
|
|
|
|
|
|
|
|
|
|
|
15 |
try:
|
16 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
17 |
ydl.download([url])
|
|
|
|
|
|
|
|
|
|
|
18 |
return 'audio.wav'
|
19 |
except Exception as e:
|
|
|
|
|
20 |
raise Exception(f"Error downloading audio: {str(e)}")
|
21 |
|
22 |
def transcribe_audio(audio_file):
|
|
|
1 |
import yt_dlp
|
2 |
from faster_whisper import WhisperModel
|
3 |
from ollama import Client
|
4 |
+
import os
|
5 |
+
import browser_cookie3
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
def get_youtube_cookies():
|
9 |
+
"""
|
10 |
+
Attempt to get YouTube cookies from multiple browsers.
|
11 |
+
Returns a cookie file path or None if no cookies found.
|
12 |
+
"""
|
13 |
+
browsers = [
|
14 |
+
(browser_cookie3.chrome, 'chrome'),
|
15 |
+
(browser_cookie3.firefox, 'firefox'),
|
16 |
+
(browser_cookie3.edge, 'edge'),
|
17 |
+
(browser_cookie3.safari, 'safari')
|
18 |
+
]
|
19 |
+
|
20 |
+
# Create a temporary file for cookies
|
21 |
+
cookie_file = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
|
22 |
+
|
23 |
+
for browser_func, browser_name in browsers:
|
24 |
+
try:
|
25 |
+
cookies = browser_func(domain_name='.youtube.com')
|
26 |
+
with open(cookie_file.name, 'w') as f:
|
27 |
+
for cookie in cookies:
|
28 |
+
f.write(f'{cookie.domain}\tTRUE\t{cookie.path}\t'
|
29 |
+
f'{"TRUE" if cookie.secure else "FALSE"}\t{cookie.expires}\t'
|
30 |
+
f'{cookie.name}\t{cookie.value}\n')
|
31 |
+
return cookie_file.name
|
32 |
+
except:
|
33 |
+
continue
|
34 |
+
|
35 |
+
return None
|
36 |
|
37 |
def download_audio(url):
|
38 |
+
cookie_file = get_youtube_cookies()
|
39 |
+
|
40 |
ydl_opts = {
|
41 |
'format': 'bestaudio/best',
|
42 |
'postprocessors': [{
|
|
|
45 |
'preferredquality': '192',
|
46 |
}],
|
47 |
'outtmpl': 'audio.%(ext)s',
|
48 |
+
'quiet': True,
|
49 |
+
'no_warnings': True
|
50 |
}
|
51 |
+
|
52 |
+
# Add cookies if available
|
53 |
+
if cookie_file:
|
54 |
+
ydl_opts['cookiefile'] = cookie_file
|
55 |
+
|
56 |
try:
|
57 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
58 |
ydl.download([url])
|
59 |
+
|
60 |
+
# Clean up cookie file
|
61 |
+
if cookie_file and os.path.exists(cookie_file):
|
62 |
+
os.unlink(cookie_file)
|
63 |
+
|
64 |
return 'audio.wav'
|
65 |
except Exception as e:
|
66 |
+
if cookie_file and os.path.exists(cookie_file):
|
67 |
+
os.unlink(cookie_file)
|
68 |
raise Exception(f"Error downloading audio: {str(e)}")
|
69 |
|
70 |
def transcribe_audio(audio_file):
|