mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Prepare for coherent feature by restructuring AttributeUpdaterKernel
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.exceptions.MkvToolNixException;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.SetUtils;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.logging.log4j.core.util.IOUtils;
|
||||
|
||||
import java.io.File;
|
||||
@@ -19,14 +19,13 @@ import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.LaneType.AUDIO;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.LaneType.SUBTITLES;
|
||||
import static java.lang.String.format;
|
||||
|
||||
@Log4j2
|
||||
@Slf4j
|
||||
public class MkvFileProcessor implements FileProcessor {
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private static final String DISABLE_DEFAULT_TRACK = "--edit track:%s --set flag-default=0 ";
|
||||
private static final String ENABLE_DEFAULT_TRACK = "--edit track:%s --set flag-default=1 ";
|
||||
private static final String ENABLE_FORCED_TRACK = "--edit track:%s --set flag-forced=1 ";
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<FileAttribute> loadAttributes(File file) {
|
||||
@@ -62,7 +61,7 @@ public class MkvFileProcessor implements FileProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
log.debug(fileAttributes);
|
||||
log.debug(fileAttributes.toString());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
log.error("File could not be found or loaded!");
|
||||
@@ -70,27 +69,11 @@ public class MkvFileProcessor implements FileProcessor {
|
||||
return fileAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public FileInfoDto filterAttributes(List<FileAttribute> attributes) {
|
||||
FileInfoDto info = new FileInfoDto();
|
||||
List<FileAttribute> nonForcedTracks = attributes.stream()
|
||||
.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());
|
||||
|
||||
detectDefaultTracks(info, attributes, nonForcedTracks);
|
||||
detectDesiredTracks(info, nonForcedTracks, nonCommentaryTracks);
|
||||
log.debug(info);
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
protected void detectDefaultTracks(FileInfoDto info, List<FileAttribute> attributes, List<FileAttribute> nonForcedTracks) {
|
||||
public void detectDefaultTracks(FileInfoDto info, List<FileAttribute> attributes, List<FileAttribute> nonForcedTracks) {
|
||||
Set<FileAttribute> detectedForcedSubtitleLanes = new HashSet<>();
|
||||
for (FileAttribute attribute : attributes) {
|
||||
if (attribute.isDefaultTrack() && AUDIO.equals(attribute.getType()))
|
||||
@@ -108,7 +91,11 @@ public class MkvFileProcessor implements FileProcessor {
|
||||
);
|
||||
}
|
||||
|
||||
protected void detectDesiredTracks(FileInfoDto info, List<FileAttribute> nonForcedTracks, List<FileAttribute> nonCommentaryTracks) {
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void detectDesiredTracks(FileInfoDto info, List<FileAttribute> nonForcedTracks, List<FileAttribute> nonCommentaryTracks) {
|
||||
for (AttributeConfig config : Config.getInstance().getAttributeConfig()) {
|
||||
FileAttribute desiredAudio = null;
|
||||
FileAttribute desiredSubtitle = null;
|
||||
@@ -126,11 +113,15 @@ public class MkvFileProcessor implements FileProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void update(File file, FileInfoDto fileInfo) throws IOException, RuntimeException {
|
||||
public void update(File file, FileInfoDto fileInfo) throws IOException, MkvToolNixException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(format("\"%s\" ", Config.getInstance().getPathFor(MkvToolNix.MKV_PROP_EDIT)));
|
||||
sb.append(format("\"%s\" ", file.getAbsolutePath()));
|
||||
|
||||
if (fileInfo.isAudioDifferent()) {
|
||||
if (fileInfo.getDefaultAudioLanes() != null && !fileInfo.getDefaultSubtitleLanes().isEmpty()) {
|
||||
for (FileAttribute track: fileInfo.getDefaultAudioLanes()) {
|
||||
@@ -139,6 +130,7 @@ public class MkvFileProcessor implements FileProcessor {
|
||||
}
|
||||
sb.append(format(ENABLE_DEFAULT_TRACK, fileInfo.getDesiredAudioLane().getId()));
|
||||
}
|
||||
|
||||
if (fileInfo.isSubtitleDifferent()) {
|
||||
if (fileInfo.getDefaultSubtitleLanes() != null && !fileInfo.getDefaultSubtitleLanes().isEmpty()) {
|
||||
for (FileAttribute track: fileInfo.getDefaultSubtitleLanes()) {
|
||||
@@ -147,6 +139,7 @@ public class MkvFileProcessor implements FileProcessor {
|
||||
}
|
||||
sb.append(format(ENABLE_DEFAULT_TRACK, fileInfo.getDesiredSubtitleLane().getId()));
|
||||
}
|
||||
|
||||
if (fileInfo.areForcedTracksDifferent()) {
|
||||
for (FileAttribute attribute : fileInfo.getDesiredForcedSubtitleLanes()) {
|
||||
sb.append(format(ENABLE_FORCED_TRACK, attribute.getId()));
|
||||
@@ -156,6 +149,6 @@ public class MkvFileProcessor implements FileProcessor {
|
||||
InputStream inputstream = Runtime.getRuntime().exec(sb.toString()).getInputStream();
|
||||
String output = IOUtils.toString(new InputStreamReader(inputstream));
|
||||
log.debug(output);
|
||||
if (output.contains("Error")) throw new RuntimeException(output);
|
||||
if (output.contains("Error")) throw new MkvToolNixException(output);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user