UNPKG

@lzwme/m3u8-dl

Version:

Batch download of m3u8 files and convert to mp4

81 lines (80 loc) 2.44 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 = [ '.mp4', '.mkv', '.avi', '.mov', '.wmv', '.ts', '.exe', '.zip', '.rar', '.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', ]; 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'; } 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 }; }