File size: 2,349 Bytes
7752dfa
ac7affe
7752dfa
 
 
 
cda7af5
7752dfa
 
ac7affe
7752dfa
 
 
cda7af5
7752dfa
 
cda7af5
 
 
 
5f7e2af
ac7affe
cda7af5
 
5f7e2af
 
 
 
ac7affe
7752dfa
5f7e2af
 
 
 
 
 
 
cda7af5
5f7e2af
 
 
 
cda7af5
5f7e2af
 
 
ac7affe
cda7af5
7752dfa
cda7af5
7752dfa
 
5f7e2af
cda7af5
7752dfa
cda7af5
 
 
 
7752dfa
 
5f7e2af
 
 
 
7752dfa
5f7e2af
7752dfa
cda7af5
7752dfa
ac7affe
7752dfa
 
5f7e2af
cda7af5
 
 
 
7752dfa
cda7af5
7752dfa
 
5f7e2af
 
 
 
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
84
85
86
from fastapi import FastAPI, Query
from fastapi.responses import StreamingResponse, JSONResponse
import yt_dlp

app = FastAPI(
    title="TikTok Downloader API",
    version="1.2"
)

YDL_OPTS = {
    "quiet": True,
    "skip_download": True,
    "noplaylist": True,
    "nocheckcertificate": True,
}

def stream_with_ytdlp(media_url: str, headers: dict, content_type: str):
    ydl = yt_dlp.YoutubeDL(YDL_OPTS)
    req = yt_dlp.networking.Request(media_url, headers=headers)
    res = ydl.urlopen(req)

    return StreamingResponse(
        res,
        media_type=content_type,
        headers={
            "Cache-Control": "no-store",
            "Accept-Ranges": "bytes"
        }
    )

# ================= ROOT =================
@app.get("/")
def root():
    return {
        "status": True,
        "service": "TikTok Downloader API",
        "author": "Farel",
        "version": "1.2",
        "endpoints": {
            "mp4": "/api/mp4?url=https://vt.tiktok.com/xxxx/",
            "mp3": "/api/mp3?url=https://vt.tiktok.com/xxxx/"
        },
        "note": "True streaming via yt-dlp (anti 403)"
    }

# ================= MP4 ==================
@app.get("/api/mp4")
def mp4(url: str = Query(...)):
    try:
        with yt_dlp.YoutubeDL(YDL_OPTS) as ydl:
            info = ydl.extract_info(url, download=False)

        media_url = info.get("url")
        headers = info.get("http_headers")

        if not media_url or not headers:
            raise Exception("Stream data not found")

        return stream_with_ytdlp(media_url, headers, "video/mp4")

    except Exception as e:
        return JSONResponse(
            status_code=500,
            content={"status": False, "error": str(e)}
        )

# ================= MP3 ==================
@app.get("/api/mp3")
def mp3(url: str = Query(...)):
    try:
        with yt_dlp.YoutubeDL(YDL_OPTS | {"format": "bestaudio"}) as ydl:
            info = ydl.extract_info(url, download=False)

        media_url = info.get("url")
        headers = info.get("http_headers")

        if not media_url or not headers:
            raise Exception("Stream data not found")

        return stream_with_ytdlp(media_url, headers, "audio/mpeg")

    except Exception as e:
        return JSONResponse(
            status_code=500,
            content={"status": False, "error": str(e)}
        )