From 0a76a0e91c10591c34148a828b1eea58cce86d23 Mon Sep 17 00:00:00 2001 From: ying_123 Date: Tue, 16 May 2023 17:13:55 +0800 Subject: [PATCH] update version --- dist/hls2mp4.cjs | 24 +++++++++++++----------- dist/hls2mp4.js | 24 +++++++++++++----------- dist/hls2mp4.umd.js | 24 +++++++++++++----------- dist/index.d.ts | 2 +- dist/index.js | 24 +++++++++++++----------- package.json | 2 +- src/index.ts | 24 +++++++++++++++--------- 7 files changed, 69 insertions(+), 55 deletions(-) diff --git a/dist/hls2mp4.cjs b/dist/hls2mp4.cjs index 7c4a9ea..106c66f 100644 --- a/dist/hls2mp4.cjs +++ b/dist/hls2mp4.cjs @@ -935,7 +935,6 @@ function parseM3u8File(url, customFetch) { var Hls2Mp4 = /** @class */ (function () { function Hls2Mp4(_a, onProgress) { var _b = _a.maxRetry, maxRetry = _b === void 0 ? 3 : _b, _c = _a.tsDownloadConcurrency, tsDownloadConcurrency = _c === void 0 ? 10 : _c, options = __rest(_a, ["maxRetry", "tsDownloadConcurrency"]); - this.ffmpegLoaded = false; this.loadRetryTime = 0; this.totalSegments = 0; this.savedSegments = 0; @@ -957,9 +956,15 @@ var Hls2Mp4 = /** @class */ (function () { } return buffer.slice(bufferOffset); }; + Hls2Mp4.prototype.hexToUint8Array = function (hex) { + return new Uint8Array(hex.replace(/^0x/, '').match(/[\da-f]{2}/gi).map(function (hx) { return parseInt(hx, 16); })); + }; Hls2Mp4.prototype.aesDecrypt = function (buffer, keyBuffer, iv) { - if (iv === void 0) { iv = ''; } - var aesCbc = new aesjs.ModeOfOperation.cbc(keyBuffer, iv); + var ivData; + if (iv) { + ivData = iv.startsWith('0x') ? this.hexToUint8Array(iv) : aesjs.utils.utf8.toBytes(iv); + } + var aesCbc = new aesjs.ModeOfOperation.cbc(keyBuffer, ivData); return aesCbc.decrypt(buffer); }; Hls2Mp4.prototype.parseM3u8 = function (url) { @@ -1070,6 +1075,7 @@ var Hls2Mp4 = /** @class */ (function () { } } this.totalSegments = segments.reduce(function (prev, current) { return prev + current.segments.length; }, 0); + this.savedSegments = 0; batch = this.tsDownloadConcurrency; treatedSegments = 0; _c.label = 2; @@ -1219,19 +1225,15 @@ var Hls2Mp4 = /** @class */ (function () { var m3u8, data; return __generator(this, function (_c) { switch (_c.label) { - case 0: - if (!!this.ffmpegLoaded) return [3 /*break*/, 2]; - return [4 /*yield*/, this.loadFFmpeg()]; + case 0: return [4 /*yield*/, this.loadFFmpeg()]; case 1: _c.sent(); - this.ffmpegLoaded = true; - _c.label = 2; - case 2: return [4 /*yield*/, this.downloadM3u8(url)]; - case 3: + return [4 /*yield*/, this.downloadM3u8(url)]; + case 2: m3u8 = _c.sent(); (_a = this.onProgress) === null || _a === void 0 ? void 0 : _a.call(this, exports.TaskType.mergeTs, 0); return [4 /*yield*/, this.instance.run('-i', m3u8, '-c', 'copy', 'temp.mp4', '-loglevel', 'debug')]; - case 4: + case 3: _c.sent(); data = this.instance.FS('readFile', 'temp.mp4'); this.instance.exit(); diff --git a/dist/hls2mp4.js b/dist/hls2mp4.js index 073830c..f6b9bbe 100644 --- a/dist/hls2mp4.js +++ b/dist/hls2mp4.js @@ -932,7 +932,6 @@ var Hls2Mp4 = /** @class */ (function () { function Hls2Mp4(_a, onProgress) { var _b = _a.maxRetry, maxRetry = _b === void 0 ? 3 : _b, _c = _a.tsDownloadConcurrency, tsDownloadConcurrency = _c === void 0 ? 10 : _c, options = __rest(_a, ["maxRetry", "tsDownloadConcurrency"]); - this.ffmpegLoaded = false; this.loadRetryTime = 0; this.totalSegments = 0; this.savedSegments = 0; @@ -954,9 +953,15 @@ } return buffer.slice(bufferOffset); }; + Hls2Mp4.prototype.hexToUint8Array = function (hex) { + return new Uint8Array(hex.replace(/^0x/, '').match(/[\da-f]{2}/gi).map(function (hx) { return parseInt(hx, 16); })); + }; Hls2Mp4.prototype.aesDecrypt = function (buffer, keyBuffer, iv) { - if (iv === void 0) { iv = ''; } - var aesCbc = new aesjs.ModeOfOperation.cbc(keyBuffer, iv); + var ivData; + if (iv) { + ivData = iv.startsWith('0x') ? this.hexToUint8Array(iv) : aesjs.utils.utf8.toBytes(iv); + } + var aesCbc = new aesjs.ModeOfOperation.cbc(keyBuffer, ivData); return aesCbc.decrypt(buffer); }; Hls2Mp4.prototype.parseM3u8 = function (url) { @@ -1067,6 +1072,7 @@ } } this.totalSegments = segments.reduce(function (prev, current) { return prev + current.segments.length; }, 0); + this.savedSegments = 0; batch = this.tsDownloadConcurrency; treatedSegments = 0; _c.label = 2; @@ -1216,19 +1222,15 @@ var m3u8, data; return __generator(this, function (_c) { switch (_c.label) { - case 0: - if (!!this.ffmpegLoaded) return [3 /*break*/, 2]; - return [4 /*yield*/, this.loadFFmpeg()]; + case 0: return [4 /*yield*/, this.loadFFmpeg()]; case 1: _c.sent(); - this.ffmpegLoaded = true; - _c.label = 2; - case 2: return [4 /*yield*/, this.downloadM3u8(url)]; - case 3: + return [4 /*yield*/, this.downloadM3u8(url)]; + case 2: m3u8 = _c.sent(); (_a = this.onProgress) === null || _a === void 0 ? void 0 : _a.call(this, exports.TaskType.mergeTs, 0); return [4 /*yield*/, this.instance.run('-i', m3u8, '-c', 'copy', 'temp.mp4', '-loglevel', 'debug')]; - case 4: + case 3: _c.sent(); data = this.instance.FS('readFile', 'temp.mp4'); this.instance.exit(); diff --git a/dist/hls2mp4.umd.js b/dist/hls2mp4.umd.js index 4b007fa..710a721 100644 --- a/dist/hls2mp4.umd.js +++ b/dist/hls2mp4.umd.js @@ -935,7 +935,6 @@ var Hls2Mp4 = /** @class */ (function () { function Hls2Mp4(_a, onProgress) { var _b = _a.maxRetry, maxRetry = _b === void 0 ? 3 : _b, _c = _a.tsDownloadConcurrency, tsDownloadConcurrency = _c === void 0 ? 10 : _c, options = __rest(_a, ["maxRetry", "tsDownloadConcurrency"]); - this.ffmpegLoaded = false; this.loadRetryTime = 0; this.totalSegments = 0; this.savedSegments = 0; @@ -957,9 +956,15 @@ } return buffer.slice(bufferOffset); }; + Hls2Mp4.prototype.hexToUint8Array = function (hex) { + return new Uint8Array(hex.replace(/^0x/, '').match(/[\da-f]{2}/gi).map(function (hx) { return parseInt(hx, 16); })); + }; Hls2Mp4.prototype.aesDecrypt = function (buffer, keyBuffer, iv) { - if (iv === void 0) { iv = ''; } - var aesCbc = new aesjs.ModeOfOperation.cbc(keyBuffer, iv); + var ivData; + if (iv) { + ivData = iv.startsWith('0x') ? this.hexToUint8Array(iv) : aesjs.utils.utf8.toBytes(iv); + } + var aesCbc = new aesjs.ModeOfOperation.cbc(keyBuffer, ivData); return aesCbc.decrypt(buffer); }; Hls2Mp4.prototype.parseM3u8 = function (url) { @@ -1070,6 +1075,7 @@ } } this.totalSegments = segments.reduce(function (prev, current) { return prev + current.segments.length; }, 0); + this.savedSegments = 0; batch = this.tsDownloadConcurrency; treatedSegments = 0; _c.label = 2; @@ -1219,19 +1225,15 @@ var m3u8, data; return __generator(this, function (_c) { switch (_c.label) { - case 0: - if (!!this.ffmpegLoaded) return [3 /*break*/, 2]; - return [4 /*yield*/, this.loadFFmpeg()]; + case 0: return [4 /*yield*/, this.loadFFmpeg()]; case 1: _c.sent(); - this.ffmpegLoaded = true; - _c.label = 2; - case 2: return [4 /*yield*/, this.downloadM3u8(url)]; - case 3: + return [4 /*yield*/, this.downloadM3u8(url)]; + case 2: m3u8 = _c.sent(); (_a = this.onProgress) === null || _a === void 0 ? void 0 : _a.call(this, exports.TaskType.mergeTs, 0); return [4 /*yield*/, this.instance.run('-i', m3u8, '-c', 'copy', 'temp.mp4', '-loglevel', 'debug')]; - case 4: + case 3: _c.sent(); data = this.instance.FS('readFile', 'temp.mp4'); this.instance.exit(); diff --git a/dist/index.d.ts b/dist/index.d.ts index ba3aaf3..0ad8cae 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -26,7 +26,6 @@ export declare function createFileUrlRegExp(ext: string, flags?: string): RegExp export declare function parseM3u8File(url: string, customFetch?: (url: string) => Promise): Promise; export default class Hls2Mp4 { private instance; - private ffmpegLoaded; private maxRetry; private loadRetryTime; private onProgress?; @@ -36,6 +35,7 @@ export default class Hls2Mp4 { static version: string; constructor({ maxRetry, tsDownloadConcurrency, ...options }: CreateFFmpegOptions & Hls2Mp4Options, onProgress?: ProgressCallback); private transformBuffer; + private hexToUint8Array; private aesDecrypt; private parseM3u8; private downloadFile; diff --git a/dist/index.js b/dist/index.js index cd12bbf..d15d380 100644 --- a/dist/index.js +++ b/dist/index.js @@ -931,7 +931,6 @@ function parseM3u8File(url, customFetch) { var Hls2Mp4 = /** @class */ (function () { function Hls2Mp4(_a, onProgress) { var _b = _a.maxRetry, maxRetry = _b === void 0 ? 3 : _b, _c = _a.tsDownloadConcurrency, tsDownloadConcurrency = _c === void 0 ? 10 : _c, options = __rest(_a, ["maxRetry", "tsDownloadConcurrency"]); - this.ffmpegLoaded = false; this.loadRetryTime = 0; this.totalSegments = 0; this.savedSegments = 0; @@ -953,9 +952,15 @@ var Hls2Mp4 = /** @class */ (function () { } return buffer.slice(bufferOffset); }; + Hls2Mp4.prototype.hexToUint8Array = function (hex) { + return new Uint8Array(hex.replace(/^0x/, '').match(/[\da-f]{2}/gi).map(function (hx) { return parseInt(hx, 16); })); + }; Hls2Mp4.prototype.aesDecrypt = function (buffer, keyBuffer, iv) { - if (iv === void 0) { iv = ''; } - var aesCbc = new aesjs.ModeOfOperation.cbc(keyBuffer, iv); + var ivData; + if (iv) { + ivData = iv.startsWith('0x') ? this.hexToUint8Array(iv) : aesjs.utils.utf8.toBytes(iv); + } + var aesCbc = new aesjs.ModeOfOperation.cbc(keyBuffer, ivData); return aesCbc.decrypt(buffer); }; Hls2Mp4.prototype.parseM3u8 = function (url) { @@ -1066,6 +1071,7 @@ var Hls2Mp4 = /** @class */ (function () { } } this.totalSegments = segments.reduce(function (prev, current) { return prev + current.segments.length; }, 0); + this.savedSegments = 0; batch = this.tsDownloadConcurrency; treatedSegments = 0; _c.label = 2; @@ -1215,19 +1221,15 @@ var Hls2Mp4 = /** @class */ (function () { var m3u8, data; return __generator(this, function (_c) { switch (_c.label) { - case 0: - if (!!this.ffmpegLoaded) return [3 /*break*/, 2]; - return [4 /*yield*/, this.loadFFmpeg()]; + case 0: return [4 /*yield*/, this.loadFFmpeg()]; case 1: _c.sent(); - this.ffmpegLoaded = true; - _c.label = 2; - case 2: return [4 /*yield*/, this.downloadM3u8(url)]; - case 3: + return [4 /*yield*/, this.downloadM3u8(url)]; + case 2: m3u8 = _c.sent(); (_a = this.onProgress) === null || _a === void 0 ? void 0 : _a.call(this, TaskType.mergeTs, 0); return [4 /*yield*/, this.instance.run('-i', m3u8, '-c', 'copy', 'temp.mp4', '-loglevel', 'debug')]; - case 4: + case 3: _c.sent(); data = this.instance.FS('readFile', 'temp.mp4'); this.instance.exit(); diff --git a/package.json b/package.json index 6d50c5b..0afb0f1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hls2mp4", - "version": "1.1.5", + "version": "1.1.6", "description": "a tool for download hls/m3u8 to mp4", "main": "index.js", "types": "index.d.ts", diff --git a/src/index.ts b/src/index.ts index c9e1d73..f26cf74 100644 --- a/src/index.ts +++ b/src/index.ts @@ -83,7 +83,6 @@ export async function parseM3u8File(url: string, customFetch?: (url: string) => export default class Hls2Mp4 { private instance: FFmpeg; - private ffmpegLoaded = false; private maxRetry: number; private loadRetryTime = 0; private onProgress?: ProgressCallback; @@ -114,8 +113,19 @@ export default class Hls2Mp4 { return buffer.slice(bufferOffset) } - private aesDecrypt(buffer: Uint8Array, keyBuffer: Uint8Array, iv = '') { - const aesCbc = new aesjs.ModeOfOperation.cbc(keyBuffer, iv); + private hexToUint8Array(hex: string) { + return new Uint8Array( + hex.replace(/^0x/, '').match( + /[\da-f]{2}/gi + ).map(hx => parseInt(hx, 16))) + } + + private aesDecrypt(buffer: Uint8Array, keyBuffer: Uint8Array, iv?: string) { + let ivData: Uint8Array; + if (iv) { + ivData = iv.startsWith('0x') ? this.hexToUint8Array(iv) : aesjs.utils.utf8.toBytes(iv) + } + const aesCbc = new aesjs.ModeOfOperation.cbc(keyBuffer, ivData); return aesCbc.decrypt(buffer); } @@ -199,13 +209,12 @@ export default class Hls2Mp4 { } this.totalSegments = segments.reduce((prev, current) => prev + current.segments.length, 0); + this.savedSegments = 0; const batch = this.tsDownloadConcurrency; let treatedSegments = 0; for (const group of segments) { - const total = group.segments.length; - let keyBuffer: Uint8Array; if (group.key) { @@ -280,10 +289,7 @@ export default class Hls2Mp4 { } public async download(url: string) { - if (!this.ffmpegLoaded) { - await this.loadFFmpeg(); - this.ffmpegLoaded = true; - } + await this.loadFFmpeg(); const m3u8 = await this.downloadM3u8(url); this.onProgress?.(TaskType.mergeTs, 0); await this.instance.run('-i', m3u8, '-c', 'copy', 'temp.mp4', '-loglevel', 'debug');