From 7e39a1f88e0557bb10b6a23f84962036a2bb47ea Mon Sep 17 00:00:00 2001 From: Elwador <75888166+Elwador@users.noreply.github.com> Date: Wed, 24 Jul 2024 15:19:18 +0200 Subject: [PATCH] Add - Added setting to also include specials in the history operations Fix - Fixed delay for ffmpeg mp4 Fix - Fixed delay for forced subtitles --- .../Crunchyroll/CrunchyrollManager.cs | 4 +- CRD/Utils/Muxing/Merger.cs | 23 +++--- CRD/Utils/Structs/CrDownloadOptions.cs | 3 + CRD/Utils/Structs/History/HistorySeries.cs | 77 +++++++++++++++---- CRD/ViewModels/SettingsPageViewModel.cs | 5 ++ CRD/Views/SettingsPageView.axaml | 6 ++ 6 files changed, 89 insertions(+), 29 deletions(-) diff --git a/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs b/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs index ad1b08e..69902aa 100644 --- a/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs +++ b/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs @@ -357,13 +357,13 @@ public class CrunchyrollManager{ var delay = await merger.ProcessVideo(basePath, syncVideo.Path); var audio = merger.options.OnlyAudio.FirstOrDefault(audio => audio.Language.CrLocale == syncVideo.Lang.CrLocale); if (audio != null){ - audio.Delay = (int)delay * 1000; + audio.Delay = (int)(delay * 1000); } var subtitles = merger.options.Subtitles.Where(a => a.RelatedVideoDownloadMedia == syncVideo).ToList(); if (subtitles.Count > 0){ foreach (var subMergerInput in subtitles){ - subMergerInput.Delay = (int)delay * 1000; + subMergerInput.Delay = (int)(delay * 1000); } } } diff --git a/CRD/Utils/Muxing/Merger.cs b/CRD/Utils/Muxing/Merger.cs index 4cd9dfa..7dcb1c9 100644 --- a/CRD/Utils/Muxing/Merger.cs +++ b/CRD/Utils/Muxing/Merger.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; using System.IO; using System.Linq; using System.Text.RegularExpressions; @@ -47,7 +48,8 @@ public class Merger{ foreach (var aud in options.OnlyAudio){ if (aud.Delay != null && aud.Delay != 0){ - args.Add($"-itsoffset {aud.Delay}"); + double delay = aud.Delay / 1000.0 ?? 0; + args.Add($"-itsoffset {delay.ToString(CultureInfo.InvariantCulture)}"); } args.Add($"-i \"{aud.Path}\""); @@ -66,8 +68,9 @@ public class Merger{ } foreach (var sub in options.Subtitles.Select((value, i) => new{ value, i })){ - if (sub.value.Delay != null){ - args.Add($"-itsoffset -{Math.Ceiling((double)sub.value.Delay * 1000)}ms"); + if (sub.value.Delay != null && sub.value.Delay != 0){ + double delay = sub.value.Delay / 1000.0 ?? 0; + args.Add($"-itsoffset {delay.ToString(CultureInfo.InvariantCulture)}"); } args.Add($"-i \"{sub.value.File}\""); @@ -178,7 +181,7 @@ public class Merger{ foreach (var subObj in options.Subtitles){ if (subObj.Delay.HasValue){ double delay = subObj.Delay ?? 0; - args.Add($"--sync 0:-{Math.Ceiling(delay * 1000)}"); + args.Add($"--sync 0:{delay}"); } string trackNameExtra = subObj.ClosedCaption == true ? $" {options.CcTag}" : ""; @@ -242,15 +245,13 @@ public class Merger{ Console.Error.WriteLine("Failed to extract Frames to Compare"); return 0; } - - var baseFrames = Directory.GetFiles(baseFramesDir).Select(fp => new FrameData - { + + var baseFrames = Directory.GetFiles(baseFramesDir).Select(fp => new FrameData{ FilePath = fp, Time = GetTimeFromFileName(fp, extractFramesBase.frameRate) }).ToList(); - var compareFrames = Directory.GetFiles(compareFramesDir).Select(fp => new FrameData - { + var compareFrames = Directory.GetFiles(compareFramesDir).Select(fp => new FrameData{ FilePath = fp, Time = GetTimeFromFileName(fp, extractFramesBase.frameRate) }).ToList(); @@ -320,8 +321,6 @@ public class Merger{ // Delete subtitle files options.Subtitles.ForEach(subtitle => Helpers.DeleteFile(subtitle.File)); } - - } public class MergerInput{ @@ -338,7 +337,7 @@ public class SubtitleInput{ public bool? ClosedCaption{ get; set; } public bool? Signs{ get; set; } public int? Delay{ get; set; } - + public DownloadedMedia? RelatedVideoDownloadMedia; } diff --git a/CRD/Utils/Structs/CrDownloadOptions.cs b/CRD/Utils/Structs/CrDownloadOptions.cs index c48384a..3640dd8 100644 --- a/CRD/Utils/Structs/CrDownloadOptions.cs +++ b/CRD/Utils/Structs/CrDownloadOptions.cs @@ -150,6 +150,9 @@ public class CrDownloadOptions{ [YamlMember(Alias = "history_lang", ApplyNamingConventions = false)] public string? HistoryLang{ get; set; } + [YamlMember(Alias = "history_add_specials", ApplyNamingConventions = false)] + public bool HistoryAddSpecials{ get; set; } + [YamlMember(Alias = "sonarr_properties", ApplyNamingConventions = false)] public SonarrProperties? SonarrProperties{ get; set; } diff --git a/CRD/Utils/Structs/History/HistorySeries.cs b/CRD/Utils/Structs/History/HistorySeries.cs index 909917f..3d8a315 100644 --- a/CRD/Utils/Structs/History/HistorySeries.cs +++ b/CRD/Utils/Structs/History/HistorySeries.cs @@ -186,25 +186,49 @@ public class HistorySeries : INotifyPropertyChanged{ public void UpdateNewEpisodes(){ int count = 0; bool foundWatched = false; + var historyAddSpecials = CrunchyrollManager.Instance.CrunOptions.HistoryAddSpecials; + + for (int i = Seasons.Count - 1; i >= 0; i--){ + var season = Seasons[i]; + + if (season.SpecialSeason == true){ + if (historyAddSpecials){ + var episodes = season.EpisodesList; + for (int j = episodes.Count - 1; j >= 0; j--){ + if (!episodes[j].WasDownloaded){ + count++; + } + } + } - // Iterate over the Seasons list from the end to the beginning - for (int i = Seasons.Count - 1; i >= 0 && !foundWatched; i--){ - if (Seasons[i].SpecialSeason == true){ continue; } - // Iterate over the Episodes from the end to the beginning - for (int j = Seasons[i].EpisodesList.Count - 1; j >= 0 && !foundWatched; j--){ - if (Seasons[i].EpisodesList[j].SpecialEpisode){ + var episodesList = season.EpisodesList; + for (int j = episodesList.Count - 1; j >= 0; j--){ + var episode = episodesList[j]; + + if (episode.SpecialEpisode){ + if (historyAddSpecials && !episode.WasDownloaded){ + count++; + } + continue; } - if (!Seasons[i].EpisodesList[j].WasDownloaded){ + if (!episode.WasDownloaded && !foundWatched){ count++; } else{ foundWatched = true; + if (!historyAddSpecials){ + break; + } } } + + if (foundWatched && !historyAddSpecials){ + break; + } } NewEpisodes = count; @@ -218,26 +242,49 @@ public class HistorySeries : INotifyPropertyChanged{ public async Task AddNewMissingToDownloads(){ bool foundWatched = false; + var historyAddSpecials = CrunchyrollManager.Instance.CrunOptions.HistoryAddSpecials; + + for (int i = Seasons.Count - 1; i >= 0; i--){ + var season = Seasons[i]; + + if (season.SpecialSeason == true){ + if (historyAddSpecials){ + var episodes = season.EpisodesList; + for (int j = episodes.Count - 1; j >= 0; j--){ + if (!episodes[j].WasDownloaded){ + await Seasons[i].EpisodesList[j].DownloadEpisode(); + } + } + } - // Iterate over the Seasons list from the end to the beginning - for (int i = Seasons.Count - 1; i >= 0 && !foundWatched; i--){ - if (Seasons[i].SpecialSeason == true){ continue; } - // Iterate over the Episodes from the end to the beginning - for (int j = Seasons[i].EpisodesList.Count - 1; j >= 0 && !foundWatched; j--){ - if (Seasons[i].EpisodesList[j].SpecialEpisode){ + var episodesList = season.EpisodesList; + for (int j = episodesList.Count - 1; j >= 0; j--){ + var episode = episodesList[j]; + + if (episode.SpecialEpisode){ + if (historyAddSpecials && !episode.WasDownloaded){ + await Seasons[i].EpisodesList[j].DownloadEpisode(); + } + continue; } - if (!Seasons[i].EpisodesList[j].WasDownloaded){ - //ADD to download queue + if (!episode.WasDownloaded && !foundWatched){ await Seasons[i].EpisodesList[j].DownloadEpisode(); } else{ foundWatched = true; + if (!historyAddSpecials){ + break; + } } } + + if (foundWatched && !historyAddSpecials){ + break; + } } } diff --git a/CRD/ViewModels/SettingsPageViewModel.cs b/CRD/ViewModels/SettingsPageViewModel.cs index 327e667..867d5d2 100644 --- a/CRD/ViewModels/SettingsPageViewModel.cs +++ b/CRD/ViewModels/SettingsPageViewModel.cs @@ -69,6 +69,9 @@ public partial class SettingsPageViewModel : ViewModelBase{ [ObservableProperty] private bool _history; + + [ObservableProperty] + private bool _historyAddSpecials; [ObservableProperty] private double? _leadingNumbers; @@ -377,6 +380,7 @@ public partial class SettingsPageViewModel : ViewModelBase{ AddScaledBorderAndShadow = options.SubsAddScaledBorder is ScaledBorderAndShadowSelection.ScaledBorderAndShadowNo or ScaledBorderAndShadowSelection.ScaledBorderAndShadowYes; SelectedScaledBorderAndShadow = GetScaledBorderAndShadowFromOptions(options); + HistoryAddSpecials = options.HistoryAddSpecials; DownloadSpeed = options.DownloadSpeedLimit; IncludeEpisodeDescription = options.IncludeVideoDescription; FileTitle = options.VideoTitle ?? ""; @@ -443,6 +447,7 @@ public partial class SettingsPageViewModel : ViewModelBase{ } CrunchyrollManager.Instance.CrunOptions.IncludeVideoDescription = IncludeEpisodeDescription; + CrunchyrollManager.Instance.CrunOptions.HistoryAddSpecials = HistoryAddSpecials; CrunchyrollManager.Instance.CrunOptions.VideoTitle = FileTitle; CrunchyrollManager.Instance.CrunOptions.Novids = !DownloadVideo; CrunchyrollManager.Instance.CrunOptions.Noaudio = !DownloadAudio; diff --git a/CRD/Views/SettingsPageView.axaml b/CRD/Views/SettingsPageView.axaml index f8dcf90..ab53732 100644 --- a/CRD/Views/SettingsPageView.axaml +++ b/CRD/Views/SettingsPageView.axaml @@ -143,6 +143,12 @@ + + + + + +