From 6e93bbd698001a9114d5760f6d1776ca1dd236a8 Mon Sep 17 00:00:00 2001 From: nilaoda Date: Fri, 2 Dec 2022 14:29:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81GZip=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=A7=A3=E5=8E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DownloadManager/SimpleDownloadManager.cs | 10 +++----- .../Downloader/SimpleDownloader.cs | 5 ++++ src/N_m3u8DL-RE/Entity/DownloadResult.cs | 1 + src/N_m3u8DL-RE/Util/DownloadUtil.cs | 7 ++++-- src/N_m3u8DL-RE/Util/OtherUtil.cs | 25 +++++++++++++++++++ 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index c402a12..9b232f5 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -11,10 +11,7 @@ using N_m3u8DL_RE.Parser; using N_m3u8DL_RE.Parser.Mp4; using N_m3u8DL_RE.Util; using Spectre.Console; -using Spectre.Console.Rendering; using System.Collections.Concurrent; -using System.Collections.Generic; -using System.IO; using System.Text; namespace N_m3u8DL_RE.DownloadManager @@ -48,12 +45,12 @@ namespace N_m3u8DL_RE.DownloadManager private string? ReadInit(string output) { + var header = new byte[4096]; //4KB using (var fs = File.OpenRead(output)) { - var header = new byte[4096]; //4KB fs.Read(header); - return ReadInit(header); } + return ReadInit(header); } //从文件读取KEY @@ -173,8 +170,7 @@ namespace N_m3u8DL_RE.DownloadManager //读取mp4信息 if (result != null && result.Success) { - var data = File.ReadAllBytes(result.ActualFilePath); - currentKID = ReadInit(data); + currentKID = ReadInit(result.ActualFilePath); //从文件读取KEY await SearchKeyAsync(currentKID); //实时解密 diff --git a/src/N_m3u8DL-RE/Downloader/SimpleDownloader.cs b/src/N_m3u8DL-RE/Downloader/SimpleDownloader.cs index 32daac4..7781288 100644 --- a/src/N_m3u8DL-RE/Downloader/SimpleDownloader.cs +++ b/src/N_m3u8DL-RE/Downloader/SimpleDownloader.cs @@ -67,6 +67,11 @@ namespace N_m3u8DL_RE.Downloader { await ImageHeaderUtil.ProcessAsync(dResult.ActualFilePath); } + //Gzip解压 + if (dResult.GzipHeader) + { + await OtherUtil.DeGzipFileAsync(dResult.ActualFilePath); + } } return dResult; } diff --git a/src/N_m3u8DL-RE/Entity/DownloadResult.cs b/src/N_m3u8DL-RE/Entity/DownloadResult.cs index 66929cb..04bdd79 100644 --- a/src/N_m3u8DL-RE/Entity/DownloadResult.cs +++ b/src/N_m3u8DL-RE/Entity/DownloadResult.cs @@ -13,6 +13,7 @@ namespace N_m3u8DL_RE.Entity public long? RespContentLength { get; set; } public long? ActualContentLength { get; set; } public bool ImageHeader { get; set; } = false; //图片伪装 + public bool GzipHeader { get; set; } = false; //GZip压缩 public required string ActualFilePath { get; set; } } } diff --git a/src/N_m3u8DL-RE/Util/DownloadUtil.cs b/src/N_m3u8DL-RE/Util/DownloadUtil.cs index 2369a56..afe6b1d 100644 --- a/src/N_m3u8DL-RE/Util/DownloadUtil.cs +++ b/src/N_m3u8DL-RE/Util/DownloadUtil.cs @@ -108,11 +108,13 @@ namespace N_m3u8DL_RE.Util var buffer = new byte[16 * 1024]; var size = 0; - //检测imageHeader size = await responseStream.ReadAsync(buffer, cancellationTokenSource.Token); speedContainer.Add(size); await stream.WriteAsync(buffer, 0, size); + //检测imageHeader bool imageHeader = ImageHeaderUtil.IsImageHeader(buffer); + //检测GZip(For DDP Audio) + bool gZipHeader = buffer.Length > 2 && buffer[0] == 0x1f && buffer[1] == 0x8b; while ((size = await responseStream.ReadAsync(buffer, cancellationTokenSource.Token)) > 0) { @@ -125,7 +127,8 @@ namespace N_m3u8DL_RE.Util ActualContentLength = stream.Length, RespContentLength = contentLength, ActualFilePath = path, - ImageHeader= imageHeader + ImageHeader= imageHeader, + GzipHeader = gZipHeader }; } catch (OperationCanceledException oce) when (oce.CancellationToken == cancellationTokenSource.Token) diff --git a/src/N_m3u8DL-RE/Util/OtherUtil.cs b/src/N_m3u8DL-RE/Util/OtherUtil.cs index 01ad877..953d578 100644 --- a/src/N_m3u8DL-RE/Util/OtherUtil.cs +++ b/src/N_m3u8DL-RE/Util/OtherUtil.cs @@ -2,6 +2,7 @@ using N_m3u8DL_RE.Common.Log; using N_m3u8DL_RE.Enum; using System.CommandLine; +using System.IO.Compression; using System.Text; namespace N_m3u8DL_RE.Util @@ -110,5 +111,29 @@ namespace N_m3u8DL_RE.Util } SafeDeleteDir(parent); } + + /// + /// 解压并替换原文件 + /// + /// + public static async Task DeGzipFileAsync(string filePath) + { + string deGzipFile = Path.ChangeExtension(filePath, ".tmp"); + try + { + using (var fileToDecompressAsStream = File.OpenRead(filePath)) + { + using var decompressedStream = File.Create(deGzipFile); + using var decompressionStream = new GZipStream(fileToDecompressAsStream, CompressionMode.Decompress); + await decompressionStream.CopyToAsync(decompressedStream); + } + File.Delete(filePath); + File.Move(deGzipFile, filePath); + } + catch + { + if (File.Exists(deGzipFile)) File.Delete(deGzipFile); + } + } } }