Spaces:
Runtime error
Runtime error
nifty-coder commited on
Commit ·
dc73a54
1
Parent(s): a8986ef
test
Browse files- main.py +28 -4
- requirements.txt +1 -0
main.py
CHANGED
|
@@ -10,6 +10,8 @@ import hashlib
|
|
| 10 |
import io
|
| 11 |
from fastapi import FastAPI, UploadFile, File, HTTPException, BackgroundTasks, Response, Request, Depends, Header
|
| 12 |
from datetime import datetime
|
|
|
|
|
|
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
| 14 |
from fastapi.responses import FileResponse
|
| 15 |
from pydantic import BaseModel
|
|
@@ -27,8 +29,12 @@ logger = logging.getLogger(__name__)
|
|
| 27 |
cookies_content = os.environ.get('YTDLP_COOKIES_CONTENT')
|
| 28 |
cookies_file_path = None
|
| 29 |
|
| 30 |
-
if cookies_content:
|
| 31 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Create a temporary file for yt-dlp to read
|
| 33 |
with tempfile.NamedTemporaryFile(mode='w', delete=False, encoding='utf-8') as tmp_file:
|
| 34 |
tmp_file.write(cookies_content)
|
|
@@ -326,9 +332,27 @@ def process_youtube_audio(data: YouTubeInput, background_tasks: BackgroundTasks)
|
|
| 326 |
subprocess.run(dl_cmd, check=True, capture_output=True, text=True)
|
| 327 |
except subprocess.CalledProcessError as e:
|
| 328 |
logger.error(f"yt-dlp download failed: {e.stderr}")
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
# 3. Process with Demucs
|
| 334 |
extracted_dir = run_demucs_and_extract(audio_path, tmpdir)
|
|
|
|
| 10 |
import io
|
| 11 |
from fastapi import FastAPI, UploadFile, File, HTTPException, BackgroundTasks, Response, Request, Depends, Header
|
| 12 |
from datetime import datetime
|
| 13 |
+
from pytubefix import YouTube as PyT
|
| 14 |
+
from pytubefix.cli import on_progress
|
| 15 |
from fastapi.middleware.cors import CORSMiddleware
|
| 16 |
from fastapi.responses import FileResponse
|
| 17 |
from pydantic import BaseModel
|
|
|
|
| 29 |
cookies_content = os.environ.get('YTDLP_COOKIES_CONTENT')
|
| 30 |
cookies_file_path = None
|
| 31 |
|
|
|
|
| 32 |
try:
|
| 33 |
+
# 1. Handle literal "\n" strings (common in Render/Heroku env vars)
|
| 34 |
+
if '\\n' in cookies_content and '\n' not in cookies_content:
|
| 35 |
+
cookies_content = cookies_content.replace('\\n', '\n')
|
| 36 |
+
logger.info("Fixed escaped newlines in YTDLP_COOKIES_CONTENT")
|
| 37 |
+
|
| 38 |
# Create a temporary file for yt-dlp to read
|
| 39 |
with tempfile.NamedTemporaryFile(mode='w', delete=False, encoding='utf-8') as tmp_file:
|
| 40 |
tmp_file.write(cookies_content)
|
|
|
|
| 332 |
subprocess.run(dl_cmd, check=True, capture_output=True, text=True)
|
| 333 |
except subprocess.CalledProcessError as e:
|
| 334 |
logger.error(f"yt-dlp download failed: {e.stderr}")
|
| 335 |
+
logger.info("Retrying with pytubefix fallback...")
|
| 336 |
+
|
| 337 |
+
try:
|
| 338 |
+
# Fallback to pytubefix which uses android clients by default to bypass bots
|
| 339 |
+
yt = PyT(data.youtube_url, use_po_token=True if PO_TOKEN else False)
|
| 340 |
+
# yt.streams.get_audio_only('mp4') download
|
| 341 |
+
stream = yt.streams.get_audio_only()
|
| 342 |
+
if not stream:
|
| 343 |
+
raise Exception("No audio stream found with pytubefix")
|
| 344 |
+
|
| 345 |
+
# Download to temp path
|
| 346 |
+
downloaded_file = stream.download(output_path=tmpdir, filename="audio_temp")
|
| 347 |
+
|
| 348 |
+
# Convert to mp3 using ffmpeg manually since pytubefix just downloads
|
| 349 |
+
subprocess.run([FFMPEG_PATH, "-y", "-i", downloaded_file, "-ac", "2", "-ar", "44100", audio_path], check=True)
|
| 350 |
+
except Exception as e2:
|
| 351 |
+
logger.error(f"pytubefix fallback also failed: {e2}")
|
| 352 |
+
# Raise the original yt-dlp error as it's usually more descriptive
|
| 353 |
+
if "Incomplete YouTube ID" in e.stderr:
|
| 354 |
+
raise HTTPException(status_code=400, detail="Invalid or truncated YouTube URL ID.")
|
| 355 |
+
raise e
|
| 356 |
|
| 357 |
# 3. Process with Demucs
|
| 358 |
extracted_dir = run_demucs_and_extract(audio_path, tmpdir)
|
requirements.txt
CHANGED
|
@@ -10,6 +10,7 @@ firebase-admin
|
|
| 10 |
torchcodec
|
| 11 |
torch
|
| 12 |
torchaudio
|
|
|
|
| 13 |
|
| 14 |
# NOTE: Demucs is installed separately via install.sh or build script
|
| 15 |
# to avoid torchaudio version conflict (demucs requires <2.1, but only 2.6+ available)
|
|
|
|
| 10 |
torchcodec
|
| 11 |
torch
|
| 12 |
torchaudio
|
| 13 |
+
pytubefix
|
| 14 |
|
| 15 |
# NOTE: Demucs is installed separately via install.sh or build script
|
| 16 |
# to avoid torchaudio version conflict (demucs requires <2.1, but only 2.6+ available)
|