一个简单易用的B站视频下载Python库
Project description
Bilibili Downloader
一个简单易用的B站视频下载Python库,支持下载视频和音频。
特性
- 支持下载B站视频(包括音视频合并)
- 支持仅下载音频并转换格式(mp3, wav, m4a)
- 支持分P视频下载
- 自动处理短链接跳转
- 支持设置最大下载时长限制
- 异步下载,提高效率
- 清晰的错误处理
安装
# 安装依赖
pip install aiohttp httpx requests ffmpy3
注意:还需要安装FFmpeg并确保在系统PATH中可用。
快速开始
基础用法
from bilibili_downloader import BilibiliDownloader
# 创建下载器实例
downloader = BilibiliDownloader()
# 下载音频
result = downloader.download_audio("https://www.bilibili.com/video/BV1xx411c7mD")
if result.success:
print(f"下载成功: {result.file_path}")
else:
print(f"下载失败: {result.message}")
# 下载视频
result = downloader.download_video("https://www.bilibili.com/video/BV1xx411c7mD")
if result.success:
print(f"下载成功: {result.file_path}")
高级配置
# 使用认证信息(可选,用于下载高清视频)
downloader = BilibiliDownloader(
sessdata="your_sessdata",
bili_jct="your_bili_jct",
buvid3="your_buvid3",
download_dir="./my_downloads",
ffmpeg_path="/usr/local/bin/ffmpeg",
max_duration=7200 # 最大2小时
)
# 指定输出路径和格式
result = downloader.download_audio(
"https://www.bilibili.com/video/BV1xx411c7mD",
output_path="./output/audio.mp3",
audio_format="mp3"
)
# 获取视频信息
video_info = downloader.get_video_info("https://www.bilibili.com/video/BV1xx411c7mD")
print(f"标题: {video_info.title}")
print(f"时长: {video_info.duration}秒")
# 检查视频时长
ok, msg, duration = downloader.check_duration("https://www.bilibili.com/video/BV1xx411c7mD")
if ok:
print(f"视频时长: {duration}秒")
else:
print(f"错误: {msg}")
异步使用
import asyncio
async def download_multiple():
downloader = BilibiliDownloader()
urls = [
"https://www.bilibili.com/video/BV1xx411c7mD",
"https://www.bilibili.com/video/BV1yy4y1k7VD",
]
tasks = [downloader.download_audio_async(url) for url in urls]
results = await asyncio.gather(*tasks)
for result in results:
if result.success:
print(f"下载成功: {result.file_path}")
else:
print(f"下载失败: {result.message}")
# 运行异步下载
asyncio.run(download_multiple())
API 参考
BilibiliDownloader
初始化参数
sessdata(str): B站Cookie中的SESSDATA,用于认证bili_jct(str): B站Cookie中的bili_jctbuvid3(str): B站Cookie中的buvid3download_dir(str): 下载目录,默认"./downloads"ffmpeg_path(str): FFmpeg路径,不指定则自动查找max_duration(int): 最大允许下载时长(秒),默认10800(3小时)
方法
get_video_info(url: str) -> VideoInfo: 获取视频信息download_audio(url: str, output_path: str = None, audio_format: str = "mp3") -> DownloadResult: 下载音频download_video(url: str, output_path: str = None, video_format: str = "mp4") -> DownloadResult: 下载视频check_duration(url: str) -> Tuple[bool, str, int]: 检查视频时长
数据模型
VideoInfo
bvid: 视频BV号title: 视频标题duration: 视频时长(秒)video_url: 视频流URLaudio_url: 音频流URLpage: 分P号
DownloadResult
success: 是否成功message: 结果消息file_path: 下载文件路径duration: 视频时长video_info: 视频信息对象
异常处理
SDK定义了以下异常类:
BilibiliDownloadError: 基础异常类VideoNotFoundError: 视频未找到DurationExceededError: 视频时长超出限制NetworkError: 网络请求失败FFmpegError: FFmpeg处理失败
from bilibili_downloader import BilibiliDownloader, VideoNotFoundError, DurationExceededError
downloader = BilibiliDownloader(max_duration=3600) # 1小时限制
try:
result = downloader.download_audio("https://www.bilibili.com/video/BV1xx411c7mD")
except VideoNotFoundError:
print("视频不存在")
except DurationExceededError:
print("视频时长超过限制")
except Exception as e:
print(f"下载失败: {e}")
注意事项
- 需要安装FFmpeg才能正常使用
- 下载高清视频可能需要提供Cookie认证信息
- 请遵守B站的使用条款,合理使用下载功能
- 默认限制最大下载时长为3小时,可通过参数调整
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bilibili_downloader-0.0.3.tar.gz.
File metadata
- Download URL: bilibili_downloader-0.0.3.tar.gz
- Upload date:
- Size: 21.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ab6da70fae34a49e2010c93015b1ab10c83c75967c972fec4c73405b923b9f4
|
|
| MD5 |
032353d0f9e35b8af0b1cf63aa13e3ce
|
|
| BLAKE2b-256 |
a897a0f67c6790ebefdf4bf02207ab91deef05bca218fe82221ba33b1f6a17cd
|
Provenance
The following attestation bundles were made for bilibili_downloader-0.0.3.tar.gz:
Publisher:
publish.yml on twwch/bilibili-downloader
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bilibili_downloader-0.0.3.tar.gz -
Subject digest:
2ab6da70fae34a49e2010c93015b1ab10c83c75967c972fec4c73405b923b9f4 - Sigstore transparency entry: 313979157
- Sigstore integration time:
-
Permalink:
twwch/bilibili-downloader@b1890a67e8d615f88b9cc50f9488b3f6ab36d425 -
Branch / Tag:
refs/tags/v0.0.3 - Owner: https://github.com/twwch
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b1890a67e8d615f88b9cc50f9488b3f6ab36d425 -
Trigger Event:
push
-
Statement type:
File details
Details for the file bilibili_downloader-0.0.3-py3-none-any.whl.
File metadata
- Download URL: bilibili_downloader-0.0.3-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a0414cce04c941e3766ffd41f5beee41f30d340acf624a3a9f2df06a2254add
|
|
| MD5 |
59e125d8b7842d28c9ac3a31a98851e8
|
|
| BLAKE2b-256 |
a20523ba89a7bcc7b7e774057fb6727e3edc8c01421ae644da526d2bc15138cf
|
Provenance
The following attestation bundles were made for bilibili_downloader-0.0.3-py3-none-any.whl:
Publisher:
publish.yml on twwch/bilibili-downloader
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bilibili_downloader-0.0.3-py3-none-any.whl -
Subject digest:
5a0414cce04c941e3766ffd41f5beee41f30d340acf624a3a9f2df06a2254add - Sigstore transparency entry: 313979167
- Sigstore integration time:
-
Permalink:
twwch/bilibili-downloader@b1890a67e8d615f88b9cc50f9488b3f6ab36d425 -
Branch / Tag:
refs/tags/v0.0.3 - Owner: https://github.com/twwch
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b1890a67e8d615f88b9cc50f9488b3f6ab36d425 -
Trigger Event:
push
-
Statement type: