From d9cc09dff86ca259829d1d570c785e00d0382de6 Mon Sep 17 00:00:00 2001 From: nilaoda Date: Thu, 18 Aug 2022 19:50:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96ffmpeg=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E6=97=B6=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0=E5=8F=8A=E6=BB=A4?= =?UTF-8?q?=E9=95=9C=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DownloadManager/SimpleDownloadManager.cs | 15 ++++++++---- src/N_m3u8DL-RE/Util/MergeUtil.cs | 24 +++++++++++++++---- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index bdff980..0b9be68 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -8,6 +8,7 @@ using N_m3u8DL_RE.Downloader; using N_m3u8DL_RE.Entity; using N_m3u8DL_RE.Util; using Spectre.Console; +using Spectre.Console.Rendering; using System.Collections.Concurrent; using System.Text; @@ -58,7 +59,7 @@ namespace N_m3u8DL_RE.DownloadManager } } - private void ChangeSpecInfo(StreamSpec streamSpec, List mediainfos) + private void ChangeSpecInfo(StreamSpec streamSpec, List mediainfos, ref bool useAACFilter) { if (!DownloaderConfig.BinaryMerge && mediainfos.Any(m => m.DolbyVison == true)) { @@ -66,6 +67,11 @@ namespace N_m3u8DL_RE.DownloadManager Logger.WarnMarkUp($"[darkorange3_1]{ResString.autoBinaryMerge2}[/]"); } + if (mediainfos.Where(m => m.Type == "Audio").All(m => m.BaseInfo.Contains("aac"))) + { + useAACFilter = true; + } + if (mediainfos.All(m => m.Type == "Audio")) { streamSpec.MediaType = MediaType.AUDIO; @@ -80,6 +86,7 @@ namespace N_m3u8DL_RE.DownloadManager private async Task DownloadStreamAsync(StreamSpec streamSpec, ProgressTask task) { + bool useAACFilter = false; //ffmpeg合并flag ConcurrentDictionary FileDic = new(); var segments = streamSpec.Playlist?.MediaParts.SelectMany(m => m.MediaSegments); @@ -169,7 +176,7 @@ namespace N_m3u8DL_RE.DownloadManager Logger.WarnMarkUp(ResString.readingInfo); var mediainfos = await MediainfoUtil.ReadInfoAsync(DownloaderConfig.FFmpegBinaryPath!, result.ActualFilePath); mediainfos.ForEach(info => Logger.InfoMarkUp(info.ToStringMarkUp())); - ChangeSpecInfo(streamSpec, mediainfos); + ChangeSpecInfo(streamSpec, mediainfos, ref useAACFilter); readInfo = true; } } @@ -212,7 +219,7 @@ namespace N_m3u8DL_RE.DownloadManager Logger.WarnMarkUp(ResString.readingInfo); var mediainfos = await MediainfoUtil.ReadInfoAsync(DownloaderConfig.FFmpegBinaryPath!, result!.ActualFilePath); mediainfos.ForEach(info => Logger.InfoMarkUp(info.ToStringMarkUp())); - ChangeSpecInfo(streamSpec, mediainfos); + ChangeSpecInfo(streamSpec, mediainfos, ref useAACFilter); readInfo = true; } @@ -459,7 +466,7 @@ namespace N_m3u8DL_RE.DownloadManager var files = FileDic.Values.Select(v => v!.ActualFilePath).OrderBy(s => s).ToArray(); Logger.InfoMarkUp(ResString.ffmpegMerge); var ext = streamSpec.MediaType == MediaType.AUDIO ? "m4a" : "mp4"; - mergeSuccess = MergeUtil.MergeByFFmpeg(DownloaderConfig.FFmpegBinaryPath!, files, Path.ChangeExtension(output, null), ext); + mergeSuccess = MergeUtil.MergeByFFmpeg(DownloaderConfig.FFmpegBinaryPath!, files, Path.ChangeExtension(output, null), ext, useAACFilter); } } diff --git a/src/N_m3u8DL-RE/Util/MergeUtil.cs b/src/N_m3u8DL-RE/Util/MergeUtil.cs index 15517b2..8f8bc67 100644 --- a/src/N_m3u8DL-RE/Util/MergeUtil.cs +++ b/src/N_m3u8DL-RE/Util/MergeUtil.cs @@ -1,4 +1,6 @@ -using System; +using N_m3u8DL_RE.Common.Log; +using Spectre.Console; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -44,7 +46,7 @@ namespace N_m3u8DL_RE.Util } } - public static bool MergeByFFmpeg(string binary, string[] files, string outputPath, string muxFormat, + public static bool MergeByFFmpeg(string binary, string[] files, string outputPath, string muxFormat, bool useAACFilter, bool fastStart = false, bool writeDate = true, string poster = "", string audioName = "", string title = "", string copyright = "", string comment = "", string encodingTool = "", string recTime = "") @@ -58,7 +60,6 @@ namespace N_m3u8DL_RE.Util Path.GetFileName(outputPath) + "_" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")); } - bool useAACFilter = true; StringBuilder command = new StringBuilder("-loglevel warning -i concat:\""); string ddpAudio = string.Empty; string addPoster = "-map 1 -c:v:1 copy -disposition:v:1 attached_pic"; @@ -109,7 +110,10 @@ namespace N_m3u8DL_RE.Util break; } - Process.Start(new ProcessStartInfo() + Logger.DebugMarkUp($"{binary}: {command}"); + + using var p = new Process(); + p.StartInfo = new ProcessStartInfo() { WorkingDirectory = Path.GetDirectoryName(files[0]), FileName = binary, @@ -117,7 +121,17 @@ namespace N_m3u8DL_RE.Util RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false - })!.WaitForExit(); + }; + p.ErrorDataReceived += (sendProcess, output) => + { + if (!string.IsNullOrEmpty(output.Data)) + { + Logger.WarnMarkUp($"[grey]{output.Data.EscapeMarkup()}[/]"); + } + }; + p.Start(); + p.BeginErrorReadLine(); + p.WaitForExit(); if (File.Exists($"{outputPath}.{muxFormat}") && new FileInfo($"{outputPath}.{muxFormat}").Length > 0) return true;