UNPKG

@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.

116 lines (115 loc) 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatOptions = formatOptions; const node_os_1 = require("node:os"); const node_path_1 = require("node:path"); const fe_utils_1 = require("@lzwme/fe-utils"); const video_parser_1 = require("../video-parser"); const utils_1 = require("./utils"); const fileSupportExtList = [ // video '.mp4', '.mkv', '.avi', '.mov', '.wmv', '.ts', // audio '.mp3', '.wav', '.ogg', '.m4a', '.aac', '.flac', '.ape', // document '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.pdf', '.mobi', '.epub', // others '.exe', '.msi', '.zip', '.rar', '.7z', '.tar.gz', '.tar.bz2', '.tar.xz', '.iso', '.dmg', '.pkg', '.deb', '.rpm', ]; async function formatOptions(url, opts) { const options = { delCache: !opts.debug, saveDir: process.cwd(), showProgress: true, ...opts, }; let ext = options.filename ? (0, node_path_1.extname)(options.filename) : ''; if (!options.type) { if (video_parser_1.VideoParser.getPlatform(url).platform !== 'unknown') { options.type = 'parser'; if (!opts.filename) { const info = await video_parser_1.VideoParser.parse(url); if (info.code === 0 && info.data?.title) { options.filename = info.data.title .split('\n')[0] // 替换全部的非中英文、数字、下划线为下划线 .replace(/[^\u4e00-\u9fa5a-zA-Z0-9]+/g, '_') .trim() .replace(/_+/g, '_') .slice(0, 100); } } } else { options.type = 'm3u8'; if (!url.includes('.m3u8')) { const e = fileSupportExtList.find(d => url.includes(d)); if (e) { options.type = 'file'; ext = e; } } } } let [u, n] = url.split(/[|$]+/); if (n?.startsWith('http')) [u, n] = [n, u]; url = u; if (n) { if (!options.filename) options.filename = n; else options.filename = `${options.filename.replace(/\.(ts|mp4)$/, '')}-${n}${ext}`; } const urlMd5 = (0, fe_utils_1.md5)(url, false); if (!options.filename) { if (ext && url.includes(ext)) options.filename = (0, node_path_1.basename)(url.split(ext)[0]) + ext; else options.filename = urlMd5 + ext; } if (!(0, node_path_1.extname)(options.filename) && options.type !== 'file') options.filename += ext || '.mp4'; if (!options.cacheDir) options.cacheDir = 'cache'; if (options.headers) options.headers = (0, utils_1.formatHeaders)(options.headers); if (!options.threadNum || +options.threadNum <= 0) options.threadNum = Math.min((0, node_os_1.cpus)().length * 2, 8); if (options.debug) { utils_1.logger.updateOptions({ levelType: 'debug' }); utils_1.logger.debug('[m3u8-DL]options', options, url); } return { url, options, urlMd5 }; }