From ddd3931f578f7f8f13ac8f28570264571cb5f4d9 Mon Sep 17 00:00:00 2001 From: nilaoda Date: Mon, 18 Jul 2022 16:07:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96init=E4=B8=AD=E7=9A=84pssh?= =?UTF-8?q?=E5=92=8Ckid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/N_m3u8DL-RE.Parser/Mp4/MP4InitUtil.cs | 62 +++++++++++++++++++ .../DownloadManager/SimpleDownloadManager.cs | 8 +++ 2 files changed, 70 insertions(+) create mode 100644 src/N_m3u8DL-RE.Parser/Mp4/MP4InitUtil.cs diff --git a/src/N_m3u8DL-RE.Parser/Mp4/MP4InitUtil.cs b/src/N_m3u8DL-RE.Parser/Mp4/MP4InitUtil.cs new file mode 100644 index 0000000..5d6fb46 --- /dev/null +++ b/src/N_m3u8DL-RE.Parser/Mp4/MP4InitUtil.cs @@ -0,0 +1,62 @@ +using N_m3u8DL_RE.Common.Util; + +namespace Mp4SubtitleParser +{ + public class MP4InitUtil + { + private static readonly byte[] SYSTEM_ID_WIDEVINE = { 0xED, 0xEF, 0x8B, 0xA9, 0x79, 0xD6, 0x4A, 0xCE, 0xA3, 0xC8, 0x27, 0xDC, 0xD5, 0x1D, 0x21, 0xED }; + private static readonly byte[] SYSTEM_ID_PLAYREADY = { 0x9A, 0x04, 0xF0, 0x79, 0x98, 0x40, 0x42, 0x86, 0xAB, 0x92, 0xE6, 0x5B, 0xE0, 0x88, 0x5F, 0x95 }; + + public static string? ReadWVPssh(byte[] data) + { + string? pssh = null; + //parse init + new MP4Parser() + .Box("moov", MP4Parser.Children) + .FullBox("pssh", (box) => + { + if (!(box.Version == 0 || box.Version == 1)) + throw new Exception("PSSH version can only be 0 or 1"); + var systemId = box.Reader.ReadBytes(16); + if (SYSTEM_ID_WIDEVINE.SequenceEqual(systemId)) + { + var dataSize = box.Reader.ReadUInt32(); + pssh = Convert.ToBase64String(box.Reader.ReadBytes((int)dataSize)); + } + }) + .Parse(data); + return pssh; + } + + public static string? ReadWVKid(byte[] data) + { + string? kid = null; + //parse init + new MP4Parser() + .Box("moov", MP4Parser.Children) + .Box("trak", MP4Parser.Children) + .Box("mdia", MP4Parser.Children) + .Box("minf", MP4Parser.Children) + .Box("stbl", MP4Parser.Children) + .FullBox("stsd", MP4Parser.SampleDescription) + .FullBox("encv", MP4Parser.AllData((data) => + { + kid = HexUtil.BytesToHex(data[^16..]).ToLower(); + })) + .FullBox("enca", MP4Parser.AllData((data) => + { + kid = HexUtil.BytesToHex(data[^16..]).ToLower(); + })) + .FullBox("enct", MP4Parser.AllData((data) => + { + kid = HexUtil.BytesToHex(data[^16..]).ToLower(); + })) + .FullBox("encs", MP4Parser.AllData((data) => + { + kid = HexUtil.BytesToHex(data[^16..]).ToLower(); + })) + .Parse(data); + return kid; + } + } +} diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index c9dcaf6..85356c5 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -74,6 +74,14 @@ namespace N_m3u8DL_RE.DownloadManager task.Increment(1); //修改输出后缀 output = Path.ChangeExtension(output, ".mp4"); + if (result != null && result.Success) + { + var data = File.ReadAllBytes(result.ActualFilePath); + var pssh = MP4InitUtil.ReadWVPssh(data); + var kid = MP4InitUtil.ReadWVKid(data); + if (pssh != null) Logger.WarnMarkUp($"[grey]PSSH(WV): {pssh}[/]"); + if (kid != null) Logger.WarnMarkUp($"[grey]KID: {kid}[/]"); + } } //开始下载