Update app.py
Browse files
app.py
CHANGED
@@ -697,7 +697,60 @@ async def download_high_quality_video(request: Request):
|
|
697 |
return {"url": download_url, "requests_remaining": rate_limiter.max_requests - rate_limiter.get_current_count(user_ip)}
|
698 |
|
699 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
700 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
701 |
|
702 |
|
703 |
|
|
|
697 |
return {"url": download_url, "requests_remaining": rate_limiter.max_requests - rate_limiter.get_current_count(user_ip)}
|
698 |
|
699 |
|
700 |
+
@app.post("/high")
|
701 |
+
async def download_high_video(request: Request):
|
702 |
+
data = await request.json()
|
703 |
+
video_url = data.get('url')
|
704 |
+
is_youtube_url = re.search(r'(youtube\.com|youtu\.be)', video_url) is not None
|
705 |
+
quality = data.get('quality', '1080')
|
706 |
+
logger.info(f'input: {video_url} {quality}')
|
707 |
+
cookiefile = "firefox-cookies.txt"
|
708 |
+
env_to_cookies_from_env("firefox-cookies.txt")
|
709 |
+
|
710 |
+
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
711 |
+
output_template = str(Path(global_download_dir) / f'%(title)s_{timestamp}.%(ext)s')
|
712 |
+
|
713 |
+
# 转换质量字符串为数值
|
714 |
+
height_map = {
|
715 |
+
'480': 480,
|
716 |
+
'720': 720,
|
717 |
+
'1080': 1080,
|
718 |
+
'1440': 1440,
|
719 |
+
'2160': 2160
|
720 |
+
}
|
721 |
+
max_height = height_map.get(quality, 1080)
|
722 |
+
|
723 |
+
# 为高分辨率视频(>1080p)不限制编码器,对较低分辨率优先选择H.264
|
724 |
+
if max_height > 1080:
|
725 |
+
# 支持横屏和竖屏视频 - 对于更高分辨率不限制编码器
|
726 |
+
format_str = f'bestvideo[height<={max_height}]/bestvideo[width<={max_height}]+bestaudio/best'
|
727 |
+
else:
|
728 |
+
# 优先选择H.264编码,同时支持横屏和竖屏(长边不超过指定分辨率的1.8倍)
|
729 |
+
long_edge = int(max_height * 1.8) # 假设长边与短边比例约为16:9
|
730 |
+
format_str = f'bestvideo[vcodec^=avc][height<={long_edge}][width<={long_edge}]+bestaudio/best'
|
731 |
+
|
732 |
+
ydl_opts = {
|
733 |
+
'format': format_str,
|
734 |
+
'outtmpl': output_template,
|
735 |
+
'quiet': True,
|
736 |
+
'no_warnings': True,
|
737 |
+
'noprogress': True,
|
738 |
+
'merge_output_format': 'mp4'
|
739 |
+
}
|
740 |
|
741 |
+
if is_youtube_url:
|
742 |
+
ydl_opts["cookiefile"] = "firefox-cookies.txt"
|
743 |
+
await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([video_url]))
|
744 |
+
|
745 |
+
downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp4"))
|
746 |
+
if not downloaded_files:
|
747 |
+
return {"error": "Download failed"}
|
748 |
+
|
749 |
+
downloaded_file = downloaded_files[0]
|
750 |
+
encoded_filename = urllib.parse.quote(downloaded_file.name)
|
751 |
+
download_url = f"{BASE_URL}/file/{encoded_filename}"
|
752 |
+
gc.collect()
|
753 |
+
return {"url": download_url}
|
754 |
|
755 |
|
756 |
|