Skip to content

Commit

Permalink
优化Init读取
Browse files Browse the repository at this point in the history
  • Loading branch information
nilaoda committed Jul 29, 2023
1 parent eb85df8 commit 10e67aa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 47 deletions.
27 changes: 4 additions & 23 deletions src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,10 @@ public SimpleDownloadManager(DownloaderConfig downloaderConfig, List<StreamSpec>
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
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 (DownloaderConfig.MyOptions.Keys == null)
Expand Down Expand Up @@ -186,7 +167,7 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
//读取mp4信息
if (result != null && result.Success)
{
currentKID = ReadInit(result.ActualFilePath);
currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath);
//从文件读取KEY
await SearchKeyAsync(currentKID);
//实时解密
Expand Down Expand Up @@ -253,7 +234,7 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
//读取init信息
if (string.IsNullOrEmpty(currentKID))
{
currentKID = ReadInit(result.ActualFilePath);
currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath);
}
//从文件读取KEY
await SearchKeyAsync(currentKID);
Expand Down Expand Up @@ -595,7 +576,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
//重新读取init信息
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
await SearchKeyAsync(currentKID);
}
Expand Down
25 changes: 3 additions & 22 deletions src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,10 @@ public SimpleLiveRecordManager2(DownloaderConfig downloaderConfig, List<StreamSp
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
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 (DownloaderConfig.MyOptions.Keys == null)
Expand Down Expand Up @@ -231,7 +212,7 @@ private async Task<bool> RecordStreamAsync(StreamSpec streamSpec, ProgressTask t
//读取mp4信息
if (result != null && result.Success)
{
currentKID = ReadInit(result.ActualFilePath);
currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath);
//从文件读取KEY
await SearchKeyAsync(currentKID);
//实时解密
Expand Down Expand Up @@ -309,7 +290,7 @@ private async Task<bool> RecordStreamAsync(StreamSpec streamSpec, ProgressTask t
//读取init信息
if (string.IsNullOrEmpty(currentKID))
{
currentKID = ReadInit(result.ActualFilePath);
currentKID = MP4DecryptUtil.ReadInit(result.ActualFilePath);
}
//从文件读取KEY
await SearchKeyAsync(currentKID);
Expand Down
30 changes: 28 additions & 2 deletions src/N_m3u8DL-RE/Util/MP4DecryptUtil.cs
Original file line number Diff line number Diff line change
@@ -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.Config;
using System.Diagnostics;
Expand Down Expand Up @@ -92,7 +93,13 @@ await Process.Start(new ProcessStartInfo()
})!.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
{
Expand All @@ -118,5 +125,24 @@ await Process.Start(new ProcessStartInfo()
}
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);
}
}
}
}

0 comments on commit 10e67aa

Please sign in to comment.