优化ffmpeg合并时日志打印及滤镜选择

This commit is contained in:
nilaoda 2022-08-18 19:50:43 +08:00
parent 04eaa64d4e
commit d9cc09dff8
2 changed files with 30 additions and 9 deletions

View File

@ -8,6 +8,7 @@ using N_m3u8DL_RE.Downloader;
using N_m3u8DL_RE.Entity; using N_m3u8DL_RE.Entity;
using N_m3u8DL_RE.Util; using N_m3u8DL_RE.Util;
using Spectre.Console; using Spectre.Console;
using Spectre.Console.Rendering;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Text; using System.Text;
@ -58,7 +59,7 @@ namespace N_m3u8DL_RE.DownloadManager
} }
} }
private void ChangeSpecInfo(StreamSpec streamSpec, List<Mediainfo> mediainfos) private void ChangeSpecInfo(StreamSpec streamSpec, List<Mediainfo> mediainfos, ref bool useAACFilter)
{ {
if (!DownloaderConfig.BinaryMerge && mediainfos.Any(m => m.DolbyVison == true)) if (!DownloaderConfig.BinaryMerge && mediainfos.Any(m => m.DolbyVison == true))
{ {
@ -66,6 +67,11 @@ namespace N_m3u8DL_RE.DownloadManager
Logger.WarnMarkUp($"[darkorange3_1]{ResString.autoBinaryMerge2}[/]"); 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")) if (mediainfos.All(m => m.Type == "Audio"))
{ {
streamSpec.MediaType = MediaType.AUDIO; streamSpec.MediaType = MediaType.AUDIO;
@ -80,6 +86,7 @@ namespace N_m3u8DL_RE.DownloadManager
private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask task) private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask task)
{ {
bool useAACFilter = false; //ffmpeg合并flag
ConcurrentDictionary<MediaSegment, DownloadResult?> FileDic = new(); ConcurrentDictionary<MediaSegment, DownloadResult?> FileDic = new();
var segments = streamSpec.Playlist?.MediaParts.SelectMany(m => m.MediaSegments); var segments = streamSpec.Playlist?.MediaParts.SelectMany(m => m.MediaSegments);
@ -169,7 +176,7 @@ namespace N_m3u8DL_RE.DownloadManager
Logger.WarnMarkUp(ResString.readingInfo); Logger.WarnMarkUp(ResString.readingInfo);
var mediainfos = await MediainfoUtil.ReadInfoAsync(DownloaderConfig.FFmpegBinaryPath!, result.ActualFilePath); var mediainfos = await MediainfoUtil.ReadInfoAsync(DownloaderConfig.FFmpegBinaryPath!, result.ActualFilePath);
mediainfos.ForEach(info => Logger.InfoMarkUp(info.ToStringMarkUp())); mediainfos.ForEach(info => Logger.InfoMarkUp(info.ToStringMarkUp()));
ChangeSpecInfo(streamSpec, mediainfos); ChangeSpecInfo(streamSpec, mediainfos, ref useAACFilter);
readInfo = true; readInfo = true;
} }
} }
@ -212,7 +219,7 @@ namespace N_m3u8DL_RE.DownloadManager
Logger.WarnMarkUp(ResString.readingInfo); Logger.WarnMarkUp(ResString.readingInfo);
var mediainfos = await MediainfoUtil.ReadInfoAsync(DownloaderConfig.FFmpegBinaryPath!, result!.ActualFilePath); var mediainfos = await MediainfoUtil.ReadInfoAsync(DownloaderConfig.FFmpegBinaryPath!, result!.ActualFilePath);
mediainfos.ForEach(info => Logger.InfoMarkUp(info.ToStringMarkUp())); mediainfos.ForEach(info => Logger.InfoMarkUp(info.ToStringMarkUp()));
ChangeSpecInfo(streamSpec, mediainfos); ChangeSpecInfo(streamSpec, mediainfos, ref useAACFilter);
readInfo = true; readInfo = true;
} }
@ -459,7 +466,7 @@ namespace N_m3u8DL_RE.DownloadManager
var files = FileDic.Values.Select(v => v!.ActualFilePath).OrderBy(s => s).ToArray(); var files = FileDic.Values.Select(v => v!.ActualFilePath).OrderBy(s => s).ToArray();
Logger.InfoMarkUp(ResString.ffmpegMerge); Logger.InfoMarkUp(ResString.ffmpegMerge);
var ext = streamSpec.MediaType == MediaType.AUDIO ? "m4a" : "mp4"; 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);
} }
} }

View File

@ -1,4 +1,6 @@
using System; using N_m3u8DL_RE.Common.Log;
using Spectre.Console;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; 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 fastStart = false,
bool writeDate = true, string poster = "", string audioName = "", string title = "", bool writeDate = true, string poster = "", string audioName = "", string title = "",
string copyright = "", string comment = "", string encodingTool = "", string recTime = "") 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")); Path.GetFileName(outputPath) + "_" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
} }
bool useAACFilter = true;
StringBuilder command = new StringBuilder("-loglevel warning -i concat:\""); StringBuilder command = new StringBuilder("-loglevel warning -i concat:\"");
string ddpAudio = string.Empty; string ddpAudio = string.Empty;
string addPoster = "-map 1 -c:v:1 copy -disposition:v:1 attached_pic"; string addPoster = "-map 1 -c:v:1 copy -disposition:v:1 attached_pic";
@ -109,7 +110,10 @@ namespace N_m3u8DL_RE.Util
break; break;
} }
Process.Start(new ProcessStartInfo() Logger.DebugMarkUp($"{binary}: {command}");
using var p = new Process();
p.StartInfo = new ProcessStartInfo()
{ {
WorkingDirectory = Path.GetDirectoryName(files[0]), WorkingDirectory = Path.GetDirectoryName(files[0]),
FileName = binary, FileName = binary,
@ -117,7 +121,17 @@ namespace N_m3u8DL_RE.Util
RedirectStandardOutput = true, RedirectStandardOutput = true,
RedirectStandardError = true, RedirectStandardError = true,
UseShellExecute = false 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) if (File.Exists($"{outputPath}.{muxFormat}") && new FileInfo($"{outputPath}.{muxFormat}").Length > 0)
return true; return true;