Download main.py from triflix/testing: direct link, hf CLI and curl.
- Browser
- Download file 1.64 kB
-
https://huggingface.co/spaces/triflix/testing/resolve/main/main.py
- Command line
-
hf download hf://spaces/triflix/testing/main.py
-
curl -L -o main.py https://huggingface.co/spaces/triflix/testing/resolve/main/main.py
1.64 kB
| from fastapi import FastAPI, HTTPException | |
| from fastapi.responses import JSONResponse | |
| import yt_dlp | |
| app = FastAPI() | |
| def get_m4a_url(video_url: str) -> str: | |
| """ | |
| Extracts the best m4a audio stream URL from a YouTube video using yt_dlp with cookies. | |
| """ | |
| ydl_opts = { | |
| 'format': 'bestaudio[ext=m4a]/best', | |
| 'quiet': True, | |
| 'skip_download': True, | |
| 'cookies_from_browser': 'chrome', # automatically extract cookies from Chrome | |
| 'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)...', | |
| } | |
| try: | |
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
| info = ydl.extract_info(video_url, download=False) | |
| # Try to find an m4a format URL | |
| for fmt in info.get('formats', []): | |
| if fmt.get('ext') == 'm4a' and fmt.get('url'): | |
| return fmt.get('url') | |
| # Fallback to best audio URL if m4a is not found | |
| return info.get('url') if info.get('url') else None | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Failed to extract audio: {str(e)}") | |
| def get_audio_url(video_id: str): | |
| """ | |
| FastAPI endpoint to return the m4a audio stream URL in JSON format. | |
| """ | |
| youtube_url = f"https://www.youtube.com/watch?v={video_id}" | |
| m4a_url = get_m4a_url(youtube_url) | |
| if not m4a_url: | |
| raise HTTPException(status_code=404, detail="m4a audio stream not found") | |
| return JSONResponse(content={"m4a_url": m4a_url}) | |
| if __name__ == "__main__": | |
| import uvicorn | |
| # Run on port 7860 | |
| uvicorn.run(app, host="0.0.0.0", port=7860) | |