From fffc5666d8f4169ba188a70cb1a7e9f57ec65dbc Mon Sep 17 00:00:00 2001 From: nilaoda Date: Thu, 23 Mar 2023 22:42:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9B=BE=E5=BD=A2=E5=AD=97?= =?UTF-8?q?=E5=B9=95=E5=A4=84=E7=90=86=20#109?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs | 2 +- .../DownloadManager/SimpleDownloadManager.cs | 30 ++------------ .../SimpleLiveRecordManager2.cs | 7 +++- src/N_m3u8DL-RE/Util/SubtitleUtil.cs | 40 +++++++++++++++++++ 4 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 src/N_m3u8DL-RE/Util/SubtitleUtil.cs diff --git a/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs b/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs index 66c9382..0b29227 100644 --- a/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs +++ b/src/N_m3u8DL-RE/CommandLine/CommandInvoker.cs @@ -18,7 +18,7 @@ namespace N_m3u8DL_RE.CommandLine { internal partial class CommandInvoker { - public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20230111"; + public const string VERSION_INFO = "N_m3u8DL-RE (Beta version) 20230323"; [GeneratedRegex("((best|worst)\\d*|all)")] private static partial Regex ForStrRegex(); diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index b3ec3cd..e666c98 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -106,7 +106,7 @@ namespace N_m3u8DL_RE.DownloadManager ConcurrentDictionary FileDic = new(); var segments = streamSpec.Playlist?.MediaParts.SelectMany(m => m.MediaSegments); - if (segments == null) return false; + if (segments == null || !segments.Any()) return false; if (segments.Count() == 1) speedContainer.SingleSegment = true; var type = streamSpec.MediaType ?? Common.Enum.MediaType.VIDEO; @@ -441,19 +441,7 @@ namespace N_m3u8DL_RE.DownloadManager var files = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).ToArray(); //处理图形字幕 - if (finalVtt.Cues.All(v => v.Payload.StartsWith("Base64::"))) - { - Logger.WarnMarkUp(ResString.processImageSub); - var _pad = "0".PadLeft(finalVtt.Cues.Count().ToString().Length, '0'); - var _i = 0; - foreach (var img in finalVtt.Cues) - { - var base64 = img.Payload[8..]; - var name = _i++.ToString(_pad) + ".png"; - await File.WriteAllBytesAsync(Path.Combine(tmpDir, name), Convert.FromBase64String(base64)); - img.Payload = name; - } - } + await SubtitleUtil.TryWriteImagePngsAsync(finalVtt, tmpDir); foreach (var item in files) File.Delete(item); FileDic.Clear(); @@ -504,19 +492,7 @@ namespace N_m3u8DL_RE.DownloadManager var files = FileDic.OrderBy(s => s.Key.Index).Select(s => s.Value).Select(v => v!.ActualFilePath).ToArray(); //处理图形字幕 - if (finalVtt.Cues.All(v => v.Payload.StartsWith("Base64::"))) - { - Logger.WarnMarkUp(ResString.processImageSub); - var _pad = "0".PadLeft(finalVtt.Cues.Count().ToString().Length, '0'); - var _i = 0; - foreach (var img in finalVtt.Cues) - { - var base64 = img.Payload[8..]; - var name = _i++.ToString(_pad) + ".png"; - await File.WriteAllBytesAsync(Path.Combine(tmpDir, name), Convert.FromBase64String(base64)); - img.Payload = name; - } - } + await SubtitleUtil.TryWriteImagePngsAsync(finalVtt, tmpDir); foreach (var item in files) File.Delete(item); FileDic.Clear(); diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs index caf0095..16f4ef2 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs @@ -122,7 +122,7 @@ namespace N_m3u8DL_RE.DownloadManager { name = GetUnixTimestamp(segment.DateTime!.Value).ToString(); } - else if (hls && segment.Index > 10) + else if (hls) { name = segment.Index.ToString(); } @@ -200,6 +200,7 @@ namespace N_m3u8DL_RE.DownloadManager //TryReceiveAll可以稍微缓解一下 source.TryReceiveAll(out IList>? segmentsList); var segments = segmentsList!.SelectMany(s => s); + if (segments == null || !segments.Any()) continue; var segmentsDuration = segments.Sum(s => s.Duration); Logger.DebugMarkUp(string.Join(",", segments.Select(sss => GetSegmentName(sss, false, false)))); @@ -594,6 +595,10 @@ namespace N_m3u8DL_RE.DownloadManager File.Delete(inputFilePath); } } + + //处理图形字幕 + await SubtitleUtil.TryWriteImagePngsAsync(currentVtt, tmpDir); + var subText = currentVtt.ToVtt(); if (outputExt == ".srt") { diff --git a/src/N_m3u8DL-RE/Util/SubtitleUtil.cs b/src/N_m3u8DL-RE/Util/SubtitleUtil.cs new file mode 100644 index 0000000..1876f18 --- /dev/null +++ b/src/N_m3u8DL-RE/Util/SubtitleUtil.cs @@ -0,0 +1,40 @@ +using N_m3u8DL_RE.Common.Entity; +using N_m3u8DL_RE.Common.Log; +using N_m3u8DL_RE.Common.Resource; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace N_m3u8DL_RE.Util +{ + internal class SubtitleUtil + { + /// + /// 写出图形字幕PNG文件 + /// + /// + /// 临时目录 + /// + public static async Task TryWriteImagePngsAsync(WebVttSub? finalVtt, string tmpDir) + { + if (finalVtt != null && finalVtt.Cues.Any(v => v.Payload.StartsWith("Base64::"))) + { + Logger.WarnMarkUp(ResString.processImageSub); + var _i = 0; + foreach (var img in finalVtt.Cues.Where(v => v.Payload.StartsWith("Base64::"))) + { + var name = $"{_i++}.png"; + var dest = ""; + for (; File.Exists(dest = Path.Combine(tmpDir, name)); name = $"{_i++}.png") ; + var base64 = img.Payload[8..]; + await File.WriteAllBytesAsync(dest, Convert.FromBase64String(base64)); + img.Payload = name; + } + return true; + } + else return false; + } + } +}