Skip to content

Commit

Permalink
fix: handle keyed videos without subtitles
Browse files Browse the repository at this point in the history
- Added test to ensure videos without subtitles are kept
- Updated MergeSameKeysItems to retain videos even if no subtitles exist
  • Loading branch information
qwqcode committed Sep 24, 2024
1 parent 28dfef2 commit b71a2f3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 36 deletions.
20 changes: 20 additions & 0 deletions SubRenamer.Tests/MatcherTests/MergeSameKeysItemsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,24 @@ public void EmptyInput()
{
Assert.That(MergeSameKeysItems(new List<MatchItem>()), Is.Empty);
}

[Test]
public void KeepVideoIfNoSubtitles()
{
var input = new List<MatchItem>
{
new("k1", "v1.mp4", ""),
new("k1", "", "s1.srt"),
new("k2", "v2.mp4", ""),
new("k3", "v3.mp4", ""),
};
var output = new List<MatchItem>
{
new("k1", "v1.mp4", "s1.srt"),
new("k2", "v2.mp4", ""),
new("k3", "v3.mp4", ""),
};

Assert.That(MergeSameKeysItems(input), Is.EqualTo(output));
}
}
93 changes: 58 additions & 35 deletions SubRenamer.Tests/MatcherTests/TopLevelTests.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,37 @@
"Input": [
{"Key": "", "Video": "abc.S02E01.123.mkv", "Subtitle": ""},
{"Key": "", "Video": "abc.S02E02.abc.mkv", "Subtitle": ""},
{"Key": "", "Video": "abc.S02E03.ccc.mkv", "Subtitle": ""},
{"Key": "", "Video": "", "Subtitle": "[SubGroup] def.S02E01.xyz.ass"},
{"Key": "", "Video": "", "Subtitle": "[SubGroup] def.S02E02.abc.ass"}
{"Key": "", "Video": "", "Subtitle": "[SubGroup] def.S02E02.abc.ass"},
{"Key": "", "Video": "", "Subtitle": "[SubGroup] def.S02E04.kkk.ass"}
],
"Output": [
{"Key": "1", "Video": "abc.S02E01.123.mkv", "Subtitle": "[SubGroup] def.S02E01.xyz.ass"},
{"Key": "2", "Video": "abc.S02E02.abc.mkv", "Subtitle": "[SubGroup] def.S02E02.abc.ass"}
{"Key": "2", "Video": "abc.S02E02.abc.mkv", "Subtitle": "[SubGroup] def.S02E02.abc.ass"},
{"Key": "3", "Video": "abc.S02E03.ccc.mkv", "Subtitle": ""},
{"Key": "4", "Video": "", "Subtitle": "[SubGroup] def.S02E04.kkk.ass"}
]
},
{
"Name": "SquareBrackets",
"Input": [
{"Key": "", "Video": "abc [01].mkv", "Subtitle": ""},
{"Key": "", "Video": "abc [02].mkv", "Subtitle": ""},
{"Key": "", "Video": "", "Subtitle": "[SubGroup] def [01].ass"},
{"Key": "", "Video": "", "Subtitle": "[SubGroup] def [02].ass"}
],
"Output": [
{"Key": "1", "Video": "abc [01].mkv", "Subtitle": "[SubGroup] def [01].ass"},
{"Key": "2", "Video": "abc [02].mkv", "Subtitle": "[SubGroup] def [02].ass"}
]
},
{
"Name": "Chinese",
"Input": [
{"Key": "", "Video": "中文 第一集.mkv", "Subtitle": ""},
{"Key": "", "Video": "中文 第二集.mkv", "Subtitle": ""},
{"Key": "", "Video": "", "Subtitle": "【字幕】中文 第一集.srt"},
{"Key": "", "Video": "", "Subtitle": "【字幕】中文 第二集.srt"}
],
"Output": [
{"Key": "", "Video": "中文 第一集.mkv", "Subtitle": "【字幕】中文 第一集.srt"},
{"Key": "", "Video": "中文 第二集.mkv", "Subtitle": "【字幕】中文 第二集.srt"}
]
},
{
"Name": "Breaking Bad",
"Name": "Breaking Bad (with episode titles)",
"Input": [
{"Key": "", "Video": "Breaking.Bad.S03E04.Green.Light.2160p.Netflix.WEB-DL.DDP.5.1.H.265.mkv", "Subtitle": ""},
{"Key": "", "Video": "Breaking.Bad.S03E12.Half.Measures.2160p.Netflix.WEB-DL.DDP.5.1.H.265.mkv", "Subtitle": ""},
{"Key": "", "Video": "Breaking.Bad.S03E100.Test.Test.2160p.Netflix.WEB-DL.DDP.5.1.H.265.mkv", "Subtitle": ""},
{"Key": "", "Video": "", "Subtitle": "breaking.bad.s03e12.720p.hdtv.x264-ctu.en.srt"},
{"Key": "", "Video": "", "Subtitle": "Breaking.Bad.S03E04.720p.HDTV.x264-CTU.en.srt"}
{"Key": "", "Video": "", "Subtitle": "Breaking.Bad.S03E04.720p.HDTV.x264-CTU.en.srt"},
{"Key": "", "Video": "", "Subtitle": "Breaking.Bad.S03E123.720p.HDTV.x264-CTU.en.srt"}
],
"Output": [
{"Key": "4", "Video": "Breaking.Bad.S03E04.Green.Light.2160p.Netflix.WEB-DL.DDP.5.1.H.265.mkv", "Subtitle": "Breaking.Bad.S03E04.720p.HDTV.x264-CTU.en.srt"},
{"Key": "12", "Video": "Breaking.Bad.S03E12.Half.Measures.2160p.Netflix.WEB-DL.DDP.5.1.H.265.mkv", "Subtitle": "breaking.bad.s03e12.720p.hdtv.x264-ctu.en.srt"}
{"Key": "12", "Video": "Breaking.Bad.S03E12.Half.Measures.2160p.Netflix.WEB-DL.DDP.5.1.H.265.mkv", "Subtitle": "breaking.bad.s03e12.720p.hdtv.x264-ctu.en.srt"},
{"Key": "100", "Video": "Breaking.Bad.S03E100.Test.Test.2160p.Netflix.WEB-DL.DDP.5.1.H.265.mkv", "Subtitle": ""},
{"Key": "123", "Video": "", "Subtitle": "Breaking.Bad.S03E123.720p.HDTV.x264-CTU.en.srt"}
]
},
{
"Name": "Pantheon",
"Name": "Pantheon (with episode titles)",
"Input": [
{"Key": "", "Video": "Pantheon.S02E04.Olivia.and.Farhad.1080p.AMZN.WEB-DL.DD5.1.H.264-testWEB.mkv", "Subtitle": ""},
{"Key": "", "Video": "Pantheon.S02E05.Yair.1080p.AMZN.WEB-DL.DD5.1.H.264-testWEB.mkv", "Subtitle": ""},
Expand All @@ -68,7 +50,7 @@
]
},
{
"Name": "鬼滅之刃",
"Name": "鬼滅之刃 (with complex characters)",
"Input": [
{"Key": "", "Video": "[Up to 21°C] 鬼滅之刃 柱訓練篇 - 02 (Baha 1920x1080 AVC AAC MP4) [784C8989].mp4", "Subtitle": ""},
{"Key": "", "Video": "[Up to 21°C] 鬼滅之刃 柱訓練篇 - 04 (Baha 1920x1080 AVC AAC MP4) [41B42367].mp4", "Subtitle": ""},
Expand All @@ -81,7 +63,7 @@
]
},
{
"Name": "BlackDoor",
"Name": "BlackDoor (with parentheses)",
"Input": [
{"Key": "", "Video": "Black Mirror (2011)(1080p)(Webdl)(VP9)(14 lang-AAC- 2.0) (S01) PHDTeam.mkv", "Subtitle": ""},
{"Key": "", "Video": "Black Mirror (2011)(1080p)(Webdl)(VP9)(14 lang-AAC- 2.0) (S11) PHDTeam.mkv", "Subtitle": ""},
Expand All @@ -94,20 +76,61 @@
]
},
{
"Name": "Haikyuu!! (sc & tc)",
"Name": "Haikyuu!! (with multiple languages, one-to-many)",
"Input": [
{"Key": "", "Video": "[Kamigami] Haikyuu!! S2 - 09 [1920x1080 HEVC AAC Sub(Chs,Cht,Jap)].mkv", "Subtitle": ""},
{"Key": "", "Video": "[Kamigami] Haikyuu!! S2 - 10 [1920x1080 HEVC AAC Sub(Chs,Cht,Jap)].mkv", "Subtitle": ""},
{"Key": "", "Video": "", "Subtitle": "[YYDM-11FANS][Haikyuu!!][09][BDRIP][720P][X264-10bit_AAC][40A7E056].en.ass"},
{"Key": "", "Video": "", "Subtitle": "[YYDM-11FANS][Haikyuu!!][09][BDRIP][720P][X264-10bit_AAC][40A7E056].sc.ass"},
{"Key": "", "Video": "", "Subtitle": "[YYDM-11FANS][Haikyuu!!][09][BDRIP][720P][X264-10bit_AAC][40A7E056].tc.ass"},
{"Key": "", "Video": "", "Subtitle": "[YYDM-11FANS][Haikyuu!!][10][BDRIP][720P][X264-10bit_AAC][6FDEFD72].sc.ass"},
{"Key": "", "Video": "", "Subtitle": "[YYDM-11FANS][Haikyuu!!][10][BDRIP][720P][X264-10bit_AAC][6FDEFD72].tc.ass"}
],
"Output": [
{"Key": "9", "Video": "[Kamigami] Haikyuu!! S2 - 09 [1920x1080 HEVC AAC Sub(Chs,Cht,Jap)].mkv", "Subtitle": "[YYDM-11FANS][Haikyuu!!][09][BDRIP][720P][X264-10bit_AAC][40A7E056].en.ass"},
{"Key": "9", "Video": "[Kamigami] Haikyuu!! S2 - 09 [1920x1080 HEVC AAC Sub(Chs,Cht,Jap)].mkv", "Subtitle": "[YYDM-11FANS][Haikyuu!!][09][BDRIP][720P][X264-10bit_AAC][40A7E056].sc.ass"},
{"Key": "9", "Video": "[Kamigami] Haikyuu!! S2 - 09 [1920x1080 HEVC AAC Sub(Chs,Cht,Jap)].mkv", "Subtitle": "[YYDM-11FANS][Haikyuu!!][09][BDRIP][720P][X264-10bit_AAC][40A7E056].tc.ass"},
{"Key": "10", "Video": "[Kamigami] Haikyuu!! S2 - 10 [1920x1080 HEVC AAC Sub(Chs,Cht,Jap)].mkv", "Subtitle": "[YYDM-11FANS][Haikyuu!!][10][BDRIP][720P][X264-10bit_AAC][6FDEFD72].sc.ass"},
{"Key": "10", "Video": "[Kamigami] Haikyuu!! S2 - 10 [1920x1080 HEVC AAC Sub(Chs,Cht,Jap)].mkv", "Subtitle": "[YYDM-11FANS][Haikyuu!!][10][BDRIP][720P][X264-10bit_AAC][6FDEFD72].tc.ass"}
]
},
{
"Name": "SquareBrackets",
"Input": [
{"Key": "", "Video": "abc [01].mkv", "Subtitle": ""},
{"Key": "", "Video": "abc [02].mkv", "Subtitle": ""},
{"Key": "", "Video": "", "Subtitle": "[SubGroup] def [01].ass"},
{"Key": "", "Video": "", "Subtitle": "[SubGroup] def [02].ass"}
],
"Output": [
{"Key": "1", "Video": "abc [01].mkv", "Subtitle": "[SubGroup] def [01].ass"},
{"Key": "2", "Video": "abc [02].mkv", "Subtitle": "[SubGroup] def [02].ass"}
]
},
{
"Name": "Chinese Characters",
"Input": [
{"Key": "", "Video": "中文 第一集.mkv", "Subtitle": ""},
{"Key": "", "Video": "中文 第二集.mkv", "Subtitle": ""},
{"Key": "", "Video": "", "Subtitle": "【字幕】中文 第一集.srt"},
{"Key": "", "Video": "", "Subtitle": "【字幕】中文 第二集.srt"}
],
"Output": [
{"Key": "", "Video": "中文 第一集.mkv", "Subtitle": "【字幕】中文 第一集.srt"},
{"Key": "", "Video": "中文 第二集.mkv", "Subtitle": "【字幕】中文 第二集.srt"}
]
},
{
"Name": "WhiteSpaceEnd",
"Input": [
{"Key": "", "Video": "视频 1 xyz.mov", "Subtitle": ""},
{"Key": "", "Video": "视频 77 test xyz.mov", "Subtitle": ""},
{"Key": "", "Video": "", "Subtitle": "字幕 1xyz.srt"},
{"Key": "", "Video": "", "Subtitle": "字幕 77test xyz.srt"}
],
"Output": [
{"Key": "1", "Video": "视频 1 xyz.mov", "Subtitle": "字幕 1xyz.srt"},
{"Key": "77", "Video": "视频 77 test xyz.mov", "Subtitle": "字幕 77test xyz.srt"}
]
}
]
4 changes: 3 additions & 1 deletion SubRenamer/Matcher/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ public static List<MatchItem> MergeSameKeysItems(IReadOnlyList<MatchItem> items)
var result = new List<MatchItem>();
foreach (var group in groupedItems)
{
// One-to-Many mapping (Video-Subtitles)
var video = group.FirstOrDefault(item => !string.IsNullOrEmpty(item.Video))?.Video ?? "";
var subtitles = group.Where(item => !string.IsNullOrEmpty(item.Subtitle)).Select(item => item.Subtitle);

// Add a new MatchItem for each subtitle under the same key and video
result.AddRange(subtitles.Select(subtitle => new MatchItem(group.Key, video, subtitle)));
result.AddRange(subtitles.Select(subtitle => new MatchItem(group.Key, video, subtitle))
.DefaultIfEmpty(new MatchItem(group.Key, video, ""))); // Keep video if no subtitles
}

// Keep items with empty keys
Expand Down

0 comments on commit b71a2f3

Please sign in to comment.