mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Improve general code quality
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class FileFilter {
|
||||
static boolean accept(File pathName, String[] fileExtensions) {
|
||||
return StringUtils.endsWithAny(pathName.getAbsolutePath().toLowerCase(), fileExtensions)
|
||||
&& Config.getInstance().getIncludePattern().matcher(pathName.getName()).matches();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.io.File;
|
||||
@@ -15,27 +14,17 @@ import java.util.stream.Stream;
|
||||
|
||||
@Log4j2
|
||||
public class MkvFileCollector implements FileCollector {
|
||||
private static final String[] fileExtensions = new String[]{".mkv", ".mka", ".mks", ".mk3d"};
|
||||
|
||||
@Override
|
||||
public List<File> loadFiles(String path) {
|
||||
File file = new File(path);
|
||||
if (file.isFile() && file.getAbsolutePath().endsWith(".mkv")) {
|
||||
return new ArrayList<>() {{
|
||||
add(file);
|
||||
}};
|
||||
} else if (file.isDirectory()) {
|
||||
try (Stream<Path> paths = Files.walk(Paths.get(path))) {
|
||||
return paths
|
||||
.filter(Files::isRegularFile)
|
||||
.map(Path::toFile)
|
||||
.filter(f -> f.getAbsolutePath().endsWith(".mkv"))
|
||||
.filter(f -> Config.getInstance().getIncludePattern().matcher(f.getName()).matches())
|
||||
.collect(Collectors.toList());
|
||||
} catch (IOException e) {
|
||||
log.error("Couldn't find file or directory!", e);
|
||||
return new ArrayList<>();
|
||||
}
|
||||
} else {
|
||||
try (Stream<Path> paths = Files.walk(Paths.get(path))) {
|
||||
return paths.filter(Files::isRegularFile)
|
||||
.map(Path::toFile)
|
||||
.filter(file -> FileFilter.accept(file, fileExtensions))
|
||||
.collect(Collectors.toList());
|
||||
} catch (IOException e) {
|
||||
log.error("Couldn't find file or directory!", e);
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.LogUtils;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -62,7 +61,7 @@ public class MkvFileProcessor implements FileProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
LogUtils.ifDebug(log, fileAttributes);
|
||||
log.debug(fileAttributes);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
log.error("File could not be found or loaded!");
|
||||
@@ -77,19 +76,20 @@ public class MkvFileProcessor implements FileProcessor {
|
||||
.filter(elem -> !StringUtils.containsAnyIgnoreCase(elem.getTrackName(), forcedKeywords))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
detectCurrentConfiguration(attributes, info, nonForcedTracks);
|
||||
detectDesiredConfiguration(info, nonForcedTracks);
|
||||
LogUtils.ifDebug(log, info);
|
||||
detectDefaultTracks(attributes, info, nonForcedTracks);
|
||||
detectDesiredTracks(info, nonForcedTracks);
|
||||
log.debug(info);
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
private void detectCurrentConfiguration(List<FileAttribute> attributes, FileInfoDto info, List<FileAttribute> nonForcedTracks) {
|
||||
private void detectDefaultTracks(List<FileAttribute> attributes, FileInfoDto info, List<FileAttribute> nonForcedTracks) {
|
||||
Set<FileAttribute> detectedForcedSubtitleLanes = new HashSet<>();
|
||||
for (FileAttribute attribute : attributes) {
|
||||
if (attribute.isDefaultTrack() && AUDIO.equals(attribute.getType())) info.setDefaultAudioLane(attribute);
|
||||
if (attribute.isDefaultTrack() && AUDIO.equals(attribute.getType()))
|
||||
info.getDefaultAudioLanes().add(attribute);
|
||||
if (attribute.isDefaultTrack() && SUBTITLES.equals(attribute.getType()))
|
||||
info.setDefaultSubtitleLane(attribute);
|
||||
info.getDefaultSubtitleLanes().add(attribute);
|
||||
if (attribute.isForcedTrack() && SUBTITLES.equals(attribute.getType()))
|
||||
detectedForcedSubtitleLanes.add(attribute);
|
||||
}
|
||||
@@ -101,7 +101,7 @@ public class MkvFileProcessor implements FileProcessor {
|
||||
);
|
||||
}
|
||||
|
||||
private void detectDesiredConfiguration(FileInfoDto info, List<FileAttribute> nonForcedTracks) {
|
||||
private void detectDesiredTracks(FileInfoDto info, List<FileAttribute> nonForcedTracks) {
|
||||
for (AttributeConfig config : Config.getInstance().getAttributeConfig()) {
|
||||
FileAttribute desiredAudio = null;
|
||||
FileAttribute desiredSubtitle = null;
|
||||
@@ -125,14 +125,18 @@ public class MkvFileProcessor implements FileProcessor {
|
||||
sb.append(format("\"%s\" ", Config.getInstance().getPathFor(MkvToolNix.MKV_PROP_EDIT)));
|
||||
sb.append(format("\"%s\" ", file.getAbsolutePath()));
|
||||
if (fileInfo.isAudioDifferent()) {
|
||||
if (fileInfo.getDefaultAudioLane() != null) {
|
||||
sb.append(format(DISABLE_DEFAULT_TRACK, fileInfo.getDefaultAudioLane().getId()));
|
||||
if (fileInfo.getDefaultAudioLanes() != null && !fileInfo.getDefaultSubtitleLanes().isEmpty()) {
|
||||
for (FileAttribute track: fileInfo.getDefaultAudioLanes()) {
|
||||
sb.append(format(DISABLE_DEFAULT_TRACK, track.getId()));
|
||||
}
|
||||
}
|
||||
sb.append(format(ENABLE_DEFAULT_TRACK, fileInfo.getDesiredAudioLane().getId()));
|
||||
}
|
||||
if (fileInfo.isSubtitleDifferent()) {
|
||||
if (fileInfo.getDefaultSubtitleLane() != null) {
|
||||
sb.append(format(DISABLE_DEFAULT_TRACK, fileInfo.getDefaultSubtitleLane().getId()));
|
||||
if (fileInfo.getDefaultSubtitleLanes() != null && !fileInfo.getDefaultSubtitleLanes().isEmpty()) {
|
||||
for (FileAttribute track: fileInfo.getDefaultSubtitleLanes()) {
|
||||
sb.append(format(DISABLE_DEFAULT_TRACK, track.getId()));
|
||||
}
|
||||
}
|
||||
sb.append(format(ENABLE_DEFAULT_TRACK, fileInfo.getDesiredSubtitleLane().getId()));
|
||||
}
|
||||
@@ -143,6 +147,6 @@ public class MkvFileProcessor implements FileProcessor {
|
||||
}
|
||||
|
||||
InputStream inputstream = Runtime.getRuntime().exec(sb.toString()).getInputStream();
|
||||
LogUtils.ifDebug(log, IOUtils.toString(new InputStreamReader(inputstream)));
|
||||
log.debug(IOUtils.toString(new InputStreamReader(inputstream)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,21 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
@Log4j2
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class AttributeConfig {
|
||||
private final String audioLanguage;
|
||||
private final String subtitleLanguage;
|
||||
|
||||
public AttributeConfig(String audioLanguage, String subtitleLanguage) {
|
||||
this.audioLanguage = audioLanguage;
|
||||
this.subtitleLanguage = subtitleLanguage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuffer sb = new StringBuffer("AttributeConfig{");
|
||||
sb.append("audioLanguage='").append(audioLanguage).append('\'');
|
||||
sb.append(", subtitleLanguage='").append(subtitleLanguage).append('\'');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
return "AttributeConfig{"
|
||||
+ "audioLanguage='" + audioLanguage + '\''
|
||||
+ ", subtitleLanguage='" + subtitleLanguage + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
public enum ConfigProperty {
|
||||
CONFIG_PATH("config", "Path to config file"),
|
||||
LIBRARY("library", "Path to library"),
|
||||
@@ -15,11 +18,6 @@ public enum ConfigProperty {
|
||||
private final String property;
|
||||
private final String description;
|
||||
|
||||
ConfigProperty(String property, String description) {
|
||||
this.property = property;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String desc() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
@Log4j2
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class FileAttribute {
|
||||
private final int id;
|
||||
private final String language;
|
||||
@@ -13,25 +15,14 @@ public class FileAttribute {
|
||||
private final boolean forcedTrack;
|
||||
private final LaneType type;
|
||||
|
||||
public FileAttribute(int id, String language, String trackName, boolean defaultTrack, boolean forcedTrack, LaneType type) {
|
||||
this.id = id;
|
||||
this.language = language;
|
||||
this.trackName = trackName;
|
||||
this.defaultTrack = defaultTrack;
|
||||
this.forcedTrack = forcedTrack;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuffer sb = new StringBuffer("[");
|
||||
sb.append("id=").append(id);
|
||||
sb.append(", language='").append(language).append('\'');
|
||||
sb.append(", trackName='").append(trackName).append('\'');
|
||||
sb.append(", defaultTrack=").append(defaultTrack);
|
||||
sb.append(", forcedTrack=").append(forcedTrack);
|
||||
sb.append(", type=").append(type);
|
||||
sb.append(']');
|
||||
return sb.toString();
|
||||
return "[" + "id=" + id +
|
||||
", language='" + language + '\'' +
|
||||
", trackName='" + trackName + '\'' +
|
||||
", defaultTrack=" + defaultTrack +
|
||||
", forcedTrack=" + forcedTrack +
|
||||
", type=" + type +
|
||||
']';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,14 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class FileInfoDto {
|
||||
private FileAttribute defaultAudioLane;
|
||||
private FileAttribute defaultSubtitleLane;
|
||||
private Set<FileAttribute> defaultAudioLanes = new HashSet<>();
|
||||
private Set<FileAttribute> defaultSubtitleLanes = new HashSet<>();
|
||||
private Set<FileAttribute> desiredForcedSubtitleLanes;
|
||||
private FileAttribute desiredAudioLane;
|
||||
private FileAttribute desiredSubtitleLane;
|
||||
@@ -19,7 +20,7 @@ public class FileInfoDto {
|
||||
}
|
||||
|
||||
public boolean isAlreadySuitable() {
|
||||
return desiredAudioLane == defaultAudioLane && desiredSubtitleLane == defaultSubtitleLane;
|
||||
return defaultAudioLanes.contains(desiredAudioLane) && defaultSubtitleLanes.contains(desiredSubtitleLane);
|
||||
}
|
||||
|
||||
public boolean isChangeNecessary() {
|
||||
@@ -28,12 +29,12 @@ public class FileInfoDto {
|
||||
|
||||
public boolean isAudioDifferent() {
|
||||
return desiredAudioLane != null &&
|
||||
(defaultAudioLane == null || defaultAudioLane.getId() != desiredAudioLane.getId());
|
||||
(defaultAudioLanes == null || !defaultAudioLanes.contains(desiredAudioLane));
|
||||
}
|
||||
|
||||
public boolean isSubtitleDifferent() {
|
||||
return desiredSubtitleLane != null &&
|
||||
(defaultSubtitleLane == null || defaultSubtitleLane.getId() != desiredSubtitleLane.getId());
|
||||
(defaultSubtitleLanes == null || !defaultSubtitleLanes.contains(desiredSubtitleLane));
|
||||
}
|
||||
|
||||
public boolean areForcedTracksDifferent() {
|
||||
@@ -42,13 +43,11 @@ public class FileInfoDto {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuffer sb = new StringBuffer("[");
|
||||
sb.append("defaultAudioLane=").append(defaultAudioLane);
|
||||
sb.append(", defaultSubtitleLane=").append(defaultSubtitleLane);
|
||||
sb.append(", desiredForcedSubtitleLanes=").append(desiredForcedSubtitleLanes);
|
||||
sb.append(", desiredAudioLane=").append(desiredAudioLane);
|
||||
sb.append(", desiredSubtitleLane=").append(desiredSubtitleLane);
|
||||
sb.append(']');
|
||||
return sb.toString();
|
||||
return "[" + "defaultAudioLanes=" + defaultAudioLanes +
|
||||
", defaultSubtitleLanes=" + defaultSubtitleLanes +
|
||||
", desiredForcedSubtitleLanes=" + desiredForcedSubtitleLanes +
|
||||
", desiredAudioLane=" + desiredAudioLane +
|
||||
", desiredSubtitleLane=" + desiredSubtitleLane +
|
||||
']';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,4 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
|
||||
public enum LaneType {
|
||||
AUDIO,
|
||||
SUBTITLES;
|
||||
|
||||
LaneType() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
public enum MkvToolNix {
|
||||
MKV_MERGER("mkvmerge.exe"),
|
||||
MKV_PROP_EDIT("mkvpropedit.exe");
|
||||
|
||||
private final String file;
|
||||
|
||||
MkvToolNix(String file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return file;
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.util;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class LogUtils {
|
||||
|
||||
public static <T> void ifDebug(Logger log, T object) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(object);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user