casa-bot/src/CasaBot/CasaBotApp/DVRScanner.cs

64 lines
2.2 KiB
C#

using Microsoft.Extensions.Logging;
using System.Diagnostics;
namespace CasaBotApp;
public class DVRScanner
{
private readonly ILogger<DVRScanner> _logger;
private const string _mediaPath = @".\media"; // Replace with your desired file path
private const string _dvrScannerFile = @".\dvr-scanner\dvr-scan.exe";
private const string _dvrScannerConfig = @".\dvr-scanner\dvr-scan.cfg";
//.\dvr-scanner\dvr-scan.exe -i .\media\*.mp4 -c .\dvr-scanner\dvr-scan.cfg
public DVRScanner(ILogger<DVRScanner> logger)
{
_logger = logger;
}
public async Task ScanVideos(CancellationToken cancellationToken = default)
{
try
{
//make sure the directory exists
Directory.CreateDirectory(Path.GetDirectoryName(_mediaPath)!);
_logger.LogDebug("Scanning videos...");
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = _dvrScannerFile,
Arguments = $"-i {_mediaPath}\\*.mp4 -c {_dvrScannerConfig}",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
cancellationToken.Register(() =>
{
_logger.LogDebug("DVR Process status: ID: {Id}, HasExited: {HasExited}, Responding: {Responding}",
process.Id, process.HasExited, process.Responding);
if(process.HasExited) return;
_logger.LogWarning("DVR Process is still running, killing it...");
process.Kill();
});
_logger.LogDebug("DVR Process started with ID: {Id}", process.Id);
await process.WaitForExitAsync(cancellationToken);
_logger.LogInformation("Videos scanned successfully!");
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while scanning the videos");
}
}
}