Update app.py
Browse files
app.py
CHANGED
@@ -470,6 +470,39 @@ async def test_download(request: Request):
|
|
470 |
return response
|
471 |
|
472 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
473 |
async def get_audio_download_url(track_id: str, quality: str) -> str:
|
474 |
if quality == 'mp3':
|
475 |
type = 'audio'
|
|
|
470 |
return response
|
471 |
|
472 |
|
473 |
+
@app.post("/hls")
|
474 |
+
async def download_hls_video(request: Request):
|
475 |
+
data = await request.json()
|
476 |
+
hls_url = data.get('url')
|
477 |
+
|
478 |
+
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
479 |
+
output_template = str(Path(global_download_dir) / f'%(title)s_{timestamp}.%(ext)s')
|
480 |
+
|
481 |
+
ydl_opts = {
|
482 |
+
'format': 'best',
|
483 |
+
'outtmpl': output_template,
|
484 |
+
'quiet': True,
|
485 |
+
'no_warnings': True,
|
486 |
+
'noprogress': True,
|
487 |
+
'merge_output_format': 'mp4'
|
488 |
+
}
|
489 |
+
|
490 |
+
try:
|
491 |
+
await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([hls_url]))
|
492 |
+
except Exception as e:
|
493 |
+
return {"error": f"Download failed: {str(e)}"}
|
494 |
+
|
495 |
+
downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp4"))
|
496 |
+
if not downloaded_files:
|
497 |
+
return {"error": "Download failed"}
|
498 |
+
|
499 |
+
downloaded_file = downloaded_files[0]
|
500 |
+
encoded_filename = urllib.parse.quote(downloaded_file.name)
|
501 |
+
download_url = f"{BASE_URL}/file/{encoded_filename}"
|
502 |
+
gc.collect()
|
503 |
+
return {"url": download_url}
|
504 |
+
|
505 |
+
|
506 |
async def get_audio_download_url(track_id: str, quality: str) -> str:
|
507 |
if quality == 'mp3':
|
508 |
type = 'audio'
|