Added support for cached DxDiag output

This commit is contained in:
Mercurio 2024-10-07 22:47:43 +02:00
parent 507e17ec14
commit d6010162fc

View file

@ -26,12 +26,12 @@ class Program
private static string _gameProcessName = "spice64"; private static string _gameProcessName = "spice64";
private static string _encoder = null!; private static string _encoder = null!;
private static async Task Main(string[] args) private static async Task Main(string[] args)
{ {
DownloadFFmpeg(); DownloadFFmpeg();
LoadSettingsFromPropFile(); LoadSettingsFromPropFile();
_encoder = GetHardwareEncoder(); _encoder = GetHardwareEncoder();
var gameProcesses = Process.GetProcessesByName(_gameProcessName); var gameProcesses = Process.GetProcessesByName(_gameProcessName);
if (gameProcesses.Length > 0) if (gameProcesses.Length > 0)
{ {
@ -46,7 +46,7 @@ class Program
private static void DownloadFFmpeg() private static void DownloadFFmpeg()
{ {
const string ffmpegExe = "ffmpeg.exe"; 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)) if (File.Exists(ffmpegExe))
{ {
@ -83,8 +83,8 @@ class Program
var path = line["path:".Length..].Trim(); var path = line["path:".Length..].Trim();
if (Directory.Exists(path)) if (Directory.Exists(path))
{ {
_ffmpegFolderPath = path; // Recording path _ffmpegFolderPath = path;
} }
else else
{ {
@ -107,7 +107,7 @@ class Program
_crf = float.Parse(line["crf:".Length..].Trim()); _crf = float.Parse(line["crf:".Length..].Trim());
Console.WriteLine($"custom crf: {_crf}"); Console.WriteLine($"custom crf: {_crf}");
} }
if (line.StartsWith("game_process_name:")) if (line.StartsWith("game_process_name:"))
{ {
_gameProcessName = line["game_process_name:".Length..].Trim(); _gameProcessName = line["game_process_name:".Length..].Trim();
Console.WriteLine($"custom process name: {_gameProcessName}"); Console.WriteLine($"custom process name: {_gameProcessName}");
@ -260,96 +260,123 @@ class Program
} }
private static void StartFfmpegRecording(string songName) 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", StartInfo = new ProcessStartInfo
Arguments = ffmpegArguments, {
UseShellExecute = false, FileName = "ffmpeg",
RedirectStandardError = true, Arguments = ffmpegArguments,
CreateNoWindow = true UseShellExecute = false,
} RedirectStandardError = true,
}; CreateNoWindow = true
_ffmpegProcess.Start(); }
};
_ffmpegProcess.Start();
Console.WriteLine("FFmpeg recording started."); Console.WriteLine("FFmpeg recording started.");
} }
private static string GetGraphicsCard() private static string GetGraphicsCard()
{ {
string dxDiagOutput = RunDxDiag(); string dxDiagOutput = GetDxDiagOutput();
string graphicsCard = ParseGraphicsCard(dxDiagOutput); string graphicsCard = ParseGraphicsCard(dxDiagOutput);
return graphicsCard.ToUpper(); 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 Process dxDiagProcess = new Process
{ {
StartInfo = new ProcessStartInfo StartInfo = new ProcessStartInfo
{ {
FileName = "dxdiag", FileName = "dxdiag",
Arguments = "/t dxdiag_output.txt", Arguments = $"/t {dxDiagFilePath}",
UseShellExecute = false, UseShellExecute = false,
RedirectStandardOutput = false, RedirectStandardOutput = false,
CreateNoWindow = true 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.Start();
dxDiagProcess.WaitForExit(); dxDiagProcess.WaitForExit();
return File.ReadAllText("dxdiag_output.txt");
return File.ReadAllText(dxDiagFilePath);
} }
private static string ParseGraphicsCard(string dxDiagOutput) private static string ParseGraphicsCard(string dxDiagOutput)
{ {
string pattern = @"Card name:\s*(.*)"; string pattern = @"Card name:\s*(.*)";
Match match = Regex.Match(dxDiagOutput, pattern); Match match = Regex.Match(dxDiagOutput, pattern);
if (match.Success) if (match.Success)
{ {
return match.Groups[1].Value; return match.Groups[1].Value;
} }
return "Unknown"; return "Unknown";
} }
private static string GetHardwareEncoder()
private static string GetHardwareEncoder() {
{ var graphicsCard = GetGraphicsCard();
var graphicsCard = GetGraphicsCard(); Console.WriteLine($"Using {graphicsCard} for hardware video acceleration");
Console.WriteLine($"Using {graphicsCard} for hardware video acceleration"); var encoder = "-c:v libx264";
var encoder = "-c:v libx264";
if (graphicsCard.Contains("NVIDIA")) if (graphicsCard.Contains("NVIDIA"))
{ {
encoder = "-c:v h264_nvenc"; encoder = "-c:v h264_nvenc";
Console.WriteLine("Using NVIDIA hardware encoding (h264_nvenc)."); Console.WriteLine("Using NVIDIA hardware encoding (h264_nvenc).");
} }
else if (graphicsCard.Contains("AMD")) else if (graphicsCard.Contains("AMD"))
{ {
encoder = "-c:v h264_amf"; encoder = "-c:v h264_amf";
Console.WriteLine("Using AMD hardware encoding (h264_amf)."); Console.WriteLine("Using AMD hardware encoding (h264_amf).");
} }
else if (graphicsCard.Contains("INTEL")) else if (graphicsCard.Contains("INTEL"))
{ {
encoder = "-c:v h264_qsv"; encoder = "-c:v h264_qsv";
Console.WriteLine("Using Intel hardware encoding (h264_qsv)."); Console.WriteLine("Using Intel hardware encoding (h264_qsv).");
} }
else else
{ {
Console.WriteLine("No recognized hardware encoder found, using CPU (libx264).");
}
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) private static void StopRecording(string songName)
{ {
@ -409,4 +436,4 @@ private static string GetHardwareEncoder()
File.Delete(videoFilePath); File.Delete(videoFilePath);
File.Delete(audioFilePath); File.Delete(audioFilePath);
} }
} }