From 8fe77bd05897194c5660da6a248be6c446aa6460 Mon Sep 17 00:00:00 2001 From: hyww Date: Sat, 1 Jun 2024 12:43:57 +0800 Subject: [PATCH] try using shaka packager, which can handle WebM, to get KID --- .../DownloadManager/SimpleDownloadManager.cs | 12 +++++++++ src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs | 26 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index 21951fa..ed182b8 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -168,6 +168,10 @@ namespace N_m3u8DL_RE.DownloadManager if (result != null && result.Success) { currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); + // try shaka packager, which can handle WebM + if (string.IsNullOrEmpty(currentKID) && DownloaderConfig.MyOptions.UseShakaPackager) { + currentKID = MP4DecryptUtil.ReadInitShaka(result.ActualFilePath, mp4decrypt); + } //从文件读取KEY await SearchKeyAsync(currentKID); //实时解密 @@ -236,6 +240,10 @@ namespace N_m3u8DL_RE.DownloadManager { currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); } + // try shaka packager, which can handle WebM + if (string.IsNullOrEmpty(currentKID) && DownloaderConfig.MyOptions.UseShakaPackager) { + currentKID = MP4DecryptUtil.ReadInitShaka(result.ActualFilePath, mp4decrypt); + } //从文件读取KEY await SearchKeyAsync(currentKID); //实时解密 @@ -577,6 +585,10 @@ namespace N_m3u8DL_RE.DownloadManager if (mergeSuccess && totalCount >= 1 && string.IsNullOrEmpty(currentKID) && streamSpec.Playlist!.MediaParts.First().MediaSegments.First().EncryptInfo.Method != Common.Enum.EncryptMethod.NONE) { currentKID = MP4DecryptUtil.ReadInit(output); + // try shaka packager, which can handle WebM + if (string.IsNullOrEmpty(currentKID) && DownloaderConfig.MyOptions.UseShakaPackager) { + currentKID = MP4DecryptUtil.ReadInitShaka(output, mp4decrypt); + } //从文件读取KEY await SearchKeyAsync(currentKID); } diff --git a/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs b/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs index f109311..41f9be6 100644 --- a/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs +++ b/src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs @@ -3,6 +3,7 @@ using N_m3u8DL_RE.Common.Log; using N_m3u8DL_RE.Common.Resource; using N_m3u8DL_RE.Config; using System.Diagnostics; +using System.Text.RegularExpressions; namespace N_m3u8DL_RE.Util { @@ -144,5 +145,30 @@ namespace N_m3u8DL_RE.Util return ReadInit(header); } } + + public static string? ReadInitShaka(string output, string bin) + { + Regex ShakaKeyIDRegex = new Regex("Key for key_id=([0-9a-f]+) was not found"); + + // TODO: handle the case that shaka packager actually decrypted (key ID == ZeroKid) + // - stop process + // - remove {output}.tmp.webm + var cmd = $"--quiet --enable_raw_key_decryption input=\"{output}\",stream=0,output=\"{output}.tmp.webm\" " + + $"--keys key_id={ZeroKid}:key={ZeroKid}"; + + using var p = new Process(); + p.StartInfo = new ProcessStartInfo() + { + FileName = bin, + Arguments = cmd, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false + }; + p.Start(); + var errorOutput = p.StandardError.ReadToEnd(); + p.WaitForExit(); + return ShakaKeyIDRegex.Match(errorOutput).Groups[1].Value; + } } }