Skip to content

Commit

Permalink
update new version
Browse files Browse the repository at this point in the history
  • Loading branch information
icefee committed Sep 3, 2023
1 parent a808947 commit 75d2376
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 220 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ yarn add hls2mp4
import Hls2Mp4 from "hls2mp4";

const hls2mp4 = new Hls2Mp4({
log: true
/**
* @type {number}
* max retry times while request data failed
*/
maxRetry = 3,
/**
* @type {number}
* the concurrency for download ts
*/
tsDownloadConcurrency = 10
}, (type, progress) => {
// type = 0 => load FFmpeg
// type = 1 => parse m3u8
Expand Down
11 changes: 10 additions & 1 deletion dist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ yarn add hls2mp4
import Hls2Mp4 from "hls2mp4";

const hls2mp4 = new Hls2Mp4({
log: true
/**
* @type {number}
* max retry times while request data failed
*/
maxRetry = 3,
/**
* @type {number}
* the concurrency for download ts
*/
tsDownloadConcurrency = 10
}, (type, progress) => {
// type = 0 => load FFmpeg
// type = 1 => parse m3u8
Expand Down
65 changes: 33 additions & 32 deletions dist/hls2mp4.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var FFmpeg = require('@ffmpeg/ffmpeg');
var ffmpeg = require('@ffmpeg/ffmpeg');
var util = require('@ffmpeg/util');

/******************************************************************************
Copyright (c) Microsoft Corporation.
Expand All @@ -19,18 +20,6 @@ PERFORMANCE OF THIS SOFTWARE.
/* global Reflect, Promise */


function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}

function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand Down Expand Up @@ -884,7 +873,6 @@ var aesJs = {exports: {}};
var aesJsExports = aesJs.exports;
var aesjs = /*@__PURE__*/getDefaultExportFromCjs(aesJsExports);

var createFFmpeg = FFmpeg.createFFmpeg, fetchFile = FFmpeg.fetchFile;
var TaskType;
(function (TaskType) {
TaskType[TaskType["loadFFmeg"] = 0] = "loadFFmeg";
Expand All @@ -903,11 +891,11 @@ function parseUrl(url, path) {
}
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"]);
var _b = _a.maxRetry, maxRetry = _b === void 0 ? 3 : _b, _c = _a.tsDownloadConcurrency, tsDownloadConcurrency = _c === void 0 ? 10 : _c;
this.loadRetryTime = 0;
this.totalSegments = 0;
this.savedSegments = 0;
this.instance = createFFmpeg(options);
this.ffmpeg = new ffmpeg.FFmpeg();
this.maxRetry = maxRetry;
this.onProgress = onProgress;
this.tsDownloadConcurrency = tsDownloadConcurrency;
Expand Down Expand Up @@ -952,7 +940,7 @@ var Hls2Mp4 = /** @class */ (function () {
case 1:
playList = _a.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, fetchFile(url).then(function (data) { return aesjs.utils.utf8.fromBytes(data); })];
case 2: return [4 /*yield*/, util.fetchFile(url).then(function (data) { return aesjs.utils.utf8.fromBytes(data); })];
case 3:
playList = _a.sent();
_a.label = 4;
Expand Down Expand Up @@ -996,7 +984,7 @@ var Hls2Mp4 = /** @class */ (function () {
var _b, done, data, fileName;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, this.loopLoadFile(function () { return fetchFile(url); })];
case 0: return [4 /*yield*/, this.loopLoadFile(function () { return util.fetchFile(url); })];
case 1:
_b = _c.sent(), done = _b.done, data = _b.data;
if (done) {
Expand All @@ -1023,7 +1011,7 @@ var Hls2Mp4 = /** @class */ (function () {
case 1:
tsData = _c.sent();
buffer = key ? this.aesDecrypt(tsData, key, iv) : this.transformBuffer(tsData);
this.instance.FS('writeFile', name, buffer);
this.ffmpeg.writeFile(name, buffer);
this.savedSegments += 1;
(_b = this.onProgress) === null || _b === void 0 ? void 0 : _b.call(this, TaskType.downloadTs, this.savedSegments / this.totalSegments);
return [2 /*return*/, {
Expand Down Expand Up @@ -1163,7 +1151,7 @@ var Hls2Mp4 = /** @class */ (function () {
case 14:
content = content.replace(keyTagMatchRegExp, '');
m3u8 = 'temp.m3u8';
this.instance.FS('writeFile', m3u8, content);
this.ffmpeg.writeFile(m3u8, content);
return [2 /*return*/, m3u8];
}
});
Expand Down Expand Up @@ -1202,20 +1190,31 @@ var Hls2Mp4 = /** @class */ (function () {
Hls2Mp4.prototype.loadFFmpeg = function () {
var _a, _b;
return __awaiter(this, void 0, void 0, function () {
var done;
var _this = this;
var baseUrl, coreURL, wasmURL, loaded;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
(_a = this.onProgress) === null || _a === void 0 ? void 0 : _a.call(this, TaskType.loadFFmeg, 0);
return [4 /*yield*/, this.loopLoadFile(function () { return _this.instance.load(); })];
baseUrl = 'https://unpkg.com/@ffmpeg/[email protected]/dist/umd';
return [4 /*yield*/, util.toBlobURL("".concat(baseUrl, "/ffmpeg-core.js"), 'text/javascript')];
case 1:
done = (_c.sent()).done;
if (done) {
(_b = this.onProgress) === null || _b === void 0 ? void 0 : _b.call(this, TaskType.loadFFmeg, done ? 1 : -1);
coreURL = _c.sent();
return [4 /*yield*/, util.toBlobURL("".concat(baseUrl, "/ffmpeg-core.wasm"), 'application/wasm')
// workerURL = workerURL ?? await toBlobURL(`${baseUrl}/ffmpeg-core.worker.js`, 'text/javascript')
];
case 2:
wasmURL = _c.sent();
return [4 /*yield*/, this.ffmpeg.load({
coreURL: coreURL,
wasmURL: wasmURL
})];
case 3:
loaded = _c.sent();
if (loaded) {
(_b = this.onProgress) === null || _b === void 0 ? void 0 : _b.call(this, TaskType.loadFFmeg, 1);
}
else {
throw new Error('FFmpeg load failed');
return [2 /*return*/, this.loadFFmpeg()];
}
return [2 /*return*/];
}
Expand All @@ -1235,13 +1234,15 @@ var Hls2Mp4 = /** @class */ (function () {
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')];
return [4 /*yield*/, this.ffmpeg.exec(['-i', m3u8, '-c', 'copy', 'temp.mp4', '-loglevel', 'debug'])];
case 3:
_c.sent();
data = this.instance.FS('readFile', 'temp.mp4');
this.instance.exit();
return [4 /*yield*/, this.ffmpeg.readFile('temp.mp4')];
case 4:
data = _c.sent();
this.ffmpeg.terminate();
(_b = this.onProgress) === null || _b === void 0 ? void 0 : _b.call(this, TaskType.mergeTs, 1);
return [2 /*return*/, data.buffer];
return [2 /*return*/, data];
}
});
});
Expand All @@ -1254,7 +1255,7 @@ var Hls2Mp4 = /** @class */ (function () {
anchor.click();
setTimeout(function () { return URL.revokeObjectURL(objectUrl); }, 100);
};
Hls2Mp4.version = '1.1.9';
Hls2Mp4.version = '1.2.0';
Hls2Mp4.TaskType = TaskType;
return Hls2Mp4;
}());
Expand Down
66 changes: 33 additions & 33 deletions dist/hls2mp4.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Hls2Mp4 = (function (FFmpeg) {
var Hls2Mp4 = (function (ffmpeg, util) {
'use strict';

/******************************************************************************
Expand All @@ -18,18 +18,6 @@ var Hls2Mp4 = (function (FFmpeg) {
/* global Reflect, Promise */


function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}

function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand Down Expand Up @@ -883,7 +871,6 @@ var Hls2Mp4 = (function (FFmpeg) {
var aesJsExports = aesJs.exports;
var aesjs = /*@__PURE__*/getDefaultExportFromCjs(aesJsExports);

var createFFmpeg = FFmpeg.createFFmpeg, fetchFile = FFmpeg.fetchFile;
var TaskType;
(function (TaskType) {
TaskType[TaskType["loadFFmeg"] = 0] = "loadFFmeg";
Expand All @@ -902,11 +889,11 @@ var Hls2Mp4 = (function (FFmpeg) {
}
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"]);
var _b = _a.maxRetry, maxRetry = _b === void 0 ? 3 : _b, _c = _a.tsDownloadConcurrency, tsDownloadConcurrency = _c === void 0 ? 10 : _c;
this.loadRetryTime = 0;
this.totalSegments = 0;
this.savedSegments = 0;
this.instance = createFFmpeg(options);
this.ffmpeg = new ffmpeg.FFmpeg();
this.maxRetry = maxRetry;
this.onProgress = onProgress;
this.tsDownloadConcurrency = tsDownloadConcurrency;
Expand Down Expand Up @@ -951,7 +938,7 @@ var Hls2Mp4 = (function (FFmpeg) {
case 1:
playList = _a.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, fetchFile(url).then(function (data) { return aesjs.utils.utf8.fromBytes(data); })];
case 2: return [4 /*yield*/, util.fetchFile(url).then(function (data) { return aesjs.utils.utf8.fromBytes(data); })];
case 3:
playList = _a.sent();
_a.label = 4;
Expand Down Expand Up @@ -995,7 +982,7 @@ var Hls2Mp4 = (function (FFmpeg) {
var _b, done, data, fileName;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, this.loopLoadFile(function () { return fetchFile(url); })];
case 0: return [4 /*yield*/, this.loopLoadFile(function () { return util.fetchFile(url); })];
case 1:
_b = _c.sent(), done = _b.done, data = _b.data;
if (done) {
Expand All @@ -1022,7 +1009,7 @@ var Hls2Mp4 = (function (FFmpeg) {
case 1:
tsData = _c.sent();
buffer = key ? this.aesDecrypt(tsData, key, iv) : this.transformBuffer(tsData);
this.instance.FS('writeFile', name, buffer);
this.ffmpeg.writeFile(name, buffer);
this.savedSegments += 1;
(_b = this.onProgress) === null || _b === void 0 ? void 0 : _b.call(this, TaskType.downloadTs, this.savedSegments / this.totalSegments);
return [2 /*return*/, {
Expand Down Expand Up @@ -1162,7 +1149,7 @@ var Hls2Mp4 = (function (FFmpeg) {
case 14:
content = content.replace(keyTagMatchRegExp, '');
m3u8 = 'temp.m3u8';
this.instance.FS('writeFile', m3u8, content);
this.ffmpeg.writeFile(m3u8, content);
return [2 /*return*/, m3u8];
}
});
Expand Down Expand Up @@ -1201,20 +1188,31 @@ var Hls2Mp4 = (function (FFmpeg) {
Hls2Mp4.prototype.loadFFmpeg = function () {
var _a, _b;
return __awaiter(this, void 0, void 0, function () {
var done;
var _this = this;
var baseUrl, coreURL, wasmURL, loaded;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
(_a = this.onProgress) === null || _a === void 0 ? void 0 : _a.call(this, TaskType.loadFFmeg, 0);
return [4 /*yield*/, this.loopLoadFile(function () { return _this.instance.load(); })];
baseUrl = 'https://unpkg.com/@ffmpeg/[email protected]/dist/umd';
return [4 /*yield*/, util.toBlobURL("".concat(baseUrl, "/ffmpeg-core.js"), 'text/javascript')];
case 1:
done = (_c.sent()).done;
if (done) {
(_b = this.onProgress) === null || _b === void 0 ? void 0 : _b.call(this, TaskType.loadFFmeg, done ? 1 : -1);
coreURL = _c.sent();
return [4 /*yield*/, util.toBlobURL("".concat(baseUrl, "/ffmpeg-core.wasm"), 'application/wasm')
// workerURL = workerURL ?? await toBlobURL(`${baseUrl}/ffmpeg-core.worker.js`, 'text/javascript')
];
case 2:
wasmURL = _c.sent();
return [4 /*yield*/, this.ffmpeg.load({
coreURL: coreURL,
wasmURL: wasmURL
})];
case 3:
loaded = _c.sent();
if (loaded) {
(_b = this.onProgress) === null || _b === void 0 ? void 0 : _b.call(this, TaskType.loadFFmeg, 1);
}
else {
throw new Error('FFmpeg load failed');
return [2 /*return*/, this.loadFFmpeg()];
}
return [2 /*return*/];
}
Expand All @@ -1234,13 +1232,15 @@ var Hls2Mp4 = (function (FFmpeg) {
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')];
return [4 /*yield*/, this.ffmpeg.exec(['-i', m3u8, '-c', 'copy', 'temp.mp4', '-loglevel', 'debug'])];
case 3:
_c.sent();
data = this.instance.FS('readFile', 'temp.mp4');
this.instance.exit();
return [4 /*yield*/, this.ffmpeg.readFile('temp.mp4')];
case 4:
data = _c.sent();
this.ffmpeg.terminate();
(_b = this.onProgress) === null || _b === void 0 ? void 0 : _b.call(this, TaskType.mergeTs, 1);
return [2 /*return*/, data.buffer];
return [2 /*return*/, data];
}
});
});
Expand All @@ -1253,11 +1253,11 @@ var Hls2Mp4 = (function (FFmpeg) {
anchor.click();
setTimeout(function () { return URL.revokeObjectURL(objectUrl); }, 100);
};
Hls2Mp4.version = '1.1.9';
Hls2Mp4.version = '1.2.0';
Hls2Mp4.TaskType = TaskType;
return Hls2Mp4;
}());

return Hls2Mp4;

})(FFmpeg);
})(FFmpeg, util);
Loading

0 comments on commit 75d2376

Please sign in to comment.