From 508f39b649a99c42f97ce86976c9ef7184048d43 Mon Sep 17 00:00:00 2001 From: AdiEcho <30563671+AdiEcho@users.noreply.github.com> Date: Thu, 25 Jul 2024 01:05:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9ADRM=E6=96=87?= =?UTF-8?q?=E4=BB=B6KID=E8=AF=BB=E5=8F=96=E5=BC=82=E5=B8=B8=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#422)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [Fix] fix reading the kid in a multi-DRM file * [Fix] fix decrypting multi-drm files * [Fix] fix glibc error in building * [Feat] 删除ReadInit方法 * [Fix] 修复ReadMe和more-help中显示参数不准确的问题 --- .github/workflows/build_latest.yml | 1 + README.md | 4 ++-- src/N_m3u8DL-RE.Common/Resource/StaticText.cs | 12 +++++----- src/N_m3u8DL-RE.Parser/Mp4/MP4InitUtil.cs | 10 +++++++-- .../DownloadManager/SimpleDownloadManager.cs | 22 +++++++++++-------- .../SimpleLiveRecordManager2.cs | 4 ++-- src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs | 16 +++++++++----- 7 files changed, 43 insertions(+), 26 deletions(-) diff --git a/.github/workflows/build_latest.yml b/.github/workflows/build_latest.yml index b5e4910..d67398c 100644 --- a/.github/workflows/build_latest.yml +++ b/.github/workflows/build_latest.yml @@ -20,6 +20,7 @@ on: env: DOTNET_SDK_VERSION: "8.0.*" + ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true jobs: build-win-x64-arm64: diff --git a/README.md b/README.md index 8261edd..dddc509 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ More Help: 通过正则表达式选择符合要求的视频流. 你能够以:分隔形式指定如下参数: -id=REGEX:lang=REGEX:name=REGEX:codec=REGEX:res=REGEX:frame=REGEX +id=REGEX:lang=REGEX:name=REGEX:codecs=REGEX:res=REGEX:frame=REGEX segsMin=number:segsMax=number:ch=REGEX:range=REGEX:url=REGEX plistDurMin=hms:plistDurMax=hms:for=FOR @@ -151,7 +151,7 @@ plistDurMin=hms:plistDurMax=hms:for=FOR # 选择最佳视频 -sv best # 选择4K+HEVC视频 --sv res="3840*":codec=hvc1:for=best +-sv res="3840*":codecs=hvc1:for=best # 选择长度大于1小时20分钟30秒的视频 -sv plistDurMin="1h20m30s":for=best ``` diff --git a/src/N_m3u8DL-RE.Common/Resource/StaticText.cs b/src/N_m3u8DL-RE.Common/Resource/StaticText.cs index 5a8e3a8..aaa4615 100644 --- a/src/N_m3u8DL-RE.Common/Resource/StaticText.cs +++ b/src/N_m3u8DL-RE.Common/Resource/StaticText.cs @@ -475,7 +475,7 @@ namespace N_m3u8DL_RE.Common.Resource ["cmd_selectVideo_more"] = new TextContainer ( zhCN: "通过正则表达式选择符合要求的视频流. 你能够以:分隔形式指定如下参数:\r\n\r\n" + - "id=REGEX:lang=REGEX:name=REGEX:codec=REGEX:res=REGEX:frame=REGEX\r\n" + + "id=REGEX:lang=REGEX:name=REGEX:codecs=REGEX:res=REGEX:frame=REGEX\r\n" + "segsMin=number:segsMax=number:ch=REGEX:range=REGEX:url=REGEX\r\n" + "plistDurMin=hms:plistDurMax=hms:role=string:for=FOR\r\n\r\n" + "* for=FOR: 选择方式. best[number], worst[number], all (默认: best)\r\n\r\n" + @@ -483,12 +483,12 @@ namespace N_m3u8DL_RE.Common.Resource "# 选择最佳视频\r\n" + "-sv best\r\n" + "# 选择4K+HEVC视频\r\n" + - "-sv res=\"3840*\":codec=hvc1:for=best\r\n" + + "-sv res=\"3840*\":codecs=hvc1:for=best\r\n" + "# 选择长度大于1小时20分钟30秒的视频\r\n" + "-sv plistDurMin=\"1h20m30s\":for=best\r\n" + "-sv role=\"main\":for:best\r\n", zhTW: "通過正則表達式選擇符合要求的影片軌. 你能夠以:分隔形式指定如下參數:\r\n\r\n" + - "id=REGEX:lang=REGEX:name=REGEX:codec=REGEX:res=REGEX:frame=REGEX\r\n" + + "id=REGEX:lang=REGEX:name=REGEX:codecs=REGEX:res=REGEX:frame=REGEX\r\n" + "segsMin=number:segsMax=number:ch=REGEX:range=REGEX:url=REGEX\r\n" + "plistDurMin=hms:plistDurMax=hms:role=string:for=FOR\r\n\r\n" + "* for=FOR: 選擇方式. best[number], worst[number], all (默認: best)\r\n\r\n" + @@ -496,12 +496,12 @@ namespace N_m3u8DL_RE.Common.Resource "# 選擇最佳影片\r\n" + "-sv best\r\n" + "# 選擇4K+HEVC影片\r\n" + - "-sv res=\"3840*\":codec=hvc1:for=best\r\n" + + "-sv res=\"3840*\":codecs=hvc1:for=best\r\n" + "# 選擇長度大於1小時20分鐘30秒的影片\r\n" + "-sv plistDurMin=\"1h20m30s\":for=best\r\n" + "-sv role=\"main\":for:best\r\n", enUS: "Select video streams by regular expressions. OPTIONS is a colon separated list of:\r\n\r\n" + - "id=REGEX:lang=REGEX:name=REGEX:codec=REGEX:res=REGEX:frame=REGEX\r\n" + + "id=REGEX:lang=REGEX:name=REGEX:codecs=REGEX:res=REGEX:frame=REGEX\r\n" + "segsMin=number:segsMax=number:ch=REGEX:range=REGEX:url=REGEX\r\n" + "plistDurMin=hms:plistDurMax=hms:role=string:for=FOR\r\n\r\n" + "* for=FOR: Select type. best[number], worst[number], all (Default: best)\r\n\r\n" + @@ -509,7 +509,7 @@ namespace N_m3u8DL_RE.Common.Resource "# select best video\r\n" + "-sv best\r\n" + "# select 4K+HEVC video\r\n" + - "-sv res=\"3840*\":codec=hvc1:for=best\r\n" + + "-sv res=\"3840*\":codecs=hvc1:for=best\r\n" + "# Select best video with duration longer than 1 hour 20 minutes 30 seconds\r\n" + "-sv plistDurMin=\"1h20m30s\":for=best\r\n" + "-sv role=\"main\":for:best\r\n" diff --git a/src/N_m3u8DL-RE.Parser/Mp4/MP4InitUtil.cs b/src/N_m3u8DL-RE.Parser/Mp4/MP4InitUtil.cs index 0482a0f..3e5ea62 100644 --- a/src/N_m3u8DL-RE.Parser/Mp4/MP4InitUtil.cs +++ b/src/N_m3u8DL-RE.Parser/Mp4/MP4InitUtil.cs @@ -8,6 +8,7 @@ namespace Mp4SubtitleParser public string? PSSH; public string? KID; public string? Scheme; + public bool isMultiDRM; } public class MP4InitUtil @@ -19,7 +20,6 @@ namespace Mp4SubtitleParser { var info = new ParsedMP4Info(); - //parse init new MP4Parser() .Box("moov", MP4Parser.Children) @@ -36,7 +36,13 @@ namespace Mp4SubtitleParser if (SYSTEM_ID_WIDEVINE.SequenceEqual(systemId)) { var dataSize = box.Reader.ReadUInt32(); - info.PSSH = Convert.ToBase64String(box.Reader.ReadBytes((int)dataSize)); + var psshData = box.Reader.ReadBytes((int)dataSize); + info.PSSH = Convert.ToBase64String(psshData); + if (info.KID == "00000000000000000000000000000000") + { + info.KID = HexUtil.BytesToHex(psshData[2..18]).ToLower(); + info.isMultiDRM = true; + } } }) .FullBox("encv", MP4Parser.AllData(data => ReadBox(data, info))) diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index 8ad42f4..5893cc5 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -76,7 +76,6 @@ namespace N_m3u8DL_RE.DownloadManager } } - private async Task DownloadStreamAsync(StreamSpec streamSpec, ProgressTask task, SpeedContainer speedContainer) { speedContainer.ResetVars(); @@ -115,6 +114,7 @@ namespace N_m3u8DL_RE.DownloadManager var mp4InitFile = ""; var currentKID = ""; var readInfo = false; //是否读取过 + var mp4Info = new ParsedMP4Info(); //用户自定义范围导致被跳过的时长 计算字幕偏移使用 var skippedDur = streamSpec.SkippedDuration ?? 0d; @@ -167,7 +167,8 @@ namespace N_m3u8DL_RE.DownloadManager //读取mp4信息 if (result != null && result.Success) { - currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); + mp4Info = MP4DecryptUtil.GetMP4Info(result.ActualFilePath); + currentKID = mp4Info.KID; // try shaka packager, which can handle WebM if (string.IsNullOrEmpty(currentKID) && DownloaderConfig.MyOptions.UseShakaPackager) { currentKID = MP4DecryptUtil.ReadInitShaka(result.ActualFilePath, mp4decrypt); @@ -179,7 +180,7 @@ namespace N_m3u8DL_RE.DownloadManager { var enc = result.ActualFilePath; var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc)); - var dResult = await MP4DecryptUtil.DecryptAsync(DownloaderConfig.MyOptions.UseShakaPackager, mp4decrypt, DownloaderConfig.MyOptions.Keys, enc, dec, currentKID); + var dResult = await MP4DecryptUtil.DecryptAsync(DownloaderConfig.MyOptions.UseShakaPackager, mp4decrypt, DownloaderConfig.MyOptions.Keys, enc, dec, currentKID, isMultiDRM: mp4Info.isMultiDRM); if (dResult) { FileDic[streamSpec.Playlist.MediaInit]!.ActualFilePath = dec; @@ -238,7 +239,7 @@ namespace N_m3u8DL_RE.DownloadManager //读取init信息 if (string.IsNullOrEmpty(currentKID)) { - currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); + currentKID = MP4DecryptUtil.GetMP4Info(result.ActualFilePath).KID; } // try shaka packager, which can handle WebM if (string.IsNullOrEmpty(currentKID) && DownloaderConfig.MyOptions.UseShakaPackager) { @@ -251,7 +252,8 @@ namespace N_m3u8DL_RE.DownloadManager { var enc = result.ActualFilePath; var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc)); - var dResult = await MP4DecryptUtil.DecryptAsync(DownloaderConfig.MyOptions.UseShakaPackager, mp4decrypt, DownloaderConfig.MyOptions.Keys, enc, dec, currentKID, mp4InitFile); + mp4Info = MP4DecryptUtil.GetMP4Info(enc); + var dResult = await MP4DecryptUtil.DecryptAsync(DownloaderConfig.MyOptions.UseShakaPackager, mp4decrypt, DownloaderConfig.MyOptions.Keys, enc, dec, currentKID, mp4InitFile, isMultiDRM: mp4Info.isMultiDRM); if (dResult) { File.Delete(enc); @@ -288,7 +290,8 @@ namespace N_m3u8DL_RE.DownloadManager { var enc = result.ActualFilePath; var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc)); - var dResult = await MP4DecryptUtil.DecryptAsync(DownloaderConfig.MyOptions.UseShakaPackager, mp4decrypt, DownloaderConfig.MyOptions.Keys, enc, dec, currentKID, mp4InitFile); + mp4Info = MP4DecryptUtil.GetMP4Info(enc); + var dResult = await MP4DecryptUtil.DecryptAsync(DownloaderConfig.MyOptions.UseShakaPackager, mp4decrypt, DownloaderConfig.MyOptions.Keys, enc, dec, currentKID, mp4InitFile, isMultiDRM: mp4Info.isMultiDRM); if (dResult) { File.Delete(enc); @@ -584,7 +587,7 @@ namespace N_m3u8DL_RE.DownloadManager //重新读取init信息 if (mergeSuccess && totalCount >= 1 && string.IsNullOrEmpty(currentKID) && streamSpec.Playlist!.MediaParts.First().MediaSegments.First().EncryptInfo.Method != Common.Enum.EncryptMethod.NONE) { - currentKID = MP4DecryptUtil.ReadInit(output); + currentKID = MP4DecryptUtil.GetMP4Info(output).KID; // try shaka packager, which can handle WebM if (string.IsNullOrEmpty(currentKID) && DownloaderConfig.MyOptions.UseShakaPackager) { currentKID = MP4DecryptUtil.ReadInitShaka(output, mp4decrypt); @@ -594,12 +597,13 @@ namespace N_m3u8DL_RE.DownloadManager } //调用mp4decrypt解密 - if (mergeSuccess && File.Exists(output) && !string.IsNullOrEmpty(currentKID) && !DownloaderConfig.MyOptions.MP4RealTimeDecryption && DownloaderConfig.MyOptions.Keys != null && DownloaderConfig.MyOptions.Keys.Length > 0) + if (mergeSuccess && File.Exists(output) && !string.IsNullOrEmpty(currentKID) && !DownloaderConfig.MyOptions.MP4RealTimeDecryption && DownloaderConfig.MyOptions.Keys != null && DownloaderConfig.MyOptions.Keys.Length > 0) { var enc = output; var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc)); + mp4Info = MP4DecryptUtil.GetMP4Info(enc); Logger.InfoMarkUp($"[grey]Decrypting...[/]"); - var result = await MP4DecryptUtil.DecryptAsync(DownloaderConfig.MyOptions.UseShakaPackager, mp4decrypt, DownloaderConfig.MyOptions.Keys, enc, dec, currentKID); + var result = await MP4DecryptUtil.DecryptAsync(DownloaderConfig.MyOptions.UseShakaPackager, mp4decrypt, DownloaderConfig.MyOptions.Keys, enc, dec, currentKID, isMultiDRM: mp4Info.isMultiDRM); if (result) { File.Delete(enc); diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs index 4ab0b66..92674dc 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs @@ -212,7 +212,7 @@ namespace N_m3u8DL_RE.DownloadManager //读取mp4信息 if (result != null && result.Success) { - currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); + currentKID = MP4DecryptUtil.GetMP4Info(result.ActualFilePath).KID; //从文件读取KEY await SearchKeyAsync(currentKID); //实时解密 @@ -290,7 +290,7 @@ namespace N_m3u8DL_RE.DownloadManager //读取init信息 if (string.IsNullOrEmpty(currentKID)) { - currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); + currentKID = MP4DecryptUtil.GetMP4Info(result.ActualFilePath).KID; } //从文件读取KEY await SearchKeyAsync(currentKID); diff --git a/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs b/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs index 41f9be6..a04291e 100644 --- a/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs +++ b/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs @@ -10,12 +10,18 @@ namespace N_m3u8DL_RE.Util internal class MP4DecryptUtil { private static string ZeroKid = "00000000000000000000000000000000"; - public static async Task DecryptAsync(bool shakaPackager, string bin, string[]? keys, string source, string dest, string? kid, string init = "") + public static async Task DecryptAsync(bool shakaPackager, string bin, string[]? keys, string source, string dest, string? kid, string init = "", bool isMultiDRM=false) { if (keys == null || keys.Length == 0) return false; string? keyPair = null; string? trackId = null; + + if (isMultiDRM) + { + trackId = "1"; + } + if (!string.IsNullOrEmpty(kid)) { var test = keys.Where(k => k.StartsWith(kid)); @@ -127,22 +133,22 @@ namespace N_m3u8DL_RE.Util return null; } - public static string? ReadInit(byte[] data) + public static ParsedMP4Info GetMP4Info(byte[] data) { var info = MP4InitUtil.ReadInit(data); if (info.Scheme != null) Logger.WarnMarkUp($"[grey]Type: {info.Scheme}[/]"); if (info.PSSH != null) Logger.WarnMarkUp($"[grey]PSSH(WV): {info.PSSH}[/]"); if (info.KID != null) Logger.WarnMarkUp($"[grey]KID: {info.KID}[/]"); - return info.KID; + return info; } - public static string? ReadInit(string output) + public static ParsedMP4Info GetMP4Info(string output) { using (var fs = File.OpenRead(output)) { var header = new byte[1 * 1024 * 1024]; //1MB fs.Read(header); - return ReadInit(header); + return GetMP4Info(header); } }