UNPKG

@lzwme/m3u8-dl

Version:

Batch download of m3u8 files and convert to mp4

51 lines (50 loc) 2.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tsDownload = tsDownload; const node_crypto_1 = require("node:crypto"); const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); const node_worker_threads_1 = require("node:worker_threads"); const fe_utils_1 = require("@lzwme/fe-utils"); const utils_js_1 = require("./utils.js"); async function tsDownload(info, cryptoInfo, headers) { try { if ((0, node_fs_1.existsSync)(info.tsOut)) return true; const r = await (0, utils_js_1.getRetry)(info.uri, headers); if (r.response.statusCode === 200) { utils_js_1.logger.debug('\n', info); const data = cryptoInfo?.key ? aesDecrypt(r.buffer, cryptoInfo) : r.buffer; (0, fe_utils_1.mkdirp)((0, node_path_1.dirname)(info.tsOut)); await node_fs_1.promises.writeFile(info.tsOut, data); info.tsSize = r.buffer.byteLength; return true; } utils_js_1.logger.warn('[TS-Download][failed]', r.response.statusCode, info.uri); } catch (e) { utils_js_1.logger.error('[TS-Download][error]', e?.message || e || 'unkown'); } return false; } function aesDecrypt(data, cryptoInfo) { try { const iv = cryptoInfo.iv || new Uint8Array(16); const decipher = (0, node_crypto_1.createDecipheriv)(`${cryptoInfo.method}-cbc`.toLocaleLowerCase(), cryptoInfo.key, iv); return Buffer.concat([decipher.update(Buffer.isBuffer(data) ? data : Buffer.from(data)), decipher.final()]); } catch (err) { console.log('aesDecrypt err:', err); throw err; } } if (!node_worker_threads_1.isMainThread && node_worker_threads_1.parentPort) { node_worker_threads_1.parentPort.on('message', (data) => { const startTime = Date.now(); if (data.options.debug) utils_js_1.logger.updateOptions({ levelType: 'debug' }); tsDownload(data.info, data.crypto, data.options?.headers).then(success => { node_worker_threads_1.parentPort.postMessage({ success, info: data.info, timeCost: Date.now() - startTime }); }); }); }