Last active
April 15, 2025 09:02
-
-
Save krisstibex/cc02816de81fe7acdae7dd8705b7b201 to your computer and use it in GitHub Desktop.
need yt-dlp & ffmpeg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import re | |
from urllib.parse import urlparse | |
from typing import Optional | |
def extract_uid(url: str) -> Optional[str]: | |
if url.isdigit(): | |
return url | |
parsed = urlparse(url) | |
match = re.search(r'/(\d+)', parsed.path) | |
return match.group(1) if match else None | |
def get_video_urls_and_titles(uid: str): | |
print("正在获取视频列表...") | |
cmd = [ | |
"yt-dlp", | |
f"https://space.bilibili.com/{uid}/video", | |
"--flat-playlist", | |
"--print", "%(title)s||%(id)s" | |
] | |
result = subprocess.run(cmd, capture_output=True, text=True) | |
lines = result.stdout.strip().splitlines() | |
videos = [line.split("||") for line in lines if "||" in line] | |
return videos | |
def download_videos(videos, start: int, end: int): | |
selected = videos[start - 1:end] | |
if not selected: | |
print("选定范围内无视频。") | |
return | |
for idx, (title, bv) in enumerate(selected, start=start): | |
print(f"\n第 {idx} 个视频:{title}") | |
print(f"正在下载:https://www.bilibili.com/video/{bv}") | |
subprocess.run([ | |
"yt-dlp", | |
f"https://www.bilibili.com/video/{bv}", | |
"-o", "%(title)s.%(ext)s" | |
]) | |
print("\n下载完成。") | |
if __name__ == "__main__": | |
user_input = input("请输入 B站用户主页链接或 UID:").strip() | |
uid = extract_uid(user_input) | |
if not uid: | |
print("无效的链接或 UID。") | |
exit(1) | |
# 获取视频列表 | |
video_list = get_video_urls_and_titles(uid) | |
total = len(video_list) | |
print(f"共获取到 {total} 个视频") | |
# 输入开始和结束视频编号 | |
try: | |
start = int(input("请输入开始视频编号(从1开始):").strip() or "1") | |
end_input = input(f"请输入结束视频编号(共 {total} 个视频),直接回车下载到最后一个视频:").strip() | |
# 如果没有输入结束编号,默认下载到最后一个视频 | |
if not end_input: | |
end = total | |
else: | |
end = int(end_input) | |
except ValueError: | |
print("请输入有效数字") | |
exit(1) | |
# 验证输入的视频范围是否合法 | |
if start < 1 or end > total or start > end: | |
print("输入的视频范围不合法") | |
exit(1) | |
download_videos(video_list, start, end) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
import sys | |
def main(): | |
print("=== YouTube 批量下载器 ===") | |
url = input("请输入频道链接或视频合集链接: ").strip() | |
start_num = int(input("请输入开始下载的视频编号(从 1 开始): ").strip()) | |
end_num = int(input("请输入结束下载的视频编号: ").strip()) | |
# 检查 yt-dlp 是否安装 | |
if not shutil.which("yt-dlp"): | |
print("错误:未安装 yt-dlp,请先使用 pip 安装:pip install -U yt-dlp") | |
sys.exit(1) | |
# 设置下载路径为当前脚本所在目录 | |
output_path = os.path.join(os.getcwd(), "%(title)s.%(ext)s") | |
# 基础命令 | |
command = [ | |
"yt-dlp", | |
'--no-check-certificate', | |
"--playlist-start", str(start_num), | |
"--playlist-end", str(end_num), | |
"-o", output_path | |
] | |
# 检查 cookie.txt 是否存在 | |
cookie_path = os.path.join(os.getcwd(), "cookie.txt") | |
if os.path.isfile(cookie_path): | |
print("检测到 cookie.txt,启用 Cookie 登录") | |
command += ["--cookies", cookie_path] | |
else: | |
print("未检测到 cookie.txt,将不使用登录") | |
# 添加链接 | |
command.append(url) | |
print("\n开始下载...") | |
try: | |
subprocess.run(command, check=True) | |
except subprocess.CalledProcessError as e: | |
print("下载出错:", e) | |
if __name__ == "__main__": | |
import shutil | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment