From 10e67aa14b29161a857aa470b8f2958ebb74cb17 Mon Sep 17 00:00:00 2001 From: nilaoda Date: Sun, 30 Jul 2023 02:20:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Init=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DownloadManager/SimpleDownloadManager.cs | 27 +++-------------- .../SimpleLiveRecordManager2.cs | 25 ++-------------- src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs | 30 +++++++++++++++++-- 3 files changed, 35 insertions(+), 47 deletions(-) diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index 33c8a85..73af9bd 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -32,29 +32,10 @@ namespace N_m3u8DL_RE.DownloadManager Downloader = new SimpleDownloader(DownloaderConfig); } - private string? ReadInit(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; - } - - private string? ReadInit(string output) - { - var header = new byte[1 * 1024 * 1024]; //1MB - using (var fs = File.OpenRead(output)) - { - fs.Read(header); - } - return ReadInit(header); - } - //从文件读取KEY private async Task SearchKeyAsync(string? currentKID) { - var _key = await MP4DecryptUtil.SearchKeyFromFile(DownloaderConfig.MyOptions.KeyTextFile, currentKID); + var _key = await MP4DecryptUtil.SearchKeyFromFileAsync(DownloaderConfig.MyOptions.KeyTextFile, currentKID); if (_key != null) { if (DownloaderConfig.MyOptions.Keys == null) @@ -186,7 +167,7 @@ namespace N_m3u8DL_RE.DownloadManager //读取mp4信息 if (result != null && result.Success) { - currentKID = ReadInit(result.ActualFilePath); + currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); //从文件读取KEY await SearchKeyAsync(currentKID); //实时解密 @@ -253,7 +234,7 @@ namespace N_m3u8DL_RE.DownloadManager //读取init信息 if (string.IsNullOrEmpty(currentKID)) { - currentKID = ReadInit(result.ActualFilePath); + currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); } //从文件读取KEY await SearchKeyAsync(currentKID); @@ -595,7 +576,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 = ReadInit(output); + currentKID = MP4DecryptUtil.ReadInit(output); //从文件读取KEY await SearchKeyAsync(currentKID); } diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs index 7701642..4064ed1 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs @@ -58,29 +58,10 @@ namespace N_m3u8DL_RE.DownloadManager SelectedSteams = selectedSteams; } - private string? ReadInit(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; - } - - private string? ReadInit(string output) - { - using (var fs = File.OpenRead(output)) - { - var header = new byte[4096]; //4KB - fs.Read(header); - return ReadInit(header); - } - } - //从文件读取KEY private async Task SearchKeyAsync(string? currentKID) { - var _key = await MP4DecryptUtil.SearchKeyFromFile(DownloaderConfig.MyOptions.KeyTextFile, currentKID); + var _key = await MP4DecryptUtil.SearchKeyFromFileAsync(DownloaderConfig.MyOptions.KeyTextFile, currentKID); if (_key != null) { if (DownloaderConfig.MyOptions.Keys == null) @@ -231,7 +212,7 @@ namespace N_m3u8DL_RE.DownloadManager //读取mp4信息 if (result != null && result.Success) { - currentKID = ReadInit(result.ActualFilePath); + currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); //从文件读取KEY await SearchKeyAsync(currentKID); //实时解密 @@ -309,7 +290,7 @@ namespace N_m3u8DL_RE.DownloadManager //读取init信息 if (string.IsNullOrEmpty(currentKID)) { - currentKID = ReadInit(result.ActualFilePath); + currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); } //从文件读取KEY await SearchKeyAsync(currentKID); diff --git a/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs b/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs index 99b8f3b..f109311 100644 --- a/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs +++ b/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs @@ -1,4 +1,5 @@ -using N_m3u8DL_RE.Common.Log; +using Mp4SubtitleParser; +using N_m3u8DL_RE.Common.Log; using N_m3u8DL_RE.Common.Resource; using N_m3u8DL_RE.Config; using System.Diagnostics; @@ -92,7 +93,13 @@ namespace N_m3u8DL_RE.Util })!.WaitForExitAsync(); } - public static async Task SearchKeyFromFile(string? file, string? kid) + /// + /// 从文本文件中查询KID的KEY + /// + /// 文本文件 + /// 目标KID + /// + public static async Task SearchKeyFromFileAsync(string? file, string? kid) { try { @@ -118,5 +125,24 @@ namespace N_m3u8DL_RE.Util } return null; } + + public static string? ReadInit(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; + } + + public static string? ReadInit(string output) + { + using (var fs = File.OpenRead(output)) + { + var header = new byte[1 * 1024 * 1024]; //1MB + fs.Read(header); + return ReadInit(header); + } + } } }