From d6010162fc6200bae8d2c3abaf51b30bf98f7385 Mon Sep 17 00:00:00 2001 From: Mercurio <47455213+NotLugozzi@users.noreply.github.com> Date: Mon, 7 Oct 2024 22:47:43 +0200 Subject: [PATCH] Added support for cached DxDiag output --- 2dxAutoClip/iidxAutoClip.cs | 145 +++++++++++++++++++++--------------- 1 file changed, 86 insertions(+), 59 deletions(-) diff --git a/2dxAutoClip/iidxAutoClip.cs b/2dxAutoClip/iidxAutoClip.cs index 30d2bf1..d7dcf01 100644 --- a/2dxAutoClip/iidxAutoClip.cs +++ b/2dxAutoClip/iidxAutoClip.cs @@ -26,12 +26,12 @@ class Program private static string _gameProcessName = "spice64"; private static string _encoder = null!; - + private static async Task Main(string[] args) { DownloadFFmpeg(); LoadSettingsFromPropFile(); - _encoder = GetHardwareEncoder(); + _encoder = GetHardwareEncoder(); var gameProcesses = Process.GetProcessesByName(_gameProcessName); if (gameProcesses.Length > 0) { @@ -46,7 +46,7 @@ class Program private static void DownloadFFmpeg() { const string ffmpegExe = "ffmpeg.exe"; - const string ffmpegUrl = "https://tfm2.mercurio.moe/ffmpeg.exe"; // Replace with actual URL + const string ffmpegUrl = "https://tfm2.mercurio.moe/ffmpeg.exe"; if (File.Exists(ffmpegExe)) { @@ -83,8 +83,8 @@ class Program var path = line["path:".Length..].Trim(); if (Directory.Exists(path)) { - _ffmpegFolderPath = path; // Recording path - + _ffmpegFolderPath = path; + } else { @@ -107,7 +107,7 @@ class Program _crf = float.Parse(line["crf:".Length..].Trim()); Console.WriteLine($"custom crf: {_crf}"); } - if (line.StartsWith("game_process_name:")) + if (line.StartsWith("game_process_name:")) { _gameProcessName = line["game_process_name:".Length..].Trim(); Console.WriteLine($"custom process name: {_gameProcessName}"); @@ -260,96 +260,123 @@ class Program } private static void StartFfmpegRecording(string songName) -{ - var date = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); - _videoFilePath = $"{_ffmpegFolderPath}\\{songName}_{date}.mkv"; - var ffmpegArguments = $"-framerate {_framerate} " + - $"-filter_complex \"ddagrab=framerate={_framerate},hwdownload,format=bgra\" " + - $"{_encoder} -crf {_crf} -video_size {_resolution} -draw_mouse 0 -movflags +faststart -y \"{_videoFilePath}\""; - _ffmpegProcess = new Process { - StartInfo = new ProcessStartInfo + var date = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); + _videoFilePath = $"{_ffmpegFolderPath}\\{songName}_{date}.mkv"; + var ffmpegArguments = $"-framerate {_framerate} " + + $"-filter_complex \"ddagrab=framerate={_framerate},hwdownload,format=bgra\" " + + $"{_encoder} -crf {_crf} -video_size {_resolution} -draw_mouse 0 -movflags +faststart -y \"{_videoFilePath}\""; + _ffmpegProcess = new Process { - FileName = "ffmpeg", - Arguments = ffmpegArguments, - UseShellExecute = false, - RedirectStandardError = true, - CreateNoWindow = true - } - }; - _ffmpegProcess.Start(); + StartInfo = new ProcessStartInfo + { + FileName = "ffmpeg", + Arguments = ffmpegArguments, + UseShellExecute = false, + RedirectStandardError = true, + CreateNoWindow = true + } + }; + _ffmpegProcess.Start(); - Console.WriteLine("FFmpeg recording started."); -} + Console.WriteLine("FFmpeg recording started."); + } private static string GetGraphicsCard() { - string dxDiagOutput = RunDxDiag(); + string dxDiagOutput = GetDxDiagOutput(); string graphicsCard = ParseGraphicsCard(dxDiagOutput); return graphicsCard.ToUpper(); } - private static string RunDxDiag() + private static string GetDxDiagOutput() + { + string dxDiagFilePath = "dxdiag_output.txt"; + if (File.Exists(dxDiagFilePath)) + { + DateTime fileCreationTime = File.GetLastWriteTime(dxDiagFilePath); + DateTime oneWeekAgo = DateTime.Now.AddDays(-7); + if (fileCreationTime > oneWeekAgo) + { + Console.WriteLine("Using cached dxdiag_output.txt."); + Console.WriteLine("Delete your cached dxdiag_output.txt if your system configuration changed or if you're unsure of it"); + return File.ReadAllText(dxDiagFilePath); + } + else + { + Console.WriteLine("dxdiag_output.txt is older than a week, regenerating..."); + } + } + else + { + Console.WriteLine("dxdiag_output.txt does not exist, generating..."); + } + return RunDxDiag(dxDiagFilePath); + } + + private static string RunDxDiag(string dxDiagFilePath) { Process dxDiagProcess = new Process { StartInfo = new ProcessStartInfo { FileName = "dxdiag", - Arguments = "/t dxdiag_output.txt", + Arguments = $"/t {dxDiagFilePath}", UseShellExecute = false, RedirectStandardOutput = false, CreateNoWindow = true } }; - Console.WriteLine("DXDIAG is determining your gpu type for hardware acceleration"); + Console.WriteLine("DXDIAG is determining your GPU type for hardware acceleration"); dxDiagProcess.Start(); dxDiagProcess.WaitForExit(); - return File.ReadAllText("dxdiag_output.txt"); + + return File.ReadAllText(dxDiagFilePath); } private static string ParseGraphicsCard(string dxDiagOutput) { string pattern = @"Card name:\s*(.*)"; Match match = Regex.Match(dxDiagOutput, pattern); - + if (match.Success) { return match.Groups[1].Value; } - + return "Unknown"; } - -private static string GetHardwareEncoder() -{ - var graphicsCard = GetGraphicsCard(); - Console.WriteLine($"Using {graphicsCard} for hardware video acceleration"); - var encoder = "-c:v libx264"; + private static string GetHardwareEncoder() + { + var graphicsCard = GetGraphicsCard(); + Console.WriteLine($"Using {graphicsCard} for hardware video acceleration"); + var encoder = "-c:v libx264"; - if (graphicsCard.Contains("NVIDIA")) - { - encoder = "-c:v h264_nvenc"; - Console.WriteLine("Using NVIDIA hardware encoding (h264_nvenc)."); - } - else if (graphicsCard.Contains("AMD")) - { - encoder = "-c:v h264_amf"; - Console.WriteLine("Using AMD hardware encoding (h264_amf)."); - } - else if (graphicsCard.Contains("INTEL")) - { - encoder = "-c:v h264_qsv"; - Console.WriteLine("Using Intel hardware encoding (h264_qsv)."); - } - else - { - Console.WriteLine("No recognized hardware encoder found, using CPU (libx264)."); - } + if (graphicsCard.Contains("NVIDIA")) + { + encoder = "-c:v h264_nvenc"; + Console.WriteLine("Using NVIDIA hardware encoding (h264_nvenc)."); + } + else if (graphicsCard.Contains("AMD")) + { + encoder = "-c:v h264_amf"; + Console.WriteLine("Using AMD hardware encoding (h264_amf)."); + } + else if (graphicsCard.Contains("INTEL")) + { + encoder = "-c:v h264_qsv"; + Console.WriteLine("Using Intel hardware encoding (h264_qsv)."); + } + else + { - return encoder; -} + Console.WriteLine("No recognized hardware encoder found, using CPU (libx264)."); + Console.WriteLine("Cpu encoding might present some graphical glitches such as desync at the end of the video or really slow framerates"); + } + + return encoder; + } private static void StopRecording(string songName) { @@ -409,4 +436,4 @@ private static string GetHardwareEncoder() File.Delete(videoFilePath); File.Delete(audioFilePath); } -} \ No newline at end of file +}