优化Init读取

This commit is contained in:
nilaoda 2023-07-30 02:20:43 +08:00
parent eb85df8b4d
commit 10e67aa14b
3 changed files with 35 additions and 47 deletions

View File

@ -32,29 +32,10 @@ namespace N_m3u8DL_RE.DownloadManager
Downloader = new SimpleDownloader(DownloaderConfig); Downloader = new SimpleDownloader(DownloaderConfig);
} }
private string? ReadInit(byte[] data)
{
var info = MP4InitUtil.ReadInit(data);
if (info.Scheme != null) Logger.WarnMarkUp($"[grey]Type: {info.Scheme}[/]");
if (info.PSSH != null) Logger.WarnMarkUp($"[grey]PSSH(WV): {info.PSSH}[/]");
if (info.KID != null) Logger.WarnMarkUp($"[grey]KID: {info.KID}[/]");
return info.KID;
}
private string? ReadInit(string output)
{
var header = new byte[1 * 1024 * 1024]; //1MB
using (var fs = File.OpenRead(output))
{
fs.Read(header);
}
return ReadInit(header);
}
//从文件读取KEY //从文件读取KEY
private async Task SearchKeyAsync(string? currentKID) private async Task SearchKeyAsync(string? currentKID)
{ {
var _key = await MP4DecryptUtil.SearchKeyFromFile(DownloaderConfig.MyOptions.KeyTextFile, currentKID); var _key = await MP4DecryptUtil.SearchKeyFromFileAsync(DownloaderConfig.MyOptions.KeyTextFile, currentKID);
if (_key != null) if (_key != null)
{ {
if (DownloaderConfig.MyOptions.Keys == null) if (DownloaderConfig.MyOptions.Keys == null)
@ -186,7 +167,7 @@ namespace N_m3u8DL_RE.DownloadManager
//读取mp4信息 //读取mp4信息
if (result != null && result.Success) if (result != null && result.Success)
{ {
currentKID = ReadInit(result.ActualFilePath); currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath);
//从文件读取KEY //从文件读取KEY
await SearchKeyAsync(currentKID); await SearchKeyAsync(currentKID);
//实时解密 //实时解密
@ -253,7 +234,7 @@ namespace N_m3u8DL_RE.DownloadManager
//读取init信息 //读取init信息
if (string.IsNullOrEmpty(currentKID)) if (string.IsNullOrEmpty(currentKID))
{ {
currentKID = ReadInit(result.ActualFilePath); currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath);
} }
//从文件读取KEY //从文件读取KEY
await SearchKeyAsync(currentKID); await SearchKeyAsync(currentKID);
@ -595,7 +576,7 @@ namespace N_m3u8DL_RE.DownloadManager
//重新读取init信息 //重新读取init信息
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 = ReadInit(output); currentKID = MP4DecryptUtil.ReadInit(output);
//从文件读取KEY //从文件读取KEY
await SearchKeyAsync(currentKID); await SearchKeyAsync(currentKID);
} }

View File

@ -58,29 +58,10 @@ namespace N_m3u8DL_RE.DownloadManager
SelectedSteams = selectedSteams; SelectedSteams = selectedSteams;
} }
private string? ReadInit(byte[] data)
{
var info = MP4InitUtil.ReadInit(data);
if (info.Scheme != null) Logger.WarnMarkUp($"[grey]Type: {info.Scheme}[/]");
if (info.PSSH != null) Logger.WarnMarkUp($"[grey]PSSH(WV): {info.PSSH}[/]");
if (info.KID != null) Logger.WarnMarkUp($"[grey]KID: {info.KID}[/]");
return info.KID;
}
private string? ReadInit(string output)
{
using (var fs = File.OpenRead(output))
{
var header = new byte[4096]; //4KB
fs.Read(header);
return ReadInit(header);
}
}
//从文件读取KEY //从文件读取KEY
private async Task SearchKeyAsync(string? currentKID) private async Task SearchKeyAsync(string? currentKID)
{ {
var _key = await MP4DecryptUtil.SearchKeyFromFile(DownloaderConfig.MyOptions.KeyTextFile, currentKID); var _key = await MP4DecryptUtil.SearchKeyFromFileAsync(DownloaderConfig.MyOptions.KeyTextFile, currentKID);
if (_key != null) if (_key != null)
{ {
if (DownloaderConfig.MyOptions.Keys == null) if (DownloaderConfig.MyOptions.Keys == null)
@ -231,7 +212,7 @@ namespace N_m3u8DL_RE.DownloadManager
//读取mp4信息 //读取mp4信息
if (result != null && result.Success) if (result != null && result.Success)
{ {
currentKID = ReadInit(result.ActualFilePath); currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath);
//从文件读取KEY //从文件读取KEY
await SearchKeyAsync(currentKID); await SearchKeyAsync(currentKID);
//实时解密 //实时解密
@ -309,7 +290,7 @@ namespace N_m3u8DL_RE.DownloadManager
//读取init信息 //读取init信息
if (string.IsNullOrEmpty(currentKID)) if (string.IsNullOrEmpty(currentKID))
{ {
currentKID = ReadInit(result.ActualFilePath); currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath);
} }
//从文件读取KEY //从文件读取KEY
await SearchKeyAsync(currentKID); await SearchKeyAsync(currentKID);

View File

@ -1,4 +1,5 @@
using N_m3u8DL_RE.Common.Log; using Mp4SubtitleParser;
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;
@ -92,7 +93,13 @@ namespace N_m3u8DL_RE.Util
})!.WaitForExitAsync(); })!.WaitForExitAsync();
} }
public static async Task<string?> SearchKeyFromFile(string? file, string? kid) /// <summary>
/// 从文本文件中查询KID的KEY
/// </summary>
/// <param name="file">文本文件</param>
/// <param name="kid">目标KID</param>
/// <returns></returns>
public static async Task<string?> SearchKeyFromFileAsync(string? file, string? kid)
{ {
try try
{ {
@ -118,5 +125,24 @@ namespace N_m3u8DL_RE.Util
} }
return null; return null;
} }
public static string? ReadInit(byte[] data)
{
var info = MP4InitUtil.ReadInit(data);
if (info.Scheme != null) Logger.WarnMarkUp($"[grey]Type: {info.Scheme}[/]");
if (info.PSSH != null) Logger.WarnMarkUp($"[grey]PSSH(WV): {info.PSSH}[/]");
if (info.KID != null) Logger.WarnMarkUp($"[grey]KID: {info.KID}[/]");
return info.KID;
}
public static string? ReadInit(string output)
{
using (var fs = File.OpenRead(output))
{
var header = new byte[1 * 1024 * 1024]; //1MB
fs.Read(header);
return ReadInit(header);
}
}
} }
} }