Skip to content

Commit

Permalink
update version
Browse files Browse the repository at this point in the history
  • Loading branch information
ying_123 committed May 16, 2023
1 parent edccbaa commit 0a76a0e
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 55 deletions.
24 changes: 13 additions & 11 deletions dist/hls2mp4.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
24 changes: 13 additions & 11 deletions dist/hls2mp4.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
24 changes: 13 additions & 11 deletions dist/hls2mp4.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export declare function createFileUrlRegExp(ext: string, flags?: string): RegExp
export declare function parseM3u8File(url: string, customFetch?: (url: string) => Promise<string>): Promise<M3u8Parsed>;
export default class Hls2Mp4 {
private instance;
private ffmpegLoaded;
private maxRetry;
private loadRetryTime;
private onProgress?;
Expand All @@ -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;
Expand Down
24 changes: 13 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
24 changes: 15 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 0a76a0e

Please sign in to comment.