File size: 3,051 Bytes
20cfce6 65b6150 20cfce6 1d00ee2 b35c517 65b6150 b35c517 20cfce6 65b6150 b35c517 65b6150 1d00ee2 65b6150 20cfce6 65b6150 20cfce6 bb64521 82f5d70 20cfce6 b35c517 20cfce6 b35c517 20cfce6 65b6150 20cfce6 65b6150 20cfce6 bb64521 5030b3b 20cfce6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
import yt_dlp
import base64
import os
app = FastAPI(
title="YouTube Download API",
description="API for retrieving YouTube audio/video download links using yt-dlp.",
version="1.0.0",
contact={
"name": "Your Name",
"url": "https://github.com/yourusername",
"email": "[email protected]",
}
)
class VideoRequest(BaseModel):
video_id: str = Field(..., example="dQw4w9WgXcQ", description="YouTube video ID (e.g., dQw4w9WgXcQ)")
class AudioResponse(BaseModel):
status: str = Field(..., example="success")
audio_url: str = Field(..., description="Base64 encoded direct audio URL")
class VideoResponse(BaseModel):
status: str = Field(..., example="success")
video_url: str = Field(..., description="Base64 encoded direct video URL")
COOKIES_PATH = "cookies.txt"
@app.post("/audio/", response_model=AudioResponse, summary="Get YouTube Audio URL")
def get_audio_link(req: VideoRequest):
"""
Returns a downloadable audio link for a given YouTube video ID.
The URL is base64-encoded.
"""
url = f"https://www.youtube.com/watch?v={req.video_id}"
ydl_opts = {
'quiet': True,
'format': 'bestaudio/best',
'skip_download': True,
'forceurl': True,
'cookies': COOKIES_PATH if os.path.isfile(COOKIES_PATH) else None,
'cache_dir': False
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
for f in info['formats']:
if f.get('acodec') != 'none' and f.get('vcodec') == 'none':
audio_url = f['url']
b64 = base64.b64encode(audio_url.encode()).decode()
return {"status": "success", "audio_url": b64}
raise Exception("No audio found")
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post("/video/", response_model=VideoResponse, summary="Get YouTube Video URL")
def get_video_link(req: VideoRequest):
"""
Returns a downloadable video link (max 720p) for a given YouTube video ID.
The URL is base64-encoded.
"""
url = f"https://www.youtube.com/watch?v={req.video_id}"
ydl_opts = {
'quiet': True,
'format': 'best[height<=720][width<=1280]',
'skip_download': True,
'forceurl': True,
'cookies': COOKIES_PATH if os.path.isfile(COOKIES_PATH) else None,
'cache_dir': False
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
for f in info['formats']:
if f.get('vcodec') != 'none':
video_url = f['url']
b64 = base64.b64encode(video_url.encode()).decode()
return {"status": "success", "video_url": b64}
raise Exception("No video found")
except Exception as e:
raise HTTPException(status_code=400, detail=str(e)) |