try using shaka packager, which can handle WebM, to get KID

This commit is contained in:
hyww 2024-06-01 12:43:57 +08:00
parent 8fdb6bc90b
commit 8fe77bd058
2 changed files with 38 additions and 0 deletions

View File

@ -168,6 +168,10 @@ namespace N_m3u8DL_RE.DownloadManager
if (result != null && result.Success) if (result != null && result.Success)
{ {
currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); 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 //从文件读取KEY
await SearchKeyAsync(currentKID); await SearchKeyAsync(currentKID);
//实时解密 //实时解密
@ -236,6 +240,10 @@ namespace N_m3u8DL_RE.DownloadManager
{ {
currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath); 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 //从文件读取KEY
await SearchKeyAsync(currentKID); 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) 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.ReadInit(output);
// try shaka packager, which can handle WebM
if (string.IsNullOrEmpty(currentKID) && DownloaderConfig.MyOptions.UseShakaPackager) {
currentKID = MP4DecryptUtil.ReadInitShaka(output, mp4decrypt);
}
//从文件读取KEY //从文件读取KEY
await SearchKeyAsync(currentKID); await SearchKeyAsync(currentKID);
} }

View File

@ -3,6 +3,7 @@ using N_m3u8DL_RE.Common.Log;
using N_m3u8DL_RE.Common.Resource; using N_m3u8DL_RE.Common.Resource;
using N_m3u8DL_RE.Config; using N_m3u8DL_RE.Config;
using System.Diagnostics; using System.Diagnostics;
using System.Text.RegularExpressions;
namespace N_m3u8DL_RE.Util namespace N_m3u8DL_RE.Util
{ {
@ -144,5 +145,30 @@ namespace N_m3u8DL_RE.Util
return ReadInit(header); 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;
}
} }
} }