From 7a96e1c40a99e30804e28ae780cb7e1d7318bfe7 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Mon, 29 Apr 2024 16:09:24 +0100 Subject: [PATCH 1/9] Add method to locate subtitle files for video A method 'TryLocateSubtitleFile' was implemented in FileUtil. It attempts to locate matching subtitle files for video files in known directories. The method was integrated into 'AddInputFile' of 'GenerateVideoWithHardSubs' and simplifies the earlier implementation for finding subtitles. This change will make the process of matching video files to subtitles more efficient and cleaner. Signed-off-by: Ivandro Jao --- src/libse/Common/FileUtil.cs | 53 +++++++++++++++++++ src/ui/Forms/GenerateVideoWithHardSubs.cs | 62 ++++++++--------------- 2 files changed, 73 insertions(+), 42 deletions(-) diff --git a/src/libse/Common/FileUtil.cs b/src/libse/Common/FileUtil.cs index 7da4577ae..b54e67e93 100644 --- a/src/libse/Common/FileUtil.cs +++ b/src/libse/Common/FileUtil.cs @@ -6,6 +6,7 @@ using Nikse.SubtitleEdit.Core.VobSub; using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; using System.Text.RegularExpressions; @@ -707,5 +708,57 @@ namespace Nikse.SubtitleEdit.Core.Common return false; } + + public static string TryLocateSubtitleFile(string path, string videoFileNameNoExtension) + { + // search in these directories + var knownSubtitleDirectories = new[] + { + path, Path.Combine(path, "Subs"), Path.Combine(path, "Sub"), Path.Combine(path, "Subtitles") + }; + foreach (var knownSubtitleDirectory in knownSubtitleDirectories) + { + var subtitleFileFromKnownDirectory = LocateSubtitle(knownSubtitleDirectory, videoFileNameNoExtension); + if (!string.IsNullOrEmpty(subtitleFileFromKnownDirectory)) + { + return subtitleFileFromKnownDirectory; + } + } + + return string.Empty; + + string LocateSubtitle(string localPath, in string localVideoFileName) + { + if (!Directory.Exists(localPath)) + { + return string.Empty; + } + + // try to locate subtitle file that has the same name as the video file + var defaultSubtitles = new[] + { + Path.Combine(localPath, Path.ChangeExtension(localVideoFileName, ".ass")), + Path.Combine(localPath, Path.ChangeExtension(localVideoFileName, ".srt")) + }; + foreach (var defaultSubtitle in defaultSubtitles) + { + if (File.Exists(defaultSubtitle)) + { + return defaultSubtitle; + } + } + + var assEnumerable = Directory.EnumerateFiles(localPath, "*.ass", SearchOption.TopDirectoryOnly); + var subripEnumerable = Directory.EnumerateFiles(localPath, "*.srt", SearchOption.TopDirectoryOnly); + var subtitleFile = assEnumerable.Concat(subripEnumerable).FirstOrDefault(); + // subtitle file found in top directory + if (!string.IsNullOrEmpty(subtitleFile)) + { + return subtitleFile; + } + + return string.Empty; + } + } } } diff --git a/src/ui/Forms/GenerateVideoWithHardSubs.cs b/src/ui/Forms/GenerateVideoWithHardSubs.cs index fad969fe1..1a08a5718 100644 --- a/src/ui/Forms/GenerateVideoWithHardSubs.cs +++ b/src/ui/Forms/GenerateVideoWithHardSubs.cs @@ -2131,54 +2131,32 @@ namespace Nikse.SubtitleEdit.Forms } } - private void AddInputFile(string fileName) + private void AddInputFile(string videoFileName) { - if (string.IsNullOrEmpty(fileName)) + if (string.IsNullOrEmpty(videoFileName)) { return; } - var ext = Path.GetExtension(fileName).ToLowerInvariant(); - if ((Utilities.AudioFileExtensions.Contains(ext) || Utilities.VideoFileExtensions.Contains(ext)) && File.Exists(fileName)) + var ext = Path.GetExtension(videoFileName).ToLowerInvariant(); + if ((Utilities.AudioFileExtensions.Contains(ext) || Utilities.VideoFileExtensions.Contains(ext)) && File.Exists(videoFileName)) { var item = new BatchVideoAndSub(); - item.VideoFileName = fileName; - item.VideoFileSizeInBytes = new FileInfo(fileName).Length; + item.VideoFileName = videoFileName; + item.VideoFileSizeInBytes = new FileInfo(videoFileName).Length; - var path = Path.GetDirectoryName(fileName); - var fileNameNoExt = Path.GetFileNameWithoutExtension(fileName); - var subFileName = Path.ChangeExtension(fileName, ".ass"); + var path = Path.GetDirectoryName(videoFileName); + var fileNameNoExt = Path.GetFileNameWithoutExtension(videoFileName); - if (!File.Exists(subFileName)) + // try locate subtitle file for the input vide file + var subtitleFile = FileUtil.TryLocateSubtitleFile(path, videoFileName); + if (File.Exists(subtitleFile)) { - subFileName = Path.ChangeExtension(fileName, ".srt"); + item.SubtitleFileName = subtitleFile; + item.SubtitleFileFileSizeInBytes = new FileInfo(subtitleFile).Length; } - if (!File.Exists(subFileName)) - { - var files = Directory.GetFiles(path, fileNameNoExt + "*.ass"); - if (files.Length > 0) - { - subFileName = files[0]; - } - } - - if (!File.Exists(subFileName)) - { - var files = Directory.GetFiles(path, fileNameNoExt + "*.srt"); - if (files.Length > 0) - { - subFileName = files[0]; - } - } - - if (File.Exists(subFileName)) - { - item.SubtitleFileName = subFileName; - item.SubtitleFileFileSizeInBytes = new FileInfo(subFileName).Length; - } - - var mediaInfo = FfmpegMediaInfo.Parse(fileName); + var mediaInfo = FfmpegMediaInfo.Parse(videoFileName); int width; int height; if (mediaInfo.VideoWidth > 0 && mediaInfo.VideoHeight > 0) @@ -2189,18 +2167,18 @@ namespace Nikse.SubtitleEdit.Forms else { var vInfo = new VideoInfo { Success = false }; - if (fileName.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase)) + if (videoFileName.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase)) { - vInfo = QuartsPlayer.GetVideoInfo(fileName); + vInfo = QuartsPlayer.GetVideoInfo(videoFileName); if (!vInfo.Success) { - vInfo = LibMpvDynamic.GetVideoInfo(fileName); + vInfo = LibMpvDynamic.GetVideoInfo(videoFileName); } } if (!vInfo.Success) { - vInfo = UiUtil.GetVideoInfo(fileName); + vInfo = UiUtil.GetVideoInfo(videoFileName); } width = vInfo.Width; @@ -2209,11 +2187,11 @@ namespace Nikse.SubtitleEdit.Forms if (width == 0 || height == 0) { - SeLogger.Error("Skipping burn-in file with no video: " + fileName); + SeLogger.Error("Skipping burn-in file with no video: " + videoFileName); return; // skip audio or damaged files } - var listViewItem = new ListViewItem(fileName); + var listViewItem = new ListViewItem(videoFileName); listViewItem.Tag = item; listViewItem.SubItems.Add($"{width}x{height}"); var s = Utilities.FormatBytesToDisplayFileSize(item.VideoFileSizeInBytes); From c3af5f22ca8b65f38b85db37f9d5de60b06ae59b Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Mon, 29 Apr 2024 16:11:14 +0100 Subject: [PATCH 2/9] Refactor TryLocateSubtitleFile method parameters The method TryLocateSubtitleFile in FileUtil.cs was refactored to receive the complete video file name, rather than just the file name without its extension. This change should enhance file locating accuracy as it now considers the file's full name, including its extension. Signed-off-by: Ivandro Jao --- src/libse/Common/FileUtil.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libse/Common/FileUtil.cs b/src/libse/Common/FileUtil.cs index b54e67e93..2ae851474 100644 --- a/src/libse/Common/FileUtil.cs +++ b/src/libse/Common/FileUtil.cs @@ -709,7 +709,7 @@ namespace Nikse.SubtitleEdit.Core.Common return false; } - public static string TryLocateSubtitleFile(string path, string videoFileNameNoExtension) + public static string TryLocateSubtitleFile(string path, string videoFileName) { // search in these directories var knownSubtitleDirectories = new[] @@ -718,7 +718,7 @@ namespace Nikse.SubtitleEdit.Core.Common }; foreach (var knownSubtitleDirectory in knownSubtitleDirectories) { - var subtitleFileFromKnownDirectory = LocateSubtitle(knownSubtitleDirectory, videoFileNameNoExtension); + var subtitleFileFromKnownDirectory = LocateSubtitle(knownSubtitleDirectory, videoFileName); if (!string.IsNullOrEmpty(subtitleFileFromKnownDirectory)) { return subtitleFileFromKnownDirectory; From 82ac6dec92c172632f26609855421b96ca8e0e3b Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Mon, 29 Apr 2024 16:17:36 +0100 Subject: [PATCH 3/9] Fix typo in code comment A typo in the comment for the subtitle file location function was corrected. Previously, the comment read "try locate subtitle file for the input vide file", and now it reads "try to locate subtitle file for the input vide file". Other changes include removal of an unused variable "fileNameNoExt". Signed-off-by: Ivandro Jao --- src/ui/Forms/GenerateVideoWithHardSubs.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ui/Forms/GenerateVideoWithHardSubs.cs b/src/ui/Forms/GenerateVideoWithHardSubs.cs index 1a08a5718..92dedf853 100644 --- a/src/ui/Forms/GenerateVideoWithHardSubs.cs +++ b/src/ui/Forms/GenerateVideoWithHardSubs.cs @@ -2146,9 +2146,7 @@ namespace Nikse.SubtitleEdit.Forms item.VideoFileSizeInBytes = new FileInfo(videoFileName).Length; var path = Path.GetDirectoryName(videoFileName); - var fileNameNoExt = Path.GetFileNameWithoutExtension(videoFileName); - - // try locate subtitle file for the input vide file + // try to locate subtitle file for the input vide file var subtitleFile = FileUtil.TryLocateSubtitleFile(path, videoFileName); if (File.Exists(subtitleFile)) { From f256b59691bbe7d50eea4e138700992f0be7296e Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Tue, 30 Apr 2024 12:35:19 +0100 Subject: [PATCH 4/9] Refactor subtitle file location code in FileUtil This commit simplifies the subtitle file location code in FileUtil by removing unnecessary nested functions. It changes the way the search is done in known subtitle directories and handles the case if a video file is sent with a full path. This results in cleaner and more manageable code. Signed-off-by: Ivandro Jao --- src/libse/Common/FileUtil.cs | 40 ++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/libse/Common/FileUtil.cs b/src/libse/Common/FileUtil.cs index 2ae851474..f9fa0760c 100644 --- a/src/libse/Common/FileUtil.cs +++ b/src/libse/Common/FileUtil.cs @@ -711,34 +711,30 @@ namespace Nikse.SubtitleEdit.Core.Common public static string TryLocateSubtitleFile(string path, string videoFileName) { - // search in these directories + // search in these subdirectories: \Subs;\Sub;\Subtitles; var knownSubtitleDirectories = new[] { path, Path.Combine(path, "Subs"), Path.Combine(path, "Sub"), Path.Combine(path, "Subtitles") }; + + // handles if video file was sent with full path + if (Path.IsPathRooted(videoFileName)) + { + videoFileName = Path.GetFileName(videoFileName); + } + foreach (var knownSubtitleDirectory in knownSubtitleDirectories) { - var subtitleFileFromKnownDirectory = LocateSubtitle(knownSubtitleDirectory, videoFileName); - if (!string.IsNullOrEmpty(subtitleFileFromKnownDirectory)) + if (!Directory.Exists(knownSubtitleDirectory)) { - return subtitleFileFromKnownDirectory; - } - } - - return string.Empty; - - string LocateSubtitle(string localPath, in string localVideoFileName) - { - if (!Directory.Exists(localPath)) - { - return string.Empty; + continue; } // try to locate subtitle file that has the same name as the video file var defaultSubtitles = new[] { - Path.Combine(localPath, Path.ChangeExtension(localVideoFileName, ".ass")), - Path.Combine(localPath, Path.ChangeExtension(localVideoFileName, ".srt")) + Path.Combine(knownSubtitleDirectory, Path.ChangeExtension(videoFileName, ".ass")), + Path.Combine(knownSubtitleDirectory, Path.ChangeExtension(videoFileName, ".srt")) }; foreach (var defaultSubtitle in defaultSubtitles) { @@ -748,17 +744,17 @@ namespace Nikse.SubtitleEdit.Core.Common } } - var assEnumerable = Directory.EnumerateFiles(localPath, "*.ass", SearchOption.TopDirectoryOnly); - var subripEnumerable = Directory.EnumerateFiles(localPath, "*.srt", SearchOption.TopDirectoryOnly); - var subtitleFile = assEnumerable.Concat(subripEnumerable).FirstOrDefault(); - // subtitle file found in top directory + // get first subtitle in path with extension .ass or .srt + var assEnumerable = Directory.EnumerateFiles(knownSubtitleDirectory, "*.ass", SearchOption.TopDirectoryOnly); + var subRipEnumerable = Directory.EnumerateFiles(knownSubtitleDirectory, "*.srt", SearchOption.TopDirectoryOnly); + var subtitleFile = assEnumerable.Concat(subRipEnumerable).FirstOrDefault(); if (!string.IsNullOrEmpty(subtitleFile)) { return subtitleFile; } - - return string.Empty; } + + return string.Empty; } } } From 3e1fabac30f194a75cb07cc65ffc35b5f299c505 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Tue, 30 Apr 2024 12:35:28 +0100 Subject: [PATCH 5/9] Refactor FfmpegMediaInfo to use Dimension for dimensions The properties VideoWidth and VideoHeight in FfmpegMediaInfo class have been replaced with a Dimension object. This modification leads to simpler and more readable code by encapsulating width and height information within the Dimension class. Any resolution information extracted is now directly assigned to the Dimension property. Signed-off-by: Ivandro Jao --- src/libse/Common/FfmpegMediaInfo.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libse/Common/FfmpegMediaInfo.cs b/src/libse/Common/FfmpegMediaInfo.cs index d4ff98e53..a45176794 100644 --- a/src/libse/Common/FfmpegMediaInfo.cs +++ b/src/libse/Common/FfmpegMediaInfo.cs @@ -11,8 +11,8 @@ namespace Nikse.SubtitleEdit.Core.Common public class FfmpegMediaInfo { public List Tracks { get; set; } - public int VideoWidth { get; set; } - public int VideoHeight { get; set; } + + public Dimension Dimension { get; set; } private static readonly Regex ResolutionRegex = new Regex(@"\d\d+x\d\d+", RegexOptions.Compiled); @@ -65,13 +65,12 @@ namespace Nikse.SubtitleEdit.Core.Common if (resolutionMatch.Success) { var parts = resolutionMatch.Value.Split('x'); - if (info.VideoWidth == 0 && + if (info.Dimension.Width == 0 && parts.Length == 2 && int.TryParse(parts[0], out var w) && int.TryParse(parts[1], out var h)) { - info.VideoWidth = w; - info.VideoHeight = h; + info.Dimension = new Dimension(h, w); } } From 3d24cdcbb63f8ff4e44ffad541573273fd44e0d0 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Tue, 30 Apr 2024 12:35:42 +0100 Subject: [PATCH 6/9] Add Dimension struct to manage object sizes The struct, Dimension which represents the dimensions of an object, is created. This contains basic properties for height and width, and methods for checking equality, validity and providing a string representation. The Dimension can later be used throughout the project for managing sizes of different objects. Signed-off-by: Ivandro Jao --- src/libse/Common/Dimension.cs | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/libse/Common/Dimension.cs diff --git a/src/libse/Common/Dimension.cs b/src/libse/Common/Dimension.cs new file mode 100644 index 000000000..e70de3674 --- /dev/null +++ b/src/libse/Common/Dimension.cs @@ -0,0 +1,40 @@ +using System; + +namespace Nikse.SubtitleEdit.Core.Common +{ + /// + /// Struct representing the dimensions of an object. + /// + public struct Dimension : IEquatable + { + public int Height { get; set; } + public int Width { get; set; } + + public Dimension(int height, int width) => (Height, Width) = (height, width); + + /// + /// Returns a string representation of the Dimension object, representing its height and width. + /// + /// A string representation of the Dimension object, in the format "height x width". + public override string ToString() => $"{Height}x{Width}"; + + public bool Equals(Dimension other) => Height == other.Height && Width == other.Width; + public override bool Equals(object obj) => obj is Dimension other && Equals(other); + public static bool operator ==(Dimension left, Dimension right) => left.Equals(right); + public static bool operator !=(Dimension left, Dimension right) => !(left == right); + + /// + /// Checks if the dimension (height and width) is valid. + /// + /// True if the dimension is valid; otherwise, false. + public bool IsValid() => Width > 0 && Height > 0; + + public override int GetHashCode() + { + unchecked + { + return (Height * 397) ^ Width; + } + } + } +} \ No newline at end of file From 93c9e9947ab4e06553057babffe5b6e5756f7aab Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Tue, 30 Apr 2024 12:35:53 +0100 Subject: [PATCH 7/9] Refactor video and subtitle batch creation code Code for creating and validating batch video and subtitle entities has been extracted into separate methods, improving the readability of the main method. These changes also include a rectification in the logic for handling invalid video dimensions. Signed-off-by: Ivandro Jao --- src/ui/Forms/GenerateVideoWithHardSubs.cs | 115 ++++++++++++---------- 1 file changed, 63 insertions(+), 52 deletions(-) diff --git a/src/ui/Forms/GenerateVideoWithHardSubs.cs b/src/ui/Forms/GenerateVideoWithHardSubs.cs index 92dedf853..d527d64df 100644 --- a/src/ui/Forms/GenerateVideoWithHardSubs.cs +++ b/src/ui/Forms/GenerateVideoWithHardSubs.cs @@ -2141,66 +2141,77 @@ namespace Nikse.SubtitleEdit.Forms var ext = Path.GetExtension(videoFileName).ToLowerInvariant(); if ((Utilities.AudioFileExtensions.Contains(ext) || Utilities.VideoFileExtensions.Contains(ext)) && File.Exists(videoFileName)) { - var item = new BatchVideoAndSub(); - item.VideoFileName = videoFileName; - item.VideoFileSizeInBytes = new FileInfo(videoFileName).Length; - - var path = Path.GetDirectoryName(videoFileName); - // try to locate subtitle file for the input vide file - var subtitleFile = FileUtil.TryLocateSubtitleFile(path, videoFileName); - if (File.Exists(subtitleFile)) + var videoDimension = GetVideoDimension(videoFileName); + if (!videoDimension.IsValid()) { - item.SubtitleFileName = subtitleFile; - item.SubtitleFileFileSizeInBytes = new FileInfo(subtitleFile).Length; + return; } - - var mediaInfo = FfmpegMediaInfo.Parse(videoFileName); - int width; - int height; - if (mediaInfo.VideoWidth > 0 && mediaInfo.VideoHeight > 0) - { - width = mediaInfo.VideoWidth; - height = mediaInfo.VideoHeight; - } - else - { - var vInfo = new VideoInfo { Success = false }; - if (videoFileName.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase)) - { - vInfo = QuartsPlayer.GetVideoInfo(videoFileName); - if (!vInfo.Success) - { - vInfo = LibMpvDynamic.GetVideoInfo(videoFileName); - } - } - - if (!vInfo.Success) - { - vInfo = UiUtil.GetVideoInfo(videoFileName); - } - - width = vInfo.Width; - height = vInfo.Height; - } - - if (width == 0 || height == 0) - { - SeLogger.Error("Skipping burn-in file with no video: " + videoFileName); - return; // skip audio or damaged files - } - - var listViewItem = new ListViewItem(videoFileName); - listViewItem.Tag = item; - listViewItem.SubItems.Add($"{width}x{height}"); - var s = Utilities.FormatBytesToDisplayFileSize(item.VideoFileSizeInBytes); + + var batchVideoAndSub = CreateBatchVideoAndSub(videoFileName); + var listViewItem = new ListViewItem(videoFileName) { Tag = batchVideoAndSub }; + listViewItem.SubItems.Add(videoDimension.ToString()); + var s = Utilities.FormatBytesToDisplayFileSize(batchVideoAndSub.VideoFileSizeInBytes); listViewItem.SubItems.Add(s); - listViewItem.SubItems.Add(Path.GetFileName(item.SubtitleFileName)); + listViewItem.SubItems.Add(Path.GetFileName(batchVideoAndSub.SubtitleFileName)); listViewItem.SubItems.Add(string.Empty); listViewBatch.Items.Add(listViewItem); - _batchVideoAndSubList.Add(item); + _batchVideoAndSubList.Add(batchVideoAndSub); } } + private BatchVideoAndSub CreateBatchVideoAndSub(string videoFileName) + { + var batchVideoAndSub = new BatchVideoAndSub + { + VideoFileName = videoFileName, + VideoFileSizeInBytes = new FileInfo(videoFileName).Length + }; + + var path = Path.GetDirectoryName(videoFileName); + // try to locate subtitle file for the input vide file + var subtitleFile = FileUtil.TryLocateSubtitleFile(path, videoFileName); + if (File.Exists(subtitleFile)) + { + batchVideoAndSub.SubtitleFileName = subtitleFile; + batchVideoAndSub.SubtitleFileFileSizeInBytes = new FileInfo(subtitleFile).Length; + } + + return batchVideoAndSub; + } + + private Dimension GetVideoDimension(string videoFileName) + { + var mediaInfo = FfmpegMediaInfo.Parse(videoFileName); + if (mediaInfo.Dimension.IsValid()) + { + return mediaInfo.Dimension; + } + + var vInfo = new VideoInfo { Success = false }; + if (videoFileName.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase)) + { + vInfo = QuartsPlayer.GetVideoInfo(videoFileName); + if (!vInfo.Success) + { + vInfo = LibMpvDynamic.GetVideoInfo(videoFileName); + } + } + + if (!vInfo.Success) + { + vInfo = UiUtil.GetVideoInfo(videoFileName); + } + + if (vInfo.Width == 0 || vInfo.Height == 0) + { + SeLogger.Error("Skipping burn-in file with no video: " + videoFileName); + // return; // skip audio or damaged files + return new Dimension(); + } + + return new Dimension(vInfo.Height, vInfo.Width); + } + private void deleteToolStripMenuItem_Click(object sender, EventArgs e) { var indices = new List(); From 6323937c3048fda0e7038983f89184e4ffb7e69b Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Tue, 30 Apr 2024 12:41:32 +0100 Subject: [PATCH 8/9] Refactor video dimension validation in GenerateVideoWithHardSubs This commit refactors the validation of video dimensions in GenerateVideoWithHardSubs class. Instead of checking width and height separately, a new Dimension object is created and validated with IsValid(). This simplifies the code and improves readability. Signed-off-by: Ivandro Jao --- src/ui/Forms/GenerateVideoWithHardSubs.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ui/Forms/GenerateVideoWithHardSubs.cs b/src/ui/Forms/GenerateVideoWithHardSubs.cs index d527d64df..4664d3b7e 100644 --- a/src/ui/Forms/GenerateVideoWithHardSubs.cs +++ b/src/ui/Forms/GenerateVideoWithHardSubs.cs @@ -2202,14 +2202,15 @@ namespace Nikse.SubtitleEdit.Forms vInfo = UiUtil.GetVideoInfo(videoFileName); } - if (vInfo.Width == 0 || vInfo.Height == 0) + var dimension = new Dimension(vInfo.Height, vInfo.Width); + + // skip audio or damaged files + if (!dimension.IsValid()) { SeLogger.Error("Skipping burn-in file with no video: " + videoFileName); - // return; // skip audio or damaged files - return new Dimension(); } - return new Dimension(vInfo.Height, vInfo.Width); + return dimension; } private void deleteToolStripMenuItem_Click(object sender, EventArgs e) From 042dfa14e5e0275956476de46c82b4b88bf6f6b9 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Tue, 30 Apr 2024 13:10:10 +0100 Subject: [PATCH 9/9] Add DimensionTest to test project A new test class, DimensionTest, is created and added to the test project. This class specifically tests the functionality of the Dimension object's validity and equality methods, thus enhancing our suite of unit tests. Signed-off-by: Ivandro Jao --- src/Test/Logic/DimensionTest.cs | 34 +++++++++++++++++++++++++++++++++ src/Test/Test.csproj | 1 + 2 files changed, 35 insertions(+) create mode 100644 src/Test/Logic/DimensionTest.cs diff --git a/src/Test/Logic/DimensionTest.cs b/src/Test/Logic/DimensionTest.cs new file mode 100644 index 000000000..c43ad570c --- /dev/null +++ b/src/Test/Logic/DimensionTest.cs @@ -0,0 +1,34 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Nikse.SubtitleEdit.Core.Common; + +namespace Test.Logic +{ + [TestClass] + public class DimensionTest + { + [TestMethod] + public void InvalidTest() + { + var dimension = new Dimension(); + Assert.AreEqual(0, dimension.Width); + Assert.AreEqual(0, dimension.Height); + Assert.IsTrue(!dimension.IsValid()); + } + + [TestMethod] + public void ValidTest() + { + var dimension = new Dimension(10, 10); + Assert.IsTrue(dimension.IsValid()); + } + + [TestMethod] + public void EqualityTest() + { + var dimensionOne = new Dimension(); + var dimensionTwo = new Dimension(); + Assert.AreEqual(dimensionOne, dimensionTwo); + Assert.IsTrue(dimensionOne == dimensionTwo); + } + } +} \ No newline at end of file diff --git a/src/Test/Test.csproj b/src/Test/Test.csproj index 0bbe85949..c0c2f74a0 100644 --- a/src/Test/Test.csproj +++ b/src/Test/Test.csproj @@ -76,6 +76,7 @@ +