File size: 15,326 Bytes
87c25b2
4a41603
 
 
 
 
53f450f
4a41603
 
 
87c25b2
 
4a41603
 
 
 
 
 
 
 
 
53f450f
4a41603
53f450f
4a41603
 
87c25b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a41603
 
 
 
 
87c25b2
 
 
 
 
4a41603
 
87c25b2
4a41603
 
 
53f450f
4a41603
 
 
 
53f450f
4a41603
 
87c25b2
4a41603
53f450f
 
 
 
 
 
 
 
 
 
 
4a41603
 
 
 
 
53f450f
 
 
 
 
 
4a41603
87c25b2
 
 
 
 
 
 
 
 
 
 
 
4a41603
 
87c25b2
 
 
 
 
53f450f
87c25b2
53f450f
4a41603
 
 
53f450f
 
 
 
 
 
87c25b2
 
4a41603
53f450f
4a41603
 
 
 
87c25b2
 
53f450f
4a41603
53f450f
 
 
 
 
 
 
 
 
 
 
 
87c25b2
53f450f
 
 
 
 
 
 
 
 
 
 
87c25b2
53f450f
 
 
 
 
 
 
4a41603
87c25b2
53f450f
 
87c25b2
53f450f
87c25b2
53f450f
87c25b2
53f450f
87c25b2
53f450f
 
4a41603
87c25b2
53f450f
 
87c25b2
 
53f450f
 
 
4a41603
87c25b2
4a41603
53f450f
87c25b2
53f450f
 
 
87c25b2
 
 
4a41603
53f450f
4a41603
87c25b2
4a41603
 
 
 
87c25b2
 
 
 
 
 
 
 
 
 
 
53f450f
 
 
 
 
87c25b2
53f450f
 
4a41603
 
 
53f450f
87c25b2
 
4a41603
 
 
 
 
 
 
 
 
 
 
 
 
 
53f450f
87c25b2
4a41603
53f450f
4a41603
 
 
 
 
 
 
 
87c25b2
 
53f450f
87c25b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a41603
 
 
 
 
 
53f450f
87c25b2
 
 
 
 
 
 
 
4a41603
 
87c25b2
53f450f
87c25b2
 
 
53f450f
87c25b2
 
53f450f
4a41603
 
 
87c25b2
 
4a41603
 
53f450f
 
4a41603
 
87c25b2
4a41603
87c25b2
53f450f
4a41603
87c25b2
 
 
 
 
 
 
53f450f
 
 
87c25b2
 
53f450f
 
 
 
 
4a41603
87c25b2
 
 
 
 
 
 
 
4a41603
87c25b2
4a41603
87c25b2
53f450f
 
87c25b2
 
4a41603
 
 
 
 
53f450f
 
 
 
 
 
 
 
4a41603
53f450f
87c25b2
53f450f
4a41603
 
 
53f450f
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# app/main.py
import os
import json
import asyncio
import uuid
import shutil
import re
from pathlib import Path
from typing import Dict, Any, List, Optional

from fastapi import FastAPI, WebSocket, WebSocketDisconnect, HTTPException
from fastapi.responses import HTMLResponse, FileResponse
from fastapi.staticfiles import StaticFiles
import yt_dlp
from pydantic import BaseModel, HttpUrl

app = FastAPI(title="Pro Video Downloader API")

# Configuration
DOWNLOAD_DIR = Path("/app/downloads")
DOWNLOAD_DIR.mkdir(exist_ok=True)
MAX_FILE_AGE_SECONDS = 1800  # 30 minutes

# Mount static files
app.mount("/static", StaticFiles(directory="app/static"), name="static")

# --- Constantes Communes ---
# User-Agent Chrome 126 (Windows) - Mis à jour Juillet 2024
STEALTH_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'

# Headers HTTP complets pour passer pour un vrai navigateur (Anti-Bot / Generic Extractor)
STEALTH_HEADERS = {
    'User-Agent': STEALTH_UA,
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Language': 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7',
    'Accept-Encoding': 'gzip, deflate, br, zstd',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'none',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'Referer': 'https://www.google.com/', # Origine "propre"
}

# Headers pour le téléchargement des fragments vidéo (m3u8/mp4) - Referer = page vidéo
def get_download_headers(referer_url: str) -> Dict[str, str]:
    origin = '/'.join(referer_url.split('/')[:3]) # https://domain.com
    return {
        'User-Agent': STEALTH_UA,
        'Accept': '*/*',
        'Accept-Language': 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7',
        'Accept-Encoding': 'gzip, deflate, br, zstd',
        'Referer': referer_url,
        'Origin': origin,
        'Sec-Fetch-Dest': 'video',
        'Sec-Fetch-Mode': 'no-cors',
        'Sec-Fetch-Site': 'cross-site',
    }

# Args pour forcer l'extracteur generic (sites sans support natif)
GENERIC_EXTRACTOR_ARGS = {
    'extractor_args': {
        'generic': {
            'force_extraction': True, # Essaye même si pattern inconnu
        }
    }
}

# --- Modèles Pydantic ---
class AnalyzeRequest(BaseModel):
    url: HttpUrl

class FormatInfo(BaseModel):
    format_id: str
    ext: str
    resolution: str
    filesize_mb: float
    note: str
    vcodec: str
    acodec: str
    is_auto: bool = False

class VideoMeta(BaseModel):
    title: str
    duration: int
    uploader: str
    thumbnail: str
    formats: List[FormatInfo]

# --- Nettoyage Périodique (30 min) ---
async def cleanup_old_files():
    while True:
        await asyncio.sleep(60)
        now = asyncio.get_event_loop().time()
        try:
            for f in DOWNLOAD_DIR.glob("*"):
                if f.is_dir():
                    try:
                        mtime = f.stat().st_mtime
                        if (now - mtime) > MAX_FILE_AGE_SECONDS:
                            shutil.rmtree(f, ignore_errors=True)
                    except Exception:
                        pass
        except Exception:
            pass

@app.on_event("startup")
async def startup_event():
    asyncio.create_task(cleanup_old_files())

# --- Helpers ---

def sanitize_filename(name: str, max_len: int = 200) -> str:
    name = re.sub(r'[\\/*?:"<>|]', "", name)
    name = re.sub(r'\s+', ' ', name).strip()
    return name[:max_len]

def get_base_ydl_opts(extra_headers: Dict = None) -> Dict:
    """Base configuration yt-dlp commune (Stealth + Timeouts)."""
    opts = {
        'quiet': True,
        'skip_download': True,
        'noplaylist': True,
        'socket_timeout': 30,
        'retries': 5,
        'http_headers': {**STEALTH_HEADERS, **(extra_headers or {})},
        **GENERIC_EXTRACTOR_ARGS,
    }
    return opts

def analyze_url_sync(url: str) -> VideoMeta:
    """Extraction infos + Construction formats intelligents (Auto)."""
    ydl_opts = get_base_ydl_opts()
    ydl_opts.update({
        'forcejson': True,
        'simulate': True,
        'format': 'bestvideo+bestaudio/best', 
    })
    
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(url, download=False)
    
    title = info.get('title', 'Sans Titre')
    duration = info.get('duration', 0)
    uploader = info.get('uploader', 'Inconnu')
    thumbnail = info.get('thumbnail', '')
    raw_formats = info.get('formats', [])
    
    best_video = {}
    best_audio = {}
    
    for f in raw_formats:
        vcodec = f.get('vcodec', 'none')
        acodec = f.get('acodec', 'none')
        fid = f.get('format_id')
        height = f.get('height')
        abr = f.get('abr')
        tbr = f.get('tbr', 0)
        fs = f.get('filesize') or f.get('filesize_approx', 0)
        
        if vcodec != 'none' and acodec == 'none': # Video Only
            if height:
                if height not in best_video or tbr > best_video[height].get('tbr', 0):
                    best_video[height] = f
        elif vcodec == 'none' and acodec != 'none': # Audio Only
            if abr:
                if abr not in best_audio or tbr > best_audio[abr].get('tbr', 0):
                    best_audio[abr] = f
                    
    formats_out: List[FormatInfo] = []
    standard_heights = [2160, 1440, 1080, 720, 480, 360]
    
    # A. Modes "Auto" Vidéo+Son
    for h in standard_heights:
        if h in best_video:
            v_fmt = best_video[h]
            best_a_fmt = max(best_audio.values(), key=lambda x: x.get('abr', 0), default=None)
            if best_a_fmt:
                selector = f"{v_fmt['format_id']}+{best_a_fmt['format_id']}"
                v_size = v_fmt.get('filesize') or v_fmt.get('filesize_approx', 0)
                a_size = best_a_fmt.get('filesize') or best_a_fmt.get('filesize_approx', 0)
                est_mb = round((v_size + a_size) / 1e6, 1) if (v_size or a_size) else 0
                
                formats_out.append(FormatInfo(
                    format_id=selector, ext="mp4",
                    resolution=f"{h}p (Auto • Vidéo+Son)",
                    filesize_mb=est_mb,
                    note=f"Meilleure vidéo {h}p + Meilleur son (Fusion FFmpeg)",
                    vcodec=v_fmt.get('vcodec', 'none'),
                    acodec=best_a_fmt.get('acodec', 'none'),
                    is_auto=True
                ))

    # B. Mode Audio Only (Auto)
    if best_audio:
        best_a = max(best_audio.values(), key=lambda x: x.get('abr', 0))
        a_size = best_a.get('filesize') or best_a.get('filesize_approx', 0)
        formats_out.append(FormatInfo(
            format_id=best_a['format_id'], ext="mp3",
            resolution="Audio MP3 (Auto • Meilleur qualité)",
            filesize_mb=round(a_size / 1e6, 1) if a_size else 0,
            note=f"Extraction audio {best_a.get('abr', '?')}kbps → MP3 192kbps",
            vcodec='none', acodec=best_a.get('acodec', 'none'),
            is_auto=True
        ))

    # C. Fallback absolu
    if not formats_out:
        formats_out.append(FormatInfo(
            format_id="bestvideo+bestaudio/best", ext="mp4",
            resolution="Auto (Meilleur dispo)", filesize_mb=0,
            note="Sélection automatique yt-dlp (Fallback)",
            vcodec='?', acodec='?', is_auto=True
        ))

    return VideoMeta(title=title, duration=duration, uploader=uploader, thumbnail=thumbnail, formats=formats_out)

def download_sync(url: str, format_id: str, audio_only: bool, progress_hook, session_dir: Path, original_title: str) -> str:
    """Téléchargement bloquant robuste. Retourne chemin fichier final."""
    safe_title = sanitize_filename(original_title)
    output_template = str(session_dir / f"{safe_title}.%(ext)s")
    
    # Headers spécifiques pour le DL (Referer = URL vidéo)
    dl_headers = get_download_headers(url)
    
    ydl_opts = {
        'format': format_id,
        'outtmpl': output_template,
        'merge_output_format': 'mp4',
        'progress_hooks': [progress_hook],
        'noplaylist': True,
        'quiet': True,
        'no_warnings': True,
        # ROBUSTESSE RÉSEAU / PROXY HF :
        'socket_timeout': 30,
        'retries': 10,
        'fragment_retries': 10,
        'retry_sleep_functions': {
            'http': lambda n: min(2 ** n, 60),
            'fragment': lambda n: min(2 ** n, 60),
        },
        'concurrent_fragment_downloads': 3,
        # HEADERS STEALTH POUR FRAGMENTS
        'http_headers': dl_headers,
        'postprocessors': []
    }
    
    if audio_only:
        ydl_opts['postprocessors'].append({
            'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '192',
        })
        
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
    
    candidates = list(session_dir.glob("*"))
    if not candidates: raise FileNotFoundError("Aucun fichier généré par yt-dlp.")
    return str(max(candidates, key=lambda f: f.stat().st_mtime))

# --- Routes API ---

@app.get("/", response_class=HTMLResponse)
async def read_root():
    with open("app/static/index.html", "r", encoding="utf-8") as f:
        return f.read()

@app.post("/api/analyze", response_model=VideoMeta)
async def analyze_video(req: AnalyzeRequest):
    try:
        loop = asyncio.get_event_loop()
        meta = await loop.run_in_executor(None, analyze_url_sync, str(req.url))
        return meta
    except yt_dlp.utils.DownloadError as e:
        raise HTTPException(status_code=400, detail=f"Lien invalide, vidéo privée ou site non supporté: {str(e)}")
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Erreur analyse: {str(e)}")

@app.websocket("/api/download")
async def websocket_download(ws: WebSocket):
    await ws.accept()
    session_id = str(uuid.uuid4())[:8]
    session_dir = DOWNLOAD_DIR / session_id
    session_dir.mkdir(exist_ok=True)
    
    progress_queue: asyncio.Queue = asyncio.Queue(maxsize=50)
    active = True
    
    def get_progress_hook(q: asyncio.Queue):
        def hook(d: Dict[str, Any]):
            if not active: return
            if d['status'] == 'downloading':
                downloaded = d.get('downloaded_bytes', 0)
                total = d.get('total_bytes') or d.get('total_bytes_estimate', 0)
                speed = d.get('speed', 0)
                eta = d.get('eta', 0)
                percent_str = d.get('_percent_str', '0%').strip()
                try:
                    q.put_nowait({
                        "stage": "downloading", "percent": percent_str,
                        "downloaded_mb": round(downloaded / 1e6, 2),
                        "total_mb": round(total / 1e6, 2) if total else 0,
                        "speed_mbps": round(speed / 1e6, 2) if speed else 0,
                        "eta_sec": eta, "info": f"Téléchargement... {percent_str}"
                    })
                except asyncio.QueueFull: pass
            elif d['status'] == 'finished':
                try: q.put_nowait({"stage": "post_processing", "info": "Fusion / Conversion (FFmpeg)...", "percent": "99%"})
                except asyncio.QueueFull: pass
            elif d['status'] == 'error':
                try: q.put_nowait({"stage": "error", "info": str(d.get('error', 'Erreur inconnue'))})
                except asyncio.QueueFull: pass
        return hook

    async def heartbeat_sender():
        while active:
            await asyncio.sleep(10)
            if not active: break
            try:
                await ws.send_json({"stage": "heartbeat", "info": "keep-alive"})
            except Exception:
                break

    try:
        config = await ws.receive_json()
        url = config['url']
        format_id = config['format_id']
        audio_only = config.get('audio_only', False)
        
        hook = get_progress_hook(progress_queue)
        loop = asyncio.get_event_loop()

        # 1. Récupérer titre original (AVEC HEADERS STEALTH)
        info = await loop.run_in_executor(None, lambda: yt_dlp.YoutubeDL(get_base_ydl_opts()).extract_info(url, download=False))
        original_title = info.get('title', 'video')

        # 2. Lancer tâches parallèles
        hb_task = asyncio.create_task(heartbeat_sender())
        
        async def progress_sender():
            while active:
                try:
                    msg = await asyncio.wait_for(progress_queue.get(), timeout=1.0)
                    if msg.get("stage") == "finished_download":
                        break
                    await ws.send_json(msg)
                except asyncio.TimeoutError:
                    continue
                except Exception:
                    break
        
        sender_task = asyncio.create_task(progress_sender())

        # 3. Download dans Executor
        def run_download():
            try:
                final_path = download_sync(url, format_id, audio_only, hook, session_dir, original_title)
                progress_queue.put_nowait({"stage": "finished_download", "final_path": final_path, "filename": Path(final_path).name})
                return final_path
            except Exception as e:
                progress_queue.put_nowait({"stage": "error", "info": f"Erreur DL: {str(e)}"})
                raise

        await loop.run_in_executor(None, run_download)
        
        # 4. Attendre fin envoi progression
        await sender_task
        active = False
        hb_task.cancel()
        try: await hb_task
        except: pass

        # 5. Réponse Finale
        final_files = list(session_dir.glob("*"))
        if not final_files:
            try: await ws.send_json({"stage": "error", "info": "Fichier introuvable post-traitement."})
            except: pass
            return
            
        final_file_path = final_files[0]
        final_filename = final_file_path.name
        download_url = f"/api/file/{session_id}/{final_filename}"
        
        try:
            await ws.send_json({
                "stage": "complete", "info": "Terminé ! Cliquez pour sauvegarder.",
                "download_url": download_url, "filename": final_filename
            })
        except Exception:
            pass

    except WebSocketDisconnect:
        print(f"[{session_id}] Client déconnecté.")
    except Exception as e:
        print(f"[{session_id}] Erreur critique: {e}")
        try: await ws.send_json({"stage": "error", "info": f"Erreur serveur: {str(e)}"})
        except: pass
    finally:
        active = False

@app.get("/api/file/{session_id}/{filename}")
async def serve_file(session_id: str, filename: str):
    file_path = DOWNLOAD_DIR / session_id / filename
    
    try:
        file_path.resolve().relative_to(DOWNLOAD_DIR.resolve())
    except ValueError:
        raise HTTPException(403, "Accès interdit")
        
    if not file_path.exists():
        raise HTTPException(404, "Fichier expiré ou introuvable (nettoyé après 30min)")

    return FileResponse(
        path=file_path,
        filename=filename,
        media_type='application/octet-stream'
    )

@app.get("/healthz")
async def health(): return {"status": "ok"}