Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}