From ad4978268dcf6e9c60fd6f0da6ac59161b091339 Mon Sep 17 00:00:00 2001 From: ying_123 Date: Thu, 25 Jan 2024 09:32:11 +0800 Subject: [PATCH] fix ExpReg compatibility on ios --- dist/hls2mp4.cjs | 22 +++++++++++++--------- dist/hls2mp4.js | 22 +++++++++++++--------- dist/hls2mp4.umd.js | 22 +++++++++++++--------- dist/index.js | 22 +++++++++++++--------- dist/package.json | 2 +- package.json | 2 +- src/index.ts | 20 ++++++++++++-------- 7 files changed, 66 insertions(+), 46 deletions(-) diff --git a/dist/hls2mp4.cjs b/dist/hls2mp4.cjs index 15ce9e3..da4a2c0 100644 --- a/dist/hls2mp4.cjs +++ b/dist/hls2mp4.cjs @@ -1058,7 +1058,7 @@ var Hls2Mp4 = /** @class */ (function () { Hls2Mp4.prototype.downloadM3u8 = function (url) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function () { - var m3u8Parsed, _d, content, parsedUrl, keyTagMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8; + var m3u8Parsed, _d, content, parsedUrl, keyTagMatchRegExp, extMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, segment, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8; var e_1, _e; return __generator(this, function (_f) { switch (_f.label) { @@ -1067,7 +1067,8 @@ var Hls2Mp4 = /** @class */ (function () { m3u8Parsed = _f.sent(); _d = m3u8Parsed, content = _d.content, parsedUrl = _d.url; keyTagMatchRegExp = new RegExp('#EXT-X-KEY:METHOD=(AES-128|NONE)(,URI="[^"]+"(,IV=\\w+)?)?', 'gi'); - matchReg = new RegExp(keyTagMatchRegExp.source + '|(?<=#EXTINF:\\d+(\\.\\d+)?,\\n).+', 'gim'); + extMatchRegExp = new RegExp('#EXTINF:\\d+(\\.\\d+)?,\\n'); + matchReg = new RegExp(keyTagMatchRegExp.source + '|' + extMatchRegExp.source + '.+', 'gim'); matches = content.match(matchReg); if (!matches) { throw new Error('Invalid m3u8 file, no ts file found'); @@ -1084,13 +1085,16 @@ var Hls2Mp4 = /** @class */ (function () { segments: [] }); } - else if (i === 0) { - segments.push({ - segments: [matched] - }); - } else { - segments[segments.length - 1].segments.push(matched); + segment = matched.replace(extMatchRegExp, ''); + if (i === 0) { + segments.push({ + segments: [segment] + }); + } + else { + segments[segments.length - 1].segments.push(segment); + } } } this.totalSegments = segments.reduce(function (prev, current) { return prev + current.segments.length; }, 0); @@ -1291,7 +1295,7 @@ var Hls2Mp4 = /** @class */ (function () { anchor.click(); setTimeout(function () { return URL.revokeObjectURL(objectUrl); }, 100); }; - Hls2Mp4.version = '1.2.5'; + Hls2Mp4.version = '1.2.7'; Hls2Mp4.TaskType = exports.TaskType; return Hls2Mp4; }()); diff --git a/dist/hls2mp4.js b/dist/hls2mp4.js index f0a3263..4fb0020 100644 --- a/dist/hls2mp4.js +++ b/dist/hls2mp4.js @@ -1054,7 +1054,7 @@ var Hls2Mp4 = (function (exports, ffmpeg, util) { Hls2Mp4.prototype.downloadM3u8 = function (url) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function () { - var m3u8Parsed, _d, content, parsedUrl, keyTagMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8; + var m3u8Parsed, _d, content, parsedUrl, keyTagMatchRegExp, extMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, segment, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8; var e_1, _e; return __generator(this, function (_f) { switch (_f.label) { @@ -1063,7 +1063,8 @@ var Hls2Mp4 = (function (exports, ffmpeg, util) { m3u8Parsed = _f.sent(); _d = m3u8Parsed, content = _d.content, parsedUrl = _d.url; keyTagMatchRegExp = new RegExp('#EXT-X-KEY:METHOD=(AES-128|NONE)(,URI="[^"]+"(,IV=\\w+)?)?', 'gi'); - matchReg = new RegExp(keyTagMatchRegExp.source + '|(?<=#EXTINF:\\d+(\\.\\d+)?,\\n).+', 'gim'); + extMatchRegExp = new RegExp('#EXTINF:\\d+(\\.\\d+)?,\\n'); + matchReg = new RegExp(keyTagMatchRegExp.source + '|' + extMatchRegExp.source + '.+', 'gim'); matches = content.match(matchReg); if (!matches) { throw new Error('Invalid m3u8 file, no ts file found'); @@ -1080,13 +1081,16 @@ var Hls2Mp4 = (function (exports, ffmpeg, util) { segments: [] }); } - else if (i === 0) { - segments.push({ - segments: [matched] - }); - } else { - segments[segments.length - 1].segments.push(matched); + segment = matched.replace(extMatchRegExp, ''); + if (i === 0) { + segments.push({ + segments: [segment] + }); + } + else { + segments[segments.length - 1].segments.push(segment); + } } } this.totalSegments = segments.reduce(function (prev, current) { return prev + current.segments.length; }, 0); @@ -1287,7 +1291,7 @@ var Hls2Mp4 = (function (exports, ffmpeg, util) { anchor.click(); setTimeout(function () { return URL.revokeObjectURL(objectUrl); }, 100); }; - Hls2Mp4.version = '1.2.5'; + Hls2Mp4.version = '1.2.7'; Hls2Mp4.TaskType = exports.TaskType; return Hls2Mp4; }()); diff --git a/dist/hls2mp4.umd.js b/dist/hls2mp4.umd.js index c757040..0f4bbbe 100644 --- a/dist/hls2mp4.umd.js +++ b/dist/hls2mp4.umd.js @@ -1057,7 +1057,7 @@ Hls2Mp4.prototype.downloadM3u8 = function (url) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function () { - var m3u8Parsed, _d, content, parsedUrl, keyTagMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8; + var m3u8Parsed, _d, content, parsedUrl, keyTagMatchRegExp, extMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, segment, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8; var e_1, _e; return __generator(this, function (_f) { switch (_f.label) { @@ -1066,7 +1066,8 @@ m3u8Parsed = _f.sent(); _d = m3u8Parsed, content = _d.content, parsedUrl = _d.url; keyTagMatchRegExp = new RegExp('#EXT-X-KEY:METHOD=(AES-128|NONE)(,URI="[^"]+"(,IV=\\w+)?)?', 'gi'); - matchReg = new RegExp(keyTagMatchRegExp.source + '|(?<=#EXTINF:\\d+(\\.\\d+)?,\\n).+', 'gim'); + extMatchRegExp = new RegExp('#EXTINF:\\d+(\\.\\d+)?,\\n'); + matchReg = new RegExp(keyTagMatchRegExp.source + '|' + extMatchRegExp.source + '.+', 'gim'); matches = content.match(matchReg); if (!matches) { throw new Error('Invalid m3u8 file, no ts file found'); @@ -1083,13 +1084,16 @@ segments: [] }); } - else if (i === 0) { - segments.push({ - segments: [matched] - }); - } else { - segments[segments.length - 1].segments.push(matched); + segment = matched.replace(extMatchRegExp, ''); + if (i === 0) { + segments.push({ + segments: [segment] + }); + } + else { + segments[segments.length - 1].segments.push(segment); + } } } this.totalSegments = segments.reduce(function (prev, current) { return prev + current.segments.length; }, 0); @@ -1290,7 +1294,7 @@ anchor.click(); setTimeout(function () { return URL.revokeObjectURL(objectUrl); }, 100); }; - Hls2Mp4.version = '1.2.5'; + Hls2Mp4.version = '1.2.7'; Hls2Mp4.TaskType = exports.TaskType; return Hls2Mp4; }()); diff --git a/dist/index.js b/dist/index.js index 44dd21f..9a2930d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1054,7 +1054,7 @@ var Hls2Mp4 = /** @class */ (function () { Hls2Mp4.prototype.downloadM3u8 = function (url) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function () { - var m3u8Parsed, _d, content, parsedUrl, keyTagMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8; + var m3u8Parsed, _d, content, parsedUrl, keyTagMatchRegExp, extMatchRegExp, matchReg, matches, segments, i, matched, matchedKey, matchedIV, segment, batch, treatedSegments, segments_1, segments_1_1, group, total, keyBuffer, keyUrl, _loop_1, this_1, i, e_1_1, m3u8; var e_1, _e; return __generator(this, function (_f) { switch (_f.label) { @@ -1063,7 +1063,8 @@ var Hls2Mp4 = /** @class */ (function () { m3u8Parsed = _f.sent(); _d = m3u8Parsed, content = _d.content, parsedUrl = _d.url; keyTagMatchRegExp = new RegExp('#EXT-X-KEY:METHOD=(AES-128|NONE)(,URI="[^"]+"(,IV=\\w+)?)?', 'gi'); - matchReg = new RegExp(keyTagMatchRegExp.source + '|(?<=#EXTINF:\\d+(\\.\\d+)?,\\n).+', 'gim'); + extMatchRegExp = new RegExp('#EXTINF:\\d+(\\.\\d+)?,\\n'); + matchReg = new RegExp(keyTagMatchRegExp.source + '|' + extMatchRegExp.source + '.+', 'gim'); matches = content.match(matchReg); if (!matches) { throw new Error('Invalid m3u8 file, no ts file found'); @@ -1080,13 +1081,16 @@ var Hls2Mp4 = /** @class */ (function () { segments: [] }); } - else if (i === 0) { - segments.push({ - segments: [matched] - }); - } else { - segments[segments.length - 1].segments.push(matched); + segment = matched.replace(extMatchRegExp, ''); + if (i === 0) { + segments.push({ + segments: [segment] + }); + } + else { + segments[segments.length - 1].segments.push(segment); + } } } this.totalSegments = segments.reduce(function (prev, current) { return prev + current.segments.length; }, 0); @@ -1287,7 +1291,7 @@ var Hls2Mp4 = /** @class */ (function () { anchor.click(); setTimeout(function () { return URL.revokeObjectURL(objectUrl); }, 100); }; - Hls2Mp4.version = '1.2.5'; + Hls2Mp4.version = '1.2.7'; Hls2Mp4.TaskType = TaskType; return Hls2Mp4; }()); diff --git a/dist/package.json b/dist/package.json index 4bda5c7..93a7af6 100644 --- a/dist/package.json +++ b/dist/package.json @@ -1,6 +1,6 @@ { "name": "hls2mp4", - "version": "1.2.6", + "version": "1.2.7", "description": "a tool for download hls/m3u8 to mp4", "main": "index.js", "types": "index.d.ts", diff --git a/package.json b/package.json index 3d482c7..0ebde33 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hls2mp4", - "version": "1.2.6", + "version": "1.2.7", "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 63ef170..4198429 100644 --- a/src/index.ts +++ b/src/index.ts @@ -79,7 +79,7 @@ class Hls2Mp4 { private tsDownloadConcurrency: number; private totalSegments = 0; private savedSegments = 0; - public static version = '1.2.5'; + public static version = '1.2.7'; public static TaskType = TaskType; constructor( @@ -230,8 +230,9 @@ class Hls2Mp4 { '#EXT-X-KEY:METHOD=(AES-128|NONE)(,URI="[^"]+"(,IV=\\w+)?)?', 'gi' ) + const extMatchRegExp = new RegExp('#EXTINF:\\d+(\\.\\d+)?,\\n') const matchReg = new RegExp( - keyTagMatchRegExp.source + '|(?<=#EXTINF:\\d+(\\.\\d+)?,\\n).+', + keyTagMatchRegExp.source + '|' + extMatchRegExp.source + '.+', 'gim' ) const matches = content.match(matchReg) @@ -250,13 +251,16 @@ class Hls2Mp4 { segments: [] }) } - else if (i === 0) { - segments.push({ - segments: [matched] - }) - } else { - segments[segments.length - 1].segments.push(matched) + const segment = matched.replace(extMatchRegExp, '') + if (i === 0) { + segments.push({ + segments: [segment] + }) + } + else { + segments[segments.length - 1].segments.push(segment) + } } }