diff --git a/src/CasaBot/AutoScan/Implementations/DVRScanner.cs b/src/CasaBot/AutoScan/Implementations/DVRScanner.cs index a557d3f..444a777 100644 --- a/src/CasaBot/AutoScan/Implementations/DVRScanner.cs +++ b/src/CasaBot/AutoScan/Implementations/DVRScanner.cs @@ -28,7 +28,7 @@ public class DVRScanner : IDVRScanner { _logger.LogDebug("Scanning videos..."); var folderParam = Path.Combine(_options.MediaFolder!, "*.mp4"); - var arguments = $"-i {folderParam} -c {_options.Scanner.ConfigFile} --output-dir {_options.Scanner.DetectionFolder} --thumbnails highscore"; + var arguments = $"-i {folderParam} -c {_options.Scanner.ConfigFile} --quiet --logfile log.txt --output-dir {_options.Scanner.DetectionFolder} --thumbnails highscore"; _logger.LogDebug("Executing command: {_dvrScannerFile} {arguments}", _options.Scanner.Exe, arguments); var process = new Process { @@ -50,7 +50,7 @@ public class DVRScanner : IDVRScanner } _startedAt = DateTime.Now; process.Start(); - process.PriorityClass = ProcessPriorityClass.High; + // process.PriorityClass = ProcessPriorityClass.High; cancellationToken.Register(() => { _logger.LogDebug("DVR Process status: ID: {Id}, HasExited: {HasExited}, Responding: {Responding}", @@ -65,6 +65,8 @@ public class DVRScanner : IDVRScanner await UpdateProcessUntilExits(process, cancellationToken); _logger.LogInformation("Videos scanned successfully!"); + + RemoveVideoFiles(); } catch (Exception ex) { @@ -84,4 +86,17 @@ public class DVRScanner : IDVRScanner } } + + private void RemoveVideoFiles() + { + //remove .avi files from the detection folder + if (_options.Scanner?.DetectionFolder is not null && !_options.Scanner.RunDry) + { + _logger.LogDebug("Removing .avi files from detection folder {DetectionFolder}", _options.Scanner.DetectionFolder); + foreach (var file in Directory.GetFiles(_options.Scanner.DetectionFolder, "*.avi")) + { + File.Delete(file); + } + } + } } \ No newline at end of file diff --git a/src/CasaBot/AutoScan/Implementations/ShinobiConnector.cs b/src/CasaBot/AutoScan/Implementations/ShinobiConnector.cs index 4a6a6e8..ad438fc 100644 --- a/src/CasaBot/AutoScan/Implementations/ShinobiConnector.cs +++ b/src/CasaBot/AutoScan/Implementations/ShinobiConnector.cs @@ -20,13 +20,19 @@ public class ShinobiConnector : IDVRConnector _options = options.Value; } - public async Task> FetchMonitorVideosBetween(DateTime from, DateTime to, CancellationToken cancellationToken = default) + public async Task> FetchMonitorVideosBetween(DateTime from, DateTime to, bool runDry = false, CancellationToken cancellationToken = default) { var endpoint = $"{_options.URL}/{_options.APIKey}/videos/{_options.GroupId}/{_options.MonitorId}"; endpoint += $"?start={from:yyyy-MM-ddTHH:mm:sszzz}&end={to:yyyy-MM-ddTHH:mm:sszzz}"; _logger.LogDebug("Fetching videos details from endpoint: {Endpoint}", endpoint); + if(runDry) + { + _logger.LogInformation("RunDry is enabled, skipping video fetch"); + return []; + } + //get from the server the response with type VideoDetails var response = await _httpClient.GetFromJsonAsync(endpoint, cancellationToken); diff --git a/src/CasaBot/AutoScan/Interfaces/IDVRConnector.cs b/src/CasaBot/AutoScan/Interfaces/IDVRConnector.cs index 8fd1ffb..0ed2008 100644 --- a/src/CasaBot/AutoScan/Interfaces/IDVRConnector.cs +++ b/src/CasaBot/AutoScan/Interfaces/IDVRConnector.cs @@ -4,6 +4,6 @@ namespace AutoScan.Interfaces; public interface IDVRConnector { - Task> FetchMonitorVideosBetween(DateTime from, DateTime to, CancellationToken cancellationToken = default); + Task> FetchMonitorVideosBetween(DateTime from, DateTime to, bool runDry = false, CancellationToken cancellationToken = default); Task DownloadMonitorVideo(VideoDetail video, string downloadFolder, bool runDry = false, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/src/CasaBot/AutoScan/Jobs/DownloaderJob.cs b/src/CasaBot/AutoScan/Jobs/DownloaderJob.cs index 7b63a03..b24f9d8 100644 --- a/src/CasaBot/AutoScan/Jobs/DownloaderJob.cs +++ b/src/CasaBot/AutoScan/Jobs/DownloaderJob.cs @@ -50,7 +50,7 @@ public class DownloaderJob : IJob var to = new DateTime(now.Year, now.Month, now.Day, int.Parse(hours), int.Parse(minutes), 0); _logger.LogInformation("Fetching videos from {From} to {To}", from, to); - var videos = await _dvrConnector.FetchMonitorVideosBetween(from, to, context.CancellationToken); + var videos = await _dvrConnector.FetchMonitorVideosBetween(from, to, _options.RunDry, context.CancellationToken); //if the amount of videos is greater than the max amount in options, log a warning if (_options.MaxAmount > 0 && videos.Count > _options.MaxAmount) diff --git a/src/CasaBot/CasaBotApp/CasaBotApp.csproj b/src/CasaBot/CasaBotApp/CasaBotApp.csproj index a3bbc4f..8bb3efa 100644 --- a/src/CasaBot/CasaBotApp/CasaBotApp.csproj +++ b/src/CasaBot/CasaBotApp/CasaBotApp.csproj @@ -31,9 +31,12 @@ - + PreserveNewest + + PreserveNewest + diff --git a/src/CasaBot/CasaBotApp/Dockerfile b/src/CasaBot/CasaBotApp/Dockerfile index 34a12c2..0c3ffc8 100644 --- a/src/CasaBot/CasaBotApp/Dockerfile +++ b/src/CasaBot/CasaBotApp/Dockerfile @@ -17,13 +17,20 @@ RUN dotnet publish "CasaBotApp/CasaBotApp.csproj" -a $TARGETARCH --no-restore -o # Runtime stage FROM mcr.microsoft.com/dotnet/runtime:9.0 +#USER $APP_UID + +RUN apt-get update && apt-get install -y tzdata +ENV TZ=America/Argentina/Buenos_Aires +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +RUN mkdir -p /app/media && chmod -R 777 /app/media WORKDIR /app # I need to run this "python3 -m pip install dvr-scan[opencv]" install everything needed -RUN apt-get update && apt-get install -y python3 python3-pip +RUN apt-get install -y python3 python3-pip +#RUN apt-get update && apt-get install -y python3 python3-pip RUN python3 -m pip install dvr-scan[opencv-headless] --break-system-packages COPY --link --from=build /app . -USER $APP_UID ENTRYPOINT ["dotnet", "CasaBotApp.dll"] #CMD ["/bin/bash"] \ No newline at end of file diff --git a/src/CasaBot/CasaBotApp/appsettings.json b/src/CasaBot/CasaBotApp/appsettings.json index c667f11..96a6014 100644 --- a/src/CasaBot/CasaBotApp/appsettings.json +++ b/src/CasaBot/CasaBotApp/appsettings.json @@ -21,18 +21,22 @@ }, "AutoScan": { "Enabled": true, - "RunDry": true, + "RunDry": false, "At": "07:00", "FromDayBefore": false, - "From": "00:45", + "From": "01:00", "To": "05:00", "MaxAmount": 0, "MediaFolder": "./media/originals/", "Scanner": { - "Exe": "./dvr-scanner/dvr-scan.exe", - "ConfigFile": "./dvr-scanner/dvr-scan.cfg", + "ExeLinux": "dvr-scan", + "ExeWindows": "./dvr-scanner/dvr-scan.exe", + "Exe": "dvr-scan", + "ConfigFileLinux": "./dvr-scan.cfg", + "ConfigFileWindows": "./dvr-scanner/dvr-scan.cfg", + "ConfigFile": "./dvr-scan.cfg", "DetectionFolder": "./media/detections/", - "RunDry": true + "RunDry": false } } } \ No newline at end of file diff --git a/src/CasaBot/CasaBotApp/dvr-scan.cfg b/src/CasaBot/CasaBotApp/dvr-scan.cfg new file mode 100644 index 0000000..260325f --- /dev/null +++ b/src/CasaBot/CasaBotApp/dvr-scan.cfg @@ -0,0 +1,172 @@ +# +# This file contains all possible DVR-Scan config options. Lines beginning +# with a # are treated as comments and ignored. Remove them when setting a +# config value. +# +# A config file path can be specified via the -c/--config option. +# DVR-Scan also looks for a `dvr-scan.cfg` file in the following locations: +# +# Windows: C:/Users/%USERNAME%/AppData/Local/DVR-Scan/dvr-scan.cfg +# +# Linux: ~/.config/DVR-Scan/dvr-scan.cfg +# $XDG_CONFIG_HOME/dvr-scan.cfg +# +# Mac: ~/Library/Preferences/DVR-Scan/dvr-scan.cfg +# +# Run `dvr-scan --help` to see the exact path on your system which will be +# used (it will be listed under the help text for the -c/--config option). +# + +# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +# GENERAL +# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + +# Show region editor window (-r/--region-editor) before scanning. +#region-editor = no + +# Suppress all console output. +#quiet-mode = no + +# Verbosity of console output (debug, info, warning, error). +# If set to debug, overrides quiet-mode unless set via command line. +#verbosity = info + + +# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +# INPUT / OUTPUT +# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + +# Directory to output all created files. If unset, files will be created +# in the current working directory. +output-dir = .\media\detected + +# Use presentation time instead of frame number for timestamps. May improve +# timestamp accuracy with videos and network streams that may skip frames. +#use-pts = no + +# Method of generating output videos. Possible values are: SCAN_ONLY, OPENCV, +# FFMPEG, COPY. Not all features are supported in FFMPEG/COPY mode. +#output-mode = OPENCV +output-mode = OPENCV + + +# Arguments to add before the input when calling ffmpeg in output-mode FFMPEG +# or COPY. Note that `-y` and `-nostdin` are always added. +#ffmpeg-input-args = -v error + +# Encoder parameters used when generating output files in FFMPEG mode. +#ffmpeg-output-args = -map 0 -c:v libx264 -preset fast -crf 21 -c:a aac -sn + +# Four-letter identifier of the encoder/video codec to use in OPENCV mode. +# Possible values are: XVID, MP4V, MP42, H264 +opencv-codec = H264 + + +# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +# MOTION EVENTS +# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + +# Amount of time which must have motion in each frame to trigger an event. +min-event-length = 2.5s + +# Amount of time to include before an event. +time-before-event = 2s + +# Amount of time to include after an event. +time-post-event = 2s + + +# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +# DETECTION PARAMETERS +# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + +# Type of background subtraction to use, one of: MOG2, CNT, MOG2_CUDA +#bg-subtractor = MOG2 + +# Threshold representing amount of motion in a frame (or the ROI, if set) for +# a motion event to be triggered. Lower values require less movement, and are +# more sensitive to motion. If too high, some movement may not be detected, +# while too low of a threshold can result in false detection events. +threshold = 2 + +# Scores of this amount or higher are ignored. 255.0 is the maximum score, so +# values greater than 255.0 will disable the filter. +#max-threshold = 255.0 + +# Value between 0.0 and 1.0 indicating how fast the background model is updated, +# or set to -1 to set rate automatically based on history length. A value of 0.0 +# will not update the model at all, and 1.0 would re-initialize it on every frame. +#learning-rate = -1 + +# Threshold on the distance between the pixel and the model to decide whether +# a pixel is well described by the background model. This parameter does not affect +# the background update. +# The threshold is not used by the CNT subtractor. +#variance-threshold = 16 + +# Size (in pixels) of the noise reduction kernel. Can be odd integer starting +# from 3, 0 to disable, or -1 to auto-set using video resolution. +#kernel-size = -1 + +# Region file to limit detection areas. Can be created with the -r / --region-editor +# See < http://www.dvr-scan.com/guide/#region-editor > for an overview of the editor. +#load-region = roi.txt + +# Integer factor to shrink video before processing. Values <= 1 have no effect. +#downscale-factor = 0 + +# Number of frames to skip between processing when looking for motion events. +#frame-skip = 0 + + +# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +# OVERLAYS +# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + +# +# Text Overlays +# + +# Enable/disable timecode overlay in top-left of video frame. +#time-code = yes + +# Enable/disable frame metrics overlay in top right of video frame. +#frame-metrics = yes + +# Size of border around text, in pixels. Must be <= text-margin. +#text-border = 4 + +# Size of margin around border, in pixels. +#text-margin = 4 + +# Font scale factor. +#text-font-scale = 2.0 + +# Thickness of font (integer values only). +#text-font-thickness = 2 + +# Text color in the form (R,G,B) or 0xFFFFFF +#text-font-color = 255, 255, 255 + +# Text background color in the form (R,G,B) or 0xFFFFFF +#text-bg-color = 0, 0, 0 + + +# +# Bounding Box Overlay +# + +# Enable/disable overlay. +bounding-box = yes + +# Box edge color in the form (R,G,B) or 0xFFFFFF +#bounding-box-color = 255, 0, 0 + +# Thickness of bounding box, relative to largest edge of input video. +#bounding-box-thickness = 0.0032 + +# Amount of temporal smoothing to apply. +bounding-box-smooth-time = 0.1s + +# Minimum side length of bounding box, relative to largest edge of input video. +#bounding-box-min-size = 0.032