@lzwme/m3u8-dl
Version:
Batch download of m3u8 files and convert to mp4
68 lines (67 loc) • 2.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = exports.getRetry = exports.request = void 0;
exports.isSupportFfmpeg = isSupportFfmpeg;
exports.findFiles = findFiles;
exports.getLocation = getLocation;
exports.formatHeaders = formatHeaders;
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const fe_utils_1 = require("@lzwme/fe-utils");
exports.request = new fe_utils_1.Request({
headers: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' },
reqOptions: { rejectUnauthorized: false },
});
// process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const getRetry = (url, headers, retries = 3) => (0, fe_utils_1.retry)(() => exports.request.get(url, null, formatHeaders(headers), { rejectUnauthorized: false }), 1000, retries, r => {
if (r.response.statusCode !== 200) {
console.log();
exports.logger.warn(`[retry][${url}][${r.response.statusCode}]`, r.response.statusMessage || r.data);
// throw Error(`[${r.response.statusCode}]${r.response.statusMessage || r.data}`);
}
return r.response.statusCode === 200;
});
exports.getRetry = getRetry;
exports.logger = fe_utils_1.NLogger.getLogger('[M3U8-DL]', { color: fe_utils_1.color });
let _isSupportFfmpeg = null;
function isSupportFfmpeg() {
if (null == _isSupportFfmpeg)
_isSupportFfmpeg = (0, fe_utils_1.execSync)('ffmpeg -version').stderr === '';
return _isSupportFfmpeg;
}
function findFiles(apidir, validate) {
const files = [];
if (apidir && (0, node_fs_1.existsSync)(apidir)) {
const stat = (0, node_fs_1.statSync)(apidir);
if (!validate || validate(apidir, stat)) {
if (stat.isFile()) {
files.push((0, node_path_1.resolve)(apidir));
}
else if (stat.isDirectory()) {
for (const filename of (0, node_fs_1.readdirSync)(apidir)) {
files.push(...findFiles((0, node_path_1.resolve)(apidir, filename)));
}
}
}
}
return files;
}
/** 获取重定向后的 URL */
async function getLocation(url, method = 'HEAD') {
const { res } = await exports.request.req(url, null, { method, headers: { 'content-type': 'text/html' } }, false);
const rurl = res.headers.location || res.headers['x-redirect'] || res.headers['x-location'];
if (typeof rurl === 'string' && rurl !== url)
return getLocation(rurl, method);
return url;
}
/**
* 将传入的 headers 转换为统一的小写键对象格式
* 如果 headers 是字符串,会先将其解析为对象;如果 headers 为空,则返回空对象。
*/
function formatHeaders(headers) {
if (!headers)
return {};
if (typeof headers === 'string')
headers = Object.fromEntries(headers.split('\n').map(line => line.split(':').map(d => d.trim())));
return (0, fe_utils_1.toLowcaseKeyObject)(headers);
}