Skip to main content

Bilibili Downloader

PyPI version Python Version License CI Downloads

一个简单易用的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_jct
  • buvid3 (str): B站Cookie中的buvid3
  • download_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: 视频流URL
  • audio_url: 音频流URL
  • page: 分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}")

注意事项

  1. 需要安装FFmpeg才能正常使用
  2. 下载高清视频可能需要提供Cookie认证信息
  3. 请遵守B站的使用条款,合理使用下载功能
  4. 默认限制最大下载时长为3小时,可通过参数调整

License

MIT

Metadata

Release files for bilibili-downloader 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bilibili-downloader 0.0.3
File Size Uploaded Provenance Actions
bilibili_downloader-0.0.3.tar.gz 21.9 kB With provenance Details for bilibili_downloader-0.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bilibili-downloader 0.0.3
File Interpreter ABI Platform Actions
bilibili_downloader-0.0.3-py3-none-any.whl Python 3 none any Details for bilibili_downloader-0.0.3-py3-none-any.whl

Total release size: 32.4 kB

Release files / bilibili_downloader-0.0.3.tar.gz

Download URL bilibili_downloader-0.0.3.tar.gz
Size 21.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2ab6da70fae34a49e2010c93015b1ab10c83c75967c972fec4c73405b923b9f4
BLAKE2b-256 checksum
How to use checksums
a897a0f67c6790ebefdf4bf02207ab91deef05bca218fe82221ba33b1f6a17cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 26, 2025.

Transparency log

Release files / bilibili_downloader-0.0.3-py3-none-any.whl

Download URL bilibili_downloader-0.0.3-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5a0414cce04c941e3766ffd41f5beee41f30d340acf624a3a9f2df06a2254add
BLAKE2b-256 checksum
How to use checksums
a20523ba89a7bcc7b7e774057fb6727e3edc8c01421ae644da526d2bc15138cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 26, 2025.

Transparency log

Security for bilibili-downloader 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for bilibili_downloader-0.0.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 26, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page