new record

This commit is contained in:
辉鸭蛋
2025-01-05 02:38:41 +08:00
parent bc1dfa8f12
commit 8f91858b8d
7 changed files with 189 additions and 18 deletions

View File

@@ -10,7 +10,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ApplicationIcon>Assets\Images\logo.ico</ApplicationIcon>
<AssemblyName>BetterGI</AssemblyName>
<AssemblyVersion>10.38.6</AssemblyVersion>
<AssemblyVersion>10.38.7</AssemblyVersion>
<Platforms>x64</Platforms>
<DebugType>embedded</DebugType>
</PropertyGroup>

View File

@@ -14,6 +14,7 @@ using System.Threading.Tasks;
using System.Windows.Forms;
using BetterGenshinImpact.Core.Config;
using BetterGenshinImpact.Core.Video;
using BetterGenshinImpact.Core.Video.obs;
using NAudio.Wave;
using SharpAvi;
using Vanara.PInvoke;
@@ -82,6 +83,7 @@ public class GlobalKeyMouseRecord : Singleton<GlobalKeyMouseRecord>
_videoRecorder = VideoRecorderFactory.Create(TaskContext.Instance().Config.CommonConfig.Recorder, fileName);
_directInputMonitor = new DirectInputMonitor();
var recorderTmp = new KeyMouseRecorderJsonLine(fileName);
// TaskTriggerDispatcher.Instance().StopTimer();
@@ -98,10 +100,26 @@ public class GlobalKeyMouseRecord : Singleton<GlobalKeyMouseRecord>
}
var videoEnabled = _videoRecorder.Start();
var tmpTime = DateTime.Now;
var sw = new Stopwatch();
sw.Start();
long current;
_directInputMonitor.Start();
_recorder = new KeyMouseRecorderJsonLine(fileName);
_logger.LogInformation("从开始到directInput启动累计耗时ms{Text}", sw.ElapsedMilliseconds);
var videoEnabled = _videoRecorder.Start();
_logger.LogInformation("从开始到视频录制启动累计耗时OBS是WS通知完成耗时并非实际录制时间ms{Text}", sw.ElapsedMilliseconds);
_recorder = recorderTmp.Start();
_recorder.AppendStartInfo();
_logger.LogInformation("从开始到键鼠录制启动累计耗时ms{Text}", sw.ElapsedMilliseconds);
sw.Stop();
ObsLogFileProcessor.FindStartTime(fileName, tmpTime);
Status = KeyMouseRecorderStatus.Recording;
@@ -115,13 +133,18 @@ public class GlobalKeyMouseRecord : Singleton<GlobalKeyMouseRecord>
throw new InvalidOperationException("未处于录制中状态,无法停止");
}
var sw = new Stopwatch();
sw.Start();
// var macro = _recorder?.ToJsonMacro() ?? string.Empty;
_recorder = null;
_directInputMonitor?.Stop();
_directInputMonitor?.Dispose();
_directInputMonitor = null;
_logger.LogInformation("从结束触发到键鼠录制关闭累计耗时ms{Text}", sw.ElapsedMilliseconds);
_videoRecorder?.Stop();
_logger.LogInformation("从结束触发到视频录制关闭累计耗时ms{Text}", sw.ElapsedMilliseconds);
if (_timer.Enabled)
{
@@ -182,7 +205,6 @@ public class GlobalKeyMouseRecord : Singleton<GlobalKeyMouseRecord>
public void GlobalHookKeyUp(KeyEventArgsExt e)
{
if (_keyDownState.TryGetValue(e.KeyCode, out bool state) && state)
{
// Debug.WriteLine($"KeyUp: {e.KeyCode}");

View File

@@ -42,11 +42,12 @@ public class KeyMouseRecorderJsonLine
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
string _path;
public KeyMouseRecorderJsonLine(string folderName)
{
var path = Global.Absolute($@"User\KeyMouseScript\{folderName}\");
_path = Global.Absolute($@"User\KeyMouseScript\{folderName}\");
DateTime startTime = DateTime.Now;
// var bootTime = EnvironmentUtil.LastBootUpTime();
// if (bootTime == null)
// {
@@ -65,14 +66,28 @@ public class KeyMouseRecorderJsonLine
Width = rect.Width,
Height = rect.Height,
RecordDpi = TaskContext.Instance().DpiScale,
StartTime = $"{startTime:yyyy-MM-dd HH:mm:ss:ffff}",
StartTimeUnixTimestamp = (startTime.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalNanoseconds.ToString("F0"),
SysParams = RecordContext.Instance.SysParams
SysParams = RecordContext.Instance.SysParams
};
var infoJson = JsonSerializer.Serialize(Info, JsonOptions);
File.WriteAllText(Path.Combine(path, $"systemInfo.json"), infoJson);
_consumerTask = Task.Run(async () => await ConsumeEventsAsync(path));
_consumerTask = Task.Run(async () => await ConsumeEventsAsync(_path));
}
public KeyMouseRecorderJsonLine Start()
{
StartTick = Kernel32.GetTickCount();
return this;
}
public void AppendStartInfo()
{
Task.Run(() =>
{
DateTime startTime = DateTime.Now;
Info.StartTime = $"{startTime:yyyy-MM-dd HH:mm:ss:ffff}";
Info.StartTimeUnixTimestamp = (startTime.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalNanoseconds.ToString("F0");
var infoJson = JsonSerializer.Serialize(Info, JsonOptions);
File.WriteAllText(Path.Combine(_path, $"systemInfo.json"), infoJson);
});
}
private async Task ConsumeEventsAsync(string path)

View File

@@ -106,7 +106,7 @@ public class ObsRecorder : IVideoRecorder
{
obs.StartRecord();
_lastRecordTime = DateTime.Now;
TaskControl.Logger.LogInformation("OBS: 开始录制,时间: {Time}", _lastRecordTime.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
TaskControl.Logger.LogInformation("OBS: ws请求开始录制,时间: {Time}", _lastRecordTime.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
return true;
}
catch (ErrorResponseException ex)
@@ -118,7 +118,7 @@ public class ObsRecorder : IVideoRecorder
}
else
{
TaskControl.Logger.LogError($"OBS: 开始录制失败: {ex.Message}");
TaskControl.Logger.LogError($"OBS: ws请求开始录制失败: {ex.Message}");
throw;
}
}
@@ -187,7 +187,7 @@ public class ObsRecorder : IVideoRecorder
TaskControl.Logger.LogError("OBS: 未找到录制结果文件");
}
File.WriteAllText(Path.Combine(folderPath, "videoStartTime.txt"), (_lastRecordTime.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalNanoseconds.ToString("F0"));
// File.WriteAllText(Path.Combine(folderPath, "videoStartTime.txt"), (_lastRecordTime.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalNanoseconds.ToString("F0"));
}
catch (Exception e)
{

View File

@@ -0,0 +1,133 @@
using System;
using System.IO;
using System.Threading.Tasks;
using BetterGenshinImpact.Core.Config;
using BetterGenshinImpact.GameTask.Common;
using Microsoft.Extensions.Logging;
namespace BetterGenshinImpact.Core.Video.obs;
public class ObsLogFileProcessor
{
private static readonly string ObsLogPath = Global.Absolute(@"video\bin\OBS-Studio-31.0.0-Windows\config\obs-studio\logs");
public static FileInfo? GetLogFilePath()
{
// 找到目录中最新的日志文件
if (!Directory.Exists(ObsLogPath))
{
TaskControl.Logger.LogError("OBS日志目录不存在");
return null;
}
var files = Directory.GetFiles(ObsLogPath, "*.txt");
if (files.Length == 0)
{
TaskControl.Logger.LogError("OBS日志文件不存在");
return null;
}
FileInfo? latestFile = null;
foreach (var file in files)
{
var fileInfo = new FileInfo(file);
if (latestFile == null || fileInfo.LastWriteTime > latestFile.LastWriteTime)
{
latestFile = fileInfo;
}
}
TaskControl.Logger.LogDebug("当前最新的OBS日志文件:{Path}", latestFile?.FullName);
return latestFile;
}
public static async Task<DateTime?> ProcessLog(DateTime? beforeStartTime)
{
var logFile = GetLogFilePath();
if (logFile == null)
{
return null;
}
// 以共享的方式读取日志文件
string content;
try
{
await using var fileStream = new FileStream(logFile.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var reader = new StreamReader(fileStream);
content = await reader.ReadToEndAsync();
}
catch (IOException e)
{
TaskControl.Logger.LogError("OBS日志文件读取失败:{Msg}", e.Message);
return null;
}
// 从下网上找到最近一次 = Recording Start = 关键词的行
var lines = content.Split('\n');
string? lastLine = null;
for (var i = lines.Length - 1; i >= 0; i--)
{
if (lines[i].Contains("= Recording Start ="))
{
if (beforeStartTime != null)
{
var timeStr = lines[i].Substring(0, 12);
if (DateTime.TryParse(timeStr, out var time))
{
if (time >= beforeStartTime)
{
lastLine = lines[i];
return time;
}
}
}
else
{
lastLine = lines[i];
break;
}
}
}
if (lastLine == null)
{
TaskControl.Logger.LogDebug("未找到OBS日志中录制开始关键词");
return null;
}
// 内容应该是 16:19:40.665: ==== Recording Start ===============================================
var timeStr2 = lastLine.Substring(0, 12);
// 16:19:40.665 转换到datetime
if (DateTime.TryParse(timeStr2, out var time2))
{
return time2;
}
return null;
}
public static void FindStartTime(string fileName, DateTime beforeStartTime)
{
Task.Run(async () =>
{
for (int i = 0; i < 10; i++)
{
await Task.Delay(1000);
var time = await ProcessLog(beforeStartTime);
if (time != null)
{
var t = time.Value;
TaskControl.Logger.LogInformation("OBS实际录制开始时间来自log:{Time}", t.ToString("yyyy-MM-dd HH:mm:s.ffff"));
var folderPath = Global.Absolute($@"User\KeyMouseScript\{fileName}\");
await File.WriteAllTextAsync(Path.Combine(folderPath, "videoStartTime.txt"), (t.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalNanoseconds.ToString("F0"));
break;
}
else
{
TaskControl.Logger.LogDebug("未找到OBS实际录制开始时间等待1s后重试");
}
}
});
}
}

View File

@@ -324,7 +324,7 @@ public class GenshinGameSettings
}
var str = Encoding.UTF8.GetString(rawBytes);
Debug.WriteLine(str);
// Debug.WriteLine(str);
return str;
}

View File

@@ -6,6 +6,7 @@ using System.Threading;
using System.Threading.Tasks;
using BetterGenshinImpact.Core.Config;
using BetterGenshinImpact.Core.Video;
using BetterGenshinImpact.Core.Video.obs;
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.GameTask.Common;
using BetterGenshinImpact.Genshin.Settings;
@@ -65,7 +66,7 @@ public class StartEndSingleton : Singleton<StartEndSingleton>
// // 退出
// Environment.Exit(0);
// }
TosClientHelper.Instance.Test(Global.Absolute(@$"User/config.json"));
// TosClientHelper.Instance.Test(Global.Absolute(@$"User/config.json"));
}