BBDown:高效便捷的哔哩哔哩视频下载工具
项目描述
BBDown是一个免费且便捷高效的哔哩哔哩下载/解析软件,支持命令行操作和HTTP API服务。该项目采用C#开发,提供完整的B站视频解析和下载解决方案,支持多种视频格式、多线程下载、弹幕下载等丰富功能。
功能特性
- 多平台解析:支持Web端、TV端、APP端和国际版API解析模式
- 多种下载方式:支持普通下载、aria2c下载和多线程下载
- 格式支持:支持HEVC、AV1、AVC等多种视频编码格式
- 画质选择:提供丰富的画质优先级设置,支持8K、杜比视界等高清格式
- 弹幕处理:支持XML和ASS格式的弹幕下载
- API服务:内置HTTP服务器,提供完整的RESTful API接口
- 批量下载:支持收藏夹、合集、系列列表等批量下载功能
- 交互式选择:提供交互式清晰度选择界面
安装指南
通过Dotnet Tool安装
如果你本地有dotnet环境,使用如下命令即可安装使用:
bash
dotnet tool install --global BBDown
更新BBDown
bash
dotnet tool update --global BBDown
系统要求
- .NET运行时环境
- 混流需要外部程序:ffmpeg或mp4box
- 杜比视界需要ffmpeg 5.0以上或新版mp4box
下载地址
- Release版本:GitHub Releases
- 自动构建测试版本:GitHub Actions
使用说明
基础使用
bash
BBDown <url> [options]
常用参数示例
bash
# 仅显示视频信息而不下载
BBDown "BV1qt4y1X7TW" --only-show-info
# 使用TV端API解析
BBDown "BV1qt4y1X7TW" --use-tv-api
# 设置编码优先级
BBDown "BV1qt4y1X7TW" --encoding-priority "hevc,av1,avc"
# 多线程下载
BBDown "BV1qt4y1X7TW" --multi-thread
# 使用aria2c下载
BBDown "BV1qt4y1X7TW" --use-aria2c
API服务器模式
启动服务器模式后,BBDown会在本地启动HTTP服务,提供以下API接口:
获取任务列表
bash
curl http://localhost:58682/get-tasks/
添加下载任务
bash
curl -X POST -H 'Content-Type: application/json' -d '{"Url": "BV1qt4y1X7TW"}' http://localhost:58682/add-task
下载到指定目录
bash
curl -X POST -H 'Content-Type: application/json' -d '{"Url": "BV1qt4y1X7TW", "FilePattern": "/Downloads/<videoTitle>[<dfn>]"}' http://localhost:58682/add-task
核心代码
下载任务管理
csharp
public class DownloadTask
{
public string Aid { get; set; } = default!;
public string Url { get; set; } = default!;
public long TaskCreateTime { get; set; }
public string? Title { get; set; }
public string? Pic { get; set; }
public long? VideoPubTime { get; set; }
public long? TaskFinishTime { get; set; }
public double Progress { get; set; }
public double DownloadSpeed { get; set; }
public double TotalDownloadedBytes { get; set; }
public bool IsSuccessful { get; set; }
}
public class DownloadTaskCollection
{
public List<DownloadTask> Running { get; set; } = new();
public List<DownloadTask> Finished { get; set; } = new();
public DownloadTaskCollection(List<DownloadTask> running, List<DownloadTask> finished)
{
Running = running;
Finished = finished;
}
}
多线程下载实现
csharp
public static class BBDownDownloadUtil
{
public class DownloadConfig
{
public bool UseAria2c { get; set; } = false;
public string Aria2cArgs { get; set; } = string.Empty;
public bool ForceHttp { get; set; } = false;
public bool MultiThread { get; set; } = false;
public DownloadTask? RelatedTask { get; set; } = null;
}
private static async Task RangeDownloadToTmpAsync(int id, string url, string tmpName,
long fromPosition, long? toPosition, Action<int, long, long> onProgress,
bool failOnRangeNotSupported = false)
{
// 实现范围下载和进度回调
DateTimeOffset? lastTime = File.Exists(tmpName) ?
new FileInfo(tmpName).LastWriteTimeUtc : null;
using var fileStream = new FileStream(tmpName, FileMode.OpenOrCreate);
fileStream.Seek(0, SeekOrigin.End);
// 下载逻辑实现...
}
}
视频解析器工厂
csharp
public static class FetcherFactory
{
public static IFetcher CreateFetcher(string aidOri, bool useIntlApi)
{
IFetcher fetcher = new NormalInfoFetcher();
if (aidOri.StartsWith("cheese"))
{
fetcher = new CheeseInfoFetcher();
}
else if (aidOri.StartsWith("ep"))
{
fetcher = useIntlApi ?
new IntlBangumiInfoFetcher() : new BangumiInfoFetcher();
}
else if (aidOri.StartsWith("mid"))
{
fetcher = new SpaceVideoFetcher();
}
// 更多解析器类型...
return fetcher;
}
}
进度条显示
csharp
public class ProgressBar : IDisposable, IProgress<double>
{
private const int blockCount = 40;
private readonly TimeSpan animationInterval = TimeSpan.FromSeconds(1.0 / 8);
private const string animation = @"|/-\";
public void Report(double value)
{
// 确保进度值在0-1范围内
value = Math.Max(0, Math.Min(1, value));
Interlocked.Exchange(ref currentProgress, value);
}
public void Report(double value, long bytesCount)
{
value = Math.Max(0, Math.Min(1, value));
Interlocked.Exchange(ref currentProgress, value);
Interlocked.Exchange(ref downloadedBytes, bytesCount);
}
// 进度显示和速度计算实现...
}
配置文件解析
csharp
internal static class BBDownConfigParser
{
public static void HandleConfig(List<string> newArgsList, RootCommand rootCommand)
{
try
{
var configPath = newArgsList.Contains("--config-file")
? newArgsList.ElementAt(newArgsList.IndexOf("--config-file") + 1)
: Path.Combine(Program.APP_DIR, "BBDown.config");
if (File.Exists(configPath))
{
// 配置文件读取和解析逻辑
var configArgs = File
.ReadAllLines(configPath)
.Where(s => !string.IsNullOrEmpty(s) && !s.StartsWith('#'))
.SelectMany(s => ParseConfigLine(s));
// 命令行参数优先级 > 配置文件优先级
}
}
catch (Exception)
{
LogError("配置文件读取异常,忽略");
}
}
}
BBDown项目提供了完整的哔哩哔哩视频下载解决方案,从视频解析到下载管理,再到进度跟踪和文件混流,每个环节都经过精心设计和实现,确保了软件的稳定性和易用性。