@lzwme/m3u8-dl
Version:
A free, open-source, and powerful m3u8 video batch downloader with multi-threaded downloading, play-while-downloading, WebUI management, video parsing, and more.
87 lines (86 loc) • 3.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VideoParser = void 0;
const node_path_1 = require("node:path");
const node_url_1 = require("node:url");
const file_download_1 = require("../lib/file-download");
const utils_1 = require("../lib/utils");
const base_parser_1 = require("./parsers/base-parser");
const douyin_parser_1 = require("./parsers/douyin-parser");
const pipixia_parser_1 = require("./parsers/pipixia-parser");
const weibo_parser_1 = require("./parsers/weibo-parser");
class VideoParser {
static platforms = {
pipixia: {
class: pipixia_parser_1.PipixiaParser,
domains: ['pipix.com'],
},
douyin: {
class: douyin_parser_1.DouyinParser,
domains: ['douyin.com'],
},
weibo: {
class: weibo_parser_1.WeiboParser,
domains: ['weibo.com'],
},
unknown: {
class: base_parser_1.BaseParser,
domains: ['**'],
},
};
/**
* 解析视频 URL
*/
static async parse(url, headers = {}) {
const info = VideoParser.getPlatform(url);
if (info.errmsg)
return { code: 400, message: info.errmsg || '不支持的视频平台' };
const parserClass = VideoParser.platforms[info.platform].class;
return await parserClass.parse(info.url, headers);
}
static async download(url, options) {
const info = await VideoParser.parse(url);
utils_1.logger.debug('解析视频信息', info);
if (info.code || !info.data?.url)
return { errmsg: info.message || '解析视频信息失败', options };
if (!options.filename && info.data.title) {
options.filename = info.data.title.replaceAll(/[\s\\/:*?"<>|]/g, '_');
}
if (!options.type)
options.type = 'parser';
if (!(0, node_path_1.extname)(options.filename))
options.filename += '.mp4';
options.headers = {
referer: info.data.referer || info.data.url,
...(0, utils_1.formatHeaders)(options.headers),
};
return (0, file_download_1.fileDownload)(info.data.url, options);
}
/**
* 根据 URL 获取平台标识
*/
static getPlatform(url) {
try {
url = /https?:\/\/[^ ]+/.exec(url)?.[0];
const parsedUrl = new node_url_1.URL(url);
const host = parsedUrl.hostname.replace(/^www\./, '');
for (const [platform, config] of Object.entries(VideoParser.platforms)) {
if (config.domains.some(domain => host.includes(domain))) {
return { url, platform };
}
}
return { url, platform: 'unknown' };
}
catch (error) {
// console.error('[parser]解析 URL 失败', url, (error as Error).message);
return { url, platform: 'unknown', errmsg: error.message };
}
}
/**
* 获取所有支持的平台列表
*/
static getSupportedPlatforms() {
return Object.keys(VideoParser.platforms);
}
}
exports.VideoParser = VideoParser;