From b17e71dc381ea68323e9b156adb891bf917a59e2 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 9 Apr 2017 12:28:00 +0800 Subject: [PATCH] Make sure that CD1/2 detection always works regardless of media folder structure by relying exclusively on MediaInfo characteristics instead of (guessed) media folder @see https://www.filebot.net/forums/viewtopic.php?f=4&t=4845 --- source/net/filebot/media/MediaDetection.java | 52 +++++++++----------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/source/net/filebot/media/MediaDetection.java b/source/net/filebot/media/MediaDetection.java index 469103c0..06cba2b6 100644 --- a/source/net/filebot/media/MediaDetection.java +++ b/source/net/filebot/media/MediaDetection.java @@ -30,6 +30,7 @@ import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.SortedSet; import java.util.TreeMap; @@ -1094,36 +1095,31 @@ public class MediaDetection { return; } - mapByMediaFolder(filesByExtension).forEach((mediaFolder, filesByMediaFolder) -> { - if (filesByMediaFolder.size() < 2) { - groups.add(filesByMediaFolder); - return; + filesByExtension.stream().collect(groupingBy(f -> { + if (VIDEO_FILES.accept(f) && f.length() > ONE_MEGABYTE) { + try (MediaInfo mi = new MediaInfo().open(f)) { + String v = mi.get(StreamKind.Video, 0, "Codec"); + String a = mi.get(StreamKind.Audio, 0, "Codec"); + String w = mi.get(StreamKind.Video, 0, "Width"); + String h = mi.get(StreamKind.Video, 0, "Height"); + return asList(v, a, w, h); + } catch (Exception e) { + debug.warning(format("Failed to read media characteristics: %s", e.getMessage())); + } + } else if (SUBTITLE_FILES.accept(f) && f.length() > ONE_KILOBYTE) { + try { + Language language = detectSubtitleLanguage(f); + if (language != null) { + return asList(language.getCode()); + } + } catch (Exception e) { + debug.warning(format("Failed to detect subtitle language: %s", e.getMessage())); + } } - filesByMediaFolder.stream().collect(groupingBy(f -> { - if (VIDEO_FILES.accept(f) && f.length() > ONE_MEGABYTE) { - try (MediaInfo mi = new MediaInfo().open(f)) { - String v = mi.get(StreamKind.Video, 0, "Codec"); - String a = mi.get(StreamKind.Audio, 0, "Codec"); - String w = mi.get(StreamKind.Video, 0, "Width"); - String h = mi.get(StreamKind.Video, 0, "Height"); - return asList(v, a, w, h); - } catch (Exception e) { - debug.warning(format("Failed to read media characteristics: %s", e.getMessage())); - } - } else if (SUBTITLE_FILES.accept(f) && f.length() > ONE_KILOBYTE) { - try { - Language language = detectSubtitleLanguage(f); - if (language != null) { - return asList(language.getCode()); - } - } catch (Exception e) { - debug.warning(format("Failed to detect subtitle language: %s", e.getMessage())); - } - } - return emptyList(); - }, LinkedHashMap::new, toList())).forEach((group, videos) -> groups.add(videos)); - }); + // default to grouping by most likely media folder + return Optional.ofNullable(guessMediaFolder(f)).map(File::getName); + }, LinkedHashMap::new, toList())).forEach((group, videos) -> groups.add(videos)); }); return groups;