Skip to content

Commit

Permalink
Merge pull request #401 from hyww/feat/webm-decrypt
Browse files Browse the repository at this point in the history
try using shaka packager, which can handle WebM, to get KID
  • Loading branch information
nilaoda authored Jun 22, 2024
2 parents a29c8b9 + 8fe77bd commit 54a9eda
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
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);
//实时解密
Expand Down Expand Up @@ -236,6 +240,10 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
{
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);
//实时解密
Expand Down Expand Up @@ -577,6 +585,10 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
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);
}
Expand Down
26 changes: 26 additions & 0 deletions src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using N_m3u8DL_RE.Common.Resource;
using N_m3u8DL_RE.Config;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace N_m3u8DL_RE.Util
{
Expand Down Expand Up @@ -144,5 +145,30 @@ await Process.Start(new ProcessStartInfo()
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;
}
}
}

0 comments on commit 54a9eda

Please sign in to comment.