FF-228 - subtitle merge now can handle language codes

This commit is contained in:
John Andrews
2022-07-21 12:49:02 +12:00
parent 7e7630579c
commit 8b6fc37564
2 changed files with 53 additions and 7 deletions

View File

@@ -69,13 +69,8 @@ public class FfmpegBuilderSubtitleTrackMerge : FfmpegBuilderNode
if (MatchFilename)
{
var origFile = new FileInfo(args.FileName);
string origFilename = origFile.Name.Replace(origFile.Extension, "");
bool matchesOriginal = file.Name.Replace(file.Extension, string.Empty).ToLowerInvariant().Equals(origFilename.ToLowerInvariant());
var workingFile = new FileInfo(args.WorkingFile);
string workingFilename = workingFile.Name.Replace(workingFile.Extension, "");
bool matchesWorking = file.Name.Replace(file.Extension, string.Empty).ToLowerInvariant().Equals(workingFilename.ToLowerInvariant());
bool matchesOriginal = FilenameMatches(args.FileName, file.FullName);
bool matchesWorking = FilenameMatches(args.WorkingFile, file.FullName);
if (matchesOriginal == false && matchesWorking == false)
continue;
@@ -102,4 +97,34 @@ public class FfmpegBuilderSubtitleTrackMerge : FfmpegBuilderNode
this.Model.ForceEncode = true;
return count > 0 ? 1 : 2;
}
internal bool FilenameMatches(string input, string other)
{
var inputFile = new FileInfo(input);
string inputName = inputFile.Name.Replace(inputFile.Extension, "");
var otherFile = new FileInfo(other);
string otherName = otherFile.Name.Replace(otherFile.Extension, "");
if (inputName.ToLowerInvariant().Equals(otherName.ToLowerInvariant()))
return true;
if(Regex.IsMatch(otherName, @"(\.[a-zA-Z]{2,3}){1,2}$"))
{
string stripLang = Regex.Replace(otherName, @"(\.[a-zA-Z]{2,3}){1,2}$", string.Empty).Replace(" ", " ").Trim();
if (inputName.ToLowerInvariant().Equals(stripLang.ToLowerInvariant()))
return true;
}
if (Regex.IsMatch(otherName, @"\([a-zA-Z]{2,3}\)"))
{
string stripLang = Regex.Replace(otherName, @"\([a-zA-Z]{2,3}\)", string.Empty).Replace(" ", " ").Trim();
if (inputName.ToLowerInvariant().Equals(stripLang.ToLowerInvariant()))
return true;
}
return false;
}
}

View File

@@ -1220,6 +1220,27 @@ public class FfmpegBuilder_BasicTests
Assert.AreEqual(1, result);
}
[TestMethod]
public void FfmpegBuilder_SubtitleTrackMerge_FileMatchesTests()
{
FfmpegBuilderSubtitleTrackMerge ffSubMerge = new();
Assert.IsTrue(ffSubMerge.FilenameMatches("Test.mkv", "test.srt"));
Assert.IsTrue(ffSubMerge.FilenameMatches("Test.mkv", "test.en.srt"));
Assert.IsTrue(ffSubMerge.FilenameMatches("Test.mkv", "test(en).srt"));
Assert.IsTrue(ffSubMerge.FilenameMatches("Test.mkv", "test (en).srt"));
Assert.IsTrue(ffSubMerge.FilenameMatches("Test.mkv", "test.en.hi.srt"));
Assert.IsTrue(ffSubMerge.FilenameMatches("Test.mkv", "test.en.sdh.srt"));
Assert.IsTrue(ffSubMerge.FilenameMatches("Test.mkv", "test.en.cc.srt"));
Assert.IsTrue(ffSubMerge.FilenameMatches("Test.mkv", "test.deu.srt"));
Assert.IsTrue(ffSubMerge.FilenameMatches("Test.mkv", "test(deu).srt"));
Assert.IsTrue(ffSubMerge.FilenameMatches("Test.mkv", "test (deu).srt"));
Assert.IsFalse(ffSubMerge.FilenameMatches("Test.mkv", "nomatch.srt"));
Assert.IsFalse(ffSubMerge.FilenameMatches("Test.mkv", "nomatch.en.srt"));
Assert.IsFalse(ffSubMerge.FilenameMatches("Test.mkv", "nomatch(en).srt"));
Assert.IsFalse(ffSubMerge.FilenameMatches("Test.mkv", "nomatch (en).srt"));
}
[TestMethod]
public void FfmpegBuilder_BlackBars_Short()