Spaces:
Sleeping
Sleeping
make loom
Browse files- audio_extractor.py +9 -46
audio_extractor.py
CHANGED
|
@@ -179,32 +179,12 @@ class RobustAudioExtractor:
|
|
| 179 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 180 |
raise Exception(f"Failed to download direct media: {str(e)}")
|
| 181 |
|
| 182 |
-
def _extract_from_loom(self, url, start_time):
|
| 183 |
-
"""Extract audio from Loom with multiple strategies"""
|
| 184 |
-
strategies = [
|
| 185 |
-
self._loom_strategy_basic,
|
| 186 |
-
self._loom_strategy_embed,
|
| 187 |
-
self._loom_strategy_api,
|
| 188 |
-
]
|
| 189 |
-
|
| 190 |
-
for i, strategy in enumerate(strategies):
|
| 191 |
-
try:
|
| 192 |
-
print(f"Trying Loom strategy {i+1}...")
|
| 193 |
-
result = strategy(url, start_time)
|
| 194 |
-
if result:
|
| 195 |
-
return result
|
| 196 |
-
time.sleep(1) # Brief delay between strategies
|
| 197 |
-
except Exception as e:
|
| 198 |
-
print(f"Loom strategy {i+1} failed: {str(e)}")
|
| 199 |
-
continue
|
| 200 |
-
|
| 201 |
-
raise Exception("Failed to extract audio from Loom URL with all strategies")
|
| 202 |
|
| 203 |
-
def
|
| 204 |
-
"""
|
| 205 |
temp_dir = tempfile.mkdtemp()
|
| 206 |
ydl_opts = {
|
| 207 |
-
'format': 'bestaudio
|
| 208 |
'postprocessors': [{
|
| 209 |
'key': 'FFmpegExtractAudio',
|
| 210 |
'preferredcodec': 'wav',
|
|
@@ -214,33 +194,16 @@ class RobustAudioExtractor:
|
|
| 214 |
'quiet': True,
|
| 215 |
'no_warnings': True,
|
| 216 |
'noplaylist': True,
|
| 217 |
-
'http_headers': {
|
| 218 |
-
'User-Agent': random.choice(self.user_agents)
|
| 219 |
-
}
|
| 220 |
}
|
| 221 |
|
| 222 |
-
with
|
| 223 |
-
|
| 224 |
-
ydl.download([url])
|
| 225 |
|
| 226 |
-
|
|
|
|
|
|
|
| 227 |
|
| 228 |
-
|
| 229 |
-
"""Try Loom embed URL format"""
|
| 230 |
-
# Extract video ID from Loom URL
|
| 231 |
-
import re
|
| 232 |
-
loom_id_match = re.search(r'loom\.com/share/([a-zA-Z0-9]+)', url)
|
| 233 |
-
if loom_id_match:
|
| 234 |
-
video_id = loom_id_match.group(1)
|
| 235 |
-
embed_url = f"https://www.loom.com/embed/{video_id}"
|
| 236 |
-
return self._loom_strategy_basic(embed_url, start_time)
|
| 237 |
-
return None
|
| 238 |
-
|
| 239 |
-
def _loom_strategy_api(self, url, start_time):
|
| 240 |
-
"""Try to get direct video URL from Loom"""
|
| 241 |
-
# This is a placeholder for a more sophisticated approach
|
| 242 |
-
# You might need to inspect Loom's network requests to find direct video URLs
|
| 243 |
-
return None
|
| 244 |
|
| 245 |
def _extract_with_ytdlp_robust(self, url, start_time):
|
| 246 |
"""Robust yt-dlp extraction with multiple strategies"""
|
|
|
|
| 179 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 180 |
raise Exception(f"Failed to download direct media: {str(e)}")
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
+
def extract_audio_from_loom(url):
|
| 184 |
+
"""Simple Loom audio extractor using yt-dlp"""
|
| 185 |
temp_dir = tempfile.mkdtemp()
|
| 186 |
ydl_opts = {
|
| 187 |
+
'format': 'bestaudio/best',
|
| 188 |
'postprocessors': [{
|
| 189 |
'key': 'FFmpegExtractAudio',
|
| 190 |
'preferredcodec': 'wav',
|
|
|
|
| 194 |
'quiet': True,
|
| 195 |
'no_warnings': True,
|
| 196 |
'noplaylist': True,
|
|
|
|
|
|
|
|
|
|
| 197 |
}
|
| 198 |
|
| 199 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 200 |
+
ydl.download([url])
|
|
|
|
| 201 |
|
| 202 |
+
for f in os.listdir(temp_dir):
|
| 203 |
+
if f.endswith('.wav'):
|
| 204 |
+
return os.path.join(temp_dir, f)
|
| 205 |
|
| 206 |
+
raise Exception("Audio file not found in output.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
def _extract_with_ytdlp_robust(self, url, start_time):
|
| 209 |
"""Robust yt-dlp extraction with multiple strategies"""
|