优化二进制合并开启条件即解密条件

This commit is contained in:
nilaoda 2022-08-17 00:16:19 +08:00
parent 764a87cb6d
commit 3140fcbbb9
3 changed files with 42 additions and 29 deletions

View File

@ -12,6 +12,7 @@ namespace N_m3u8DL_RE.Common.Resource
public static string autoBinaryMerge { get => GetText("autoBinaryMerge"); }
public static string autoBinaryMerge2 { get => GetText("autoBinaryMerge2"); }
public static string autoBinaryMerge3 { get => GetText("autoBinaryMerge3"); }
public static string autoBinaryMerge4 { get => GetText("autoBinaryMerge4"); }
public static string badM3u8 { get => GetText("badM3u8"); }
public static string binaryMerge { get => GetText("binaryMerge"); }
public static string checkingLast { get => GetText("checkingLast"); }

View File

@ -28,6 +28,12 @@ namespace N_m3u8DL_RE.Common.Resource
zhTW: "檢測到無法識別的加密方式,自動開啟二進位制合併",
enUS: "An unrecognized encryption method is detected, binary merging is automatically enabled"
),
["autoBinaryMerge4"] = new TextContainer
(
zhCN: "检测到CENC加密方式自动开启二进制合并",
zhTW: "檢測到CENC加密方式自動開啟二進位制合併",
enUS: "When CENC encryption is detected, binary merging is automatically enabled"
),
["badM3u8"] = new TextContainer
(
zhCN: "错误的m3u8",

View File

@ -95,6 +95,13 @@ namespace N_m3u8DL_RE.DownloadManager
//开始下载
Logger.InfoMarkUp(ResString.startDownloading + streamSpec.ToShortString());
//对于CENC全部自动开启二进制合并
if (!DownloaderConfig.BinaryMerge && totalCount >= 1 && streamSpec.Playlist!.MediaParts.First().MediaSegments.First().EncryptInfo.Method == Common.Enum.EncryptMethod.CENC)
{
DownloaderConfig.BinaryMerge = true;
Logger.WarnMarkUp($"[darkorange3_1]{ResString.autoBinaryMerge4}[/]");
}
//下载init
if (streamSpec.Playlist?.MediaInit != null)
{
@ -447,42 +454,41 @@ namespace N_m3u8DL_RE.DownloadManager
}
}
//调用mp4decrypt解密
if (mergeSuccess && File.Exists(output) && !DownloaderConfig.MP4RealTimeDecryption && DownloaderConfig.Keys != null && DownloaderConfig.Keys.Length > 0)
//重新读取init信息
if (mergeSuccess && totalCount >= 1 && string.IsNullOrEmpty(currentKID) && streamSpec.Playlist!.MediaParts.First().MediaSegments.First().EncryptInfo.Method == Common.Enum.EncryptMethod.CENC)
{
if (totalCount >= 1 && streamSpec.Playlist!.MediaParts.First().MediaSegments.First().EncryptInfo.Method != Common.Enum.EncryptMethod.NONE)
using (var fs = File.OpenRead(output))
{
if (string.IsNullOrEmpty(currentKID))
var header = new byte[4096]; //4KB
fs.Read(header);
currentKID = ReadInit(header);
//从文件读取KEY
var _key = await MP4DecryptUtil.SearchKeyFromFile(DownloaderConfig.KeyTextFile, currentKID);
if (_key != null)
{
using (var fs = File.OpenRead(output))
{
var header = new byte[4096]; //4KB
fs.Read(header);
currentKID = ReadInit(header);
//从文件读取KEY
var _key = await MP4DecryptUtil.SearchKeyFromFile(DownloaderConfig.KeyTextFile, currentKID);
if (_key != null)
{
if (DownloaderConfig.Keys == null)
DownloaderConfig.Keys = new string[] { _key };
else
DownloaderConfig.Keys = DownloaderConfig.Keys.Concat(new string[] { _key }).ToArray();
}
}
}
var enc = output;
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));
Logger.InfoMarkUp($"[grey]Decrypting...[/]");
var result = await MP4DecryptUtil.DecryptAsync(DownloaderConfig.UseShakaPackager, mp4decrypt, DownloaderConfig.Keys, enc, dec, currentKID);
if (result)
{
File.Delete(enc);
File.Move(dec, enc);
output = dec;
if (DownloaderConfig.Keys == null)
DownloaderConfig.Keys = new string[] { _key };
else
DownloaderConfig.Keys = DownloaderConfig.Keys.Concat(new string[] { _key }).ToArray();
}
}
}
//调用mp4decrypt解密
if (mergeSuccess && File.Exists(output) && !string.IsNullOrEmpty(currentKID) && !DownloaderConfig.MP4RealTimeDecryption && DownloaderConfig.Keys != null && DownloaderConfig.Keys.Length > 0)
{
var enc = output;
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));
Logger.InfoMarkUp($"[grey]Decrypting...[/]");
var result = await MP4DecryptUtil.DecryptAsync(DownloaderConfig.UseShakaPackager, mp4decrypt, DownloaderConfig.Keys, enc, dec, currentKID);
if (result)
{
File.Delete(enc);
File.Move(dec, enc);
output = dec;
}
}
return true;
}