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 33c8a85e..73af9bde 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -32,29 +32,10 @@ public SimpleDownloadManager(DownloaderConfig downloaderConfig, List 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 @@ private async Task DownloadStreamAsync(StreamSpec streamSpec, ProgressTask //读取mp4信息 if (result != null && result.Success) { - currentKID = ReadInit(result.ActualFilePath); + currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); //从文件读取KEY await SearchKeyAsync(currentKID); //实时解密 @@ -253,7 +234,7 @@ private async Task DownloadStreamAsync(StreamSpec streamSpec, ProgressTask //读取init信息 if (string.IsNullOrEmpty(currentKID)) { - currentKID = ReadInit(result.ActualFilePath); + currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); } //从文件读取KEY await SearchKeyAsync(currentKID); @@ -595,7 +576,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) => //重新读取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 77016423..4064ed12 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs @@ -58,29 +58,10 @@ public SimpleLiveRecordManager2(DownloaderConfig downloaderConfig, List RecordStreamAsync(StreamSpec streamSpec, ProgressTask t //读取mp4信息 if (result != null && result.Success) { - currentKID = ReadInit(result.ActualFilePath); + currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); //从文件读取KEY await SearchKeyAsync(currentKID); //实时解密 @@ -309,7 +290,7 @@ private async Task RecordStreamAsync(StreamSpec streamSpec, ProgressTask t //读取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 99b8f3b8..f109311e 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 @@ await Process.Start(new ProcessStartInfo() })!.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 @@ await Process.Start(new ProcessStartInfo() } 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); + } + } } }