@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.
147 lines (146 loc) • 6.94 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const node_path_1 = require("node:path");
const fe_utils_1 = require("@lzwme/fe-utils");
const commander_1 = require("commander");
const console_log_colors_1 = require("console-log-colors");
const i18n_js_1 = require("./lib/i18n.js");
const utils_js_1 = require("./lib/utils.js");
const video_search_js_1 = require("./lib/video-search.js");
const m3u8_batch_download_1 = require("./m3u8-batch-download");
const index_js_1 = require("./video-parser/index.js");
const pkg = (0, fe_utils_1.readJsonFileSync)((0, node_path_1.resolve)(__dirname, '../package.json'));
process.on('unhandledRejection', r => {
console.error(r);
const lang = (0, i18n_js_1.getLang)();
utils_js_1.logger.info(`[${(0, i18n_js_1.t)('common.exit', lang)}][unhandledRejection]`, r.message);
process.exit();
});
process.on('SIGINT', signal => {
const lang = (0, i18n_js_1.getLang)();
utils_js_1.logger.info(`[SIGINT]${(0, i18n_js_1.t)('common.exit', lang)}`, signal);
process.exit();
});
// Initialize language before using t()
const initialLang = 'en'; // getLang();
(0, i18n_js_1.setLanguage)(initialLang);
commander_1.program
.version(pkg.version, '-v, --version')
.description((0, console_log_colors_1.cyanBright)(pkg.description))
.argument('<m3u8Urls...>', (0, i18n_js_1.t)('cli.command.download.description', initialLang))
.option('--silent', (0, i18n_js_1.t)('cli.option.silent', initialLang))
.option('--debug', (0, i18n_js_1.t)('cli.option.debug', initialLang))
.option('-f, --filename <name>', (0, i18n_js_1.t)('cli.option.filename', initialLang))
.option('-n, --thread-num <number>', (0, i18n_js_1.t)('cli.option.threadNum', initialLang))
.option('-F, --force', (0, i18n_js_1.t)('cli.option.force', initialLang))
.option('--no-progress', (0, i18n_js_1.t)('cli.option.noProgress', initialLang))
.option('-p, --play', (0, i18n_js_1.t)('cli.option.play', initialLang))
.option('-C, --cache-dir <dirpath>', (0, i18n_js_1.t)('cli.option.cacheDir', initialLang))
.option('-S, --save-dir <dirpath>', (0, i18n_js_1.t)('cli.option.saveDir', initialLang))
.option('--no-del-cache', (0, i18n_js_1.t)('cli.option.noDelCache', initialLang))
.option('--no-convert', (0, i18n_js_1.t)('cli.option.noConvert', initialLang))
.option('--ffmpeg-path <path>', (0, i18n_js_1.t)('cli.option.ffmpegPath', initialLang))
.option('-H, --headers <headers>', (0, i18n_js_1.t)('cli.option.headers', initialLang))
.option('-T, --type <type>', (0, i18n_js_1.t)('cli.option.type', initialLang))
.option('-I, --ignore-segments <time-segments>', (0, i18n_js_1.t)('cli.option.ignoreSegments', initialLang))
.option('--lang <lang>', (0, i18n_js_1.t)('cli.option.lang', initialLang))
.action(async (urls) => {
const options = getOptions();
utils_js_1.logger.debug(urls, options);
if (options.progress != null)
options.showProgress = options.progress;
delete options.progress;
if (urls.length > 0) {
await (0, m3u8_batch_download_1.m3u8BatchDownload)(urls, options).then(r => process.exit(r ? 0 : 1));
}
else
commander_1.program.help();
});
commander_1.program
.command('server')
.description((0, i18n_js_1.t)('cli.command.server.description', initialLang))
.option('-P, --port <port>', (0, i18n_js_1.t)('cli.option.port', initialLang))
.option('-t, --token <token>', (0, i18n_js_1.t)('cli.option.token', initialLang))
.option('--lang <lang>', (0, i18n_js_1.t)('cli.option.lang', initialLang))
.action((options) => {
const opts = getOptions();
if (opts.debug)
options.debug = true;
if (opts.cacheDir)
options.cacheDir = opts.cacheDir;
utils_js_1.logger.debug('[cli][server]', opts, options);
Promise.resolve().then(() => __importStar(require('./server/download-server.js'))).then(m => {
new m.DLServer(options);
});
});
commander_1.program
.command('search [keyword]')
.alias('s')
.option('-u,--url <api...>', (0, i18n_js_1.t)('cli.option.url', initialLang))
.option('-d, --apidir <dirpath>', (0, i18n_js_1.t)('cli.option.apidir', initialLang))
.option('--lang <lang>', (0, i18n_js_1.t)('cli.option.lang', initialLang))
// .option('-R,--remote-config-url <url>', '自定义远程配置加载地址。默认从主仓库配置读取')
.description((0, i18n_js_1.t)('cli.command.search.description', initialLang))
.action(async (keyword, options) => {
await (0, video_search_js_1.VideoSerachAndDL)(keyword, options, getOptions());
});
commander_1.program
.command('info <url>')
.description('解析视频 URL 并输出详细信息')
.option('--lang <lang>', (0, i18n_js_1.t)('cli.option.lang', initialLang))
.action(async (url) => {
getOptions(); // 处理语言设置
utils_js_1.logger.info('正在解析视频 URL:', url);
const result = await index_js_1.VideoParser.parse(url);
console.log(JSON.stringify(result, null, 2));
process.exit();
});
commander_1.program.parse(process.argv);
function getOptions() {
const options = commander_1.program.opts();
// Set global language if specified
if (options.lang) {
(0, i18n_js_1.setLanguage)((0, i18n_js_1.getLang)(options.lang));
}
if (options.debug) {
utils_js_1.logger.updateOptions({ levelType: 'debug' });
}
else if (options.silent) {
utils_js_1.logger.updateOptions({ levelType: 'silent' });
options.progress = false;
}
return options;
}