43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
import os
|
|
import yt_dlp
|
|
|
|
def download_video(url, save_dir="/home/docker/openlist/ytb-video"):
|
|
os.makedirs(save_dir, exist_ok=True)
|
|
|
|
base_opts = {
|
|
'merge_output_format': 'mp4',
|
|
'outtmpl': f'{save_dir}/%(title)s.%(ext)s',
|
|
'quiet': False,
|
|
'verbose': True, # 调试模式
|
|
'retries': 5,
|
|
'fragment_retries': 5,
|
|
}
|
|
|
|
# 首选格式组合
|
|
formats = [
|
|
'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
|
'bestvideo+bestaudio/best'
|
|
]
|
|
|
|
for fmt in formats:
|
|
opts = dict(base_opts)
|
|
opts['format'] = fmt
|
|
print(f"🔧 尝试格式: {fmt}")
|
|
try:
|
|
with yt_dlp.YoutubeDL(opts) as ydl:
|
|
info = ydl.extract_info(url, download=True)
|
|
if info:
|
|
filepath = ydl.prepare_filename(info)
|
|
print(f"✅ 下载完成: {filepath}")
|
|
return filepath
|
|
except Exception as e:
|
|
print(f"⚠️ 格式 {fmt} 下载失败,异常: {e}")
|
|
continue
|
|
|
|
print("❌ 所有格式尝试均失败。")
|
|
return None
|
|
|
|
if __name__ == "__main__":
|
|
video_url = "https://www.youtube.com/watch?v=QL3T2Nzcqcs"
|
|
download_video(video_url)
|