Add track detection for coherent feature

This commit is contained in:
2023-03-05 17:48:07 +01:00
parent 440251c7c9
commit 8317e97639
4 changed files with 73 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl; package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException; import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfoDto; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfoDto;
@@ -31,8 +32,14 @@ public interface FileProcessor {
* @param info to be populated * @param info to be populated
* @param nonForcedTracks List of all not forced tracks * @param nonForcedTracks List of all not forced tracks
* @param nonCommentaryTracks List of all not commentary tracks * @param nonCommentaryTracks List of all not commentary tracks
* @param configs
*/ */
void detectDesiredTracks(FileInfoDto info, List<FileAttribute> nonForcedTracks, List<FileAttribute> nonCommentaryTracks); void detectDesiredTracks(FileInfoDto info, List<FileAttribute> nonForcedTracks, List<FileAttribute> nonCommentaryTracks,
AttributeConfig... configs);
List<FileAttribute> retrieveNonForcedTracks(List<FileAttribute> attributes);
List<FileAttribute> retrieveNonCommentaryTracks(List<FileAttribute> attributes);
/** /**
* Update the file. * Update the file.

View File

@@ -6,6 +6,7 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.SetUtils; import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.SetUtils;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.util.IOUtils; import org.apache.logging.log4j.core.util.IOUtils;
import java.io.File; import java.io.File;
@@ -95,8 +96,9 @@ public class MkvFileProcessor implements FileProcessor {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void detectDesiredTracks(FileInfoDto info, List<FileAttribute> nonForcedTracks, List<FileAttribute> nonCommentaryTracks) { public void detectDesiredTracks(FileInfoDto info, List<FileAttribute> nonForcedTracks, List<FileAttribute> nonCommentaryTracks,
for (AttributeConfig config : Config.getInstance().getAttributeConfig()) { AttributeConfig... configs) {
for (AttributeConfig config : configs) {
FileAttribute desiredAudio = null; FileAttribute desiredAudio = null;
FileAttribute desiredSubtitle = null; FileAttribute desiredSubtitle = null;
for (FileAttribute attribute : SetUtils.retainOf(nonForcedTracks, nonCommentaryTracks)) { for (FileAttribute attribute : SetUtils.retainOf(nonForcedTracks, nonCommentaryTracks)) {
@@ -113,6 +115,23 @@ public class MkvFileProcessor implements FileProcessor {
} }
} }
@Override
public List<FileAttribute> retrieveNonForcedTracks(List<FileAttribute> attributes) {
return attributes.stream()
.filter(elem -> !StringUtils.containsAnyIgnoreCase(elem.getTrackName(),
Config.getInstance().getForcedKeywords().toArray(new CharSequence[0])))
.filter(elem -> !elem.isForcedTrack())
.collect(Collectors.toList());
}
@Override
public List<FileAttribute> retrieveNonCommentaryTracks(List<FileAttribute> attributes) {
return attributes.stream()
.filter(elem -> !StringUtils.containsAnyIgnoreCase(elem.getTrackName(),
Config.getInstance().getCommentaryKeywords().toArray(new CharSequence[0])))
.collect(Collectors.toList());
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@@ -3,11 +3,16 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.FileProcessor;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfoDto;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.io.File; import java.io.File;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Slf4j @Slf4j
@@ -42,6 +47,43 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
*/ */
@Override @Override
void process(File file) { void process(File file) {
List<FileInfoDto> fileInfos = collector.loadFiles(file.getAbsolutePath())
.stream().map(FileInfoDto::new)
.collect(Collectors.toList());
Map<FileInfoDto, List<FileAttribute>> fileAttributeCache = new HashMap<>();
for (FileInfoDto fileInfo : fileInfos) {
if (!Config.getInstance().getIncludePattern().matcher(fileInfo.getFile().getAbsolutePath()).matches()) {
statistic.excluded();
continue;
}
fileAttributeCache.put(fileInfo, processor.loadAttributes(fileInfo.getFile()));
}
for (AttributeConfig config : Config.getInstance().getAttributeConfig()) {
for (FileInfoDto fileInfo : fileInfos) {
List<FileAttribute> attributes = fileAttributeCache.get(fileInfo);
List<FileAttribute> nonForcedTracks = processor.retrieveNonForcedTracks(attributes);
List<FileAttribute> nonCommentaryTracks = processor.retrieveNonCommentaryTracks(attributes);
processor.detectDefaultTracks(fileInfo, attributes, nonForcedTracks);
processor.detectDesiredTracks(fileInfo, nonForcedTracks, nonCommentaryTracks, config);
}
if (fileInfos.stream().allMatch(elem -> elem.getDesiredSubtitleLane() != null && elem.getDesiredAudioLane() != null)) {
log.debug("Found {}/{} match for {}", config.getAudioLanguage(), config.getSubtitleLanguage(), file.getAbsolutePath());
break;
}
fileInfos.forEach(f -> {
f.setDesiredAudioLane(null);
f.setDesiredSubtitleLane(null);
});
}
// apply config
// apply default process if nothing was found (if parameter is set)
} }
} }

View File

@@ -39,16 +39,8 @@ public class DefaultAttributeUpdaterKernel extends AttributeUpdaterKernel {
FileInfoDto fileInfo = new FileInfoDto(file); FileInfoDto fileInfo = new FileInfoDto(file);
List<FileAttribute> attributes = processor.loadAttributes(file); List<FileAttribute> attributes = processor.loadAttributes(file);
// TODO: extract this to a attributeProcessor? List<FileAttribute> nonForcedTracks = processor.retrieveNonForcedTracks(attributes);
List<FileAttribute> nonForcedTracks = attributes.stream() List<FileAttribute> nonCommentaryTracks = processor.retrieveNonCommentaryTracks(attributes);
.filter(elem -> !StringUtils.containsAnyIgnoreCase(elem.getTrackName(),
Config.getInstance().getForcedKeywords().toArray(new CharSequence[0])))
.filter(elem -> !elem.isForcedTrack())
.collect(Collectors.toList());
List<FileAttribute> nonCommentaryTracks = attributes.stream()
.filter(elem -> !StringUtils.containsAnyIgnoreCase(elem.getTrackName(),
Config.getInstance().getCommentaryKeywords().toArray(new CharSequence[0])))
.collect(Collectors.toList());
processor.detectDefaultTracks(fileInfo, attributes, nonForcedTracks); processor.detectDefaultTracks(fileInfo, attributes, nonForcedTracks);
processor.detectDesiredTracks(fileInfo, nonForcedTracks, nonCommentaryTracks); processor.detectDesiredTracks(fileInfo, nonForcedTracks, nonCommentaryTracks);