Improve general code quality

This commit is contained in:
2022-04-24 16:47:55 +02:00
parent a99abd5989
commit 10954f07a4
14 changed files with 75 additions and 118 deletions

View File

@@ -8,19 +8,16 @@ assignees: RatzzFatzz
--- ---
**Environment** **Environment**
Java Version: [e.g. 8, 11, 17] Java Version: [e.g. 8, 11, 17]
MKV Audio Subtitle Changer version: [e.g. v1.1, v2.0] MKV Audio Subtitle Changer version: [e.g. v1.1, v2.0]
**Describe the bug** **Describe the bug**
A clear and concise description of what the bug is. A clear and concise description of what the bug is.
**Expected behavior** **Expected behavior**
A clear and concise description of what you expected to happen. A clear and concise description of what you expected to happen.
**Additional context** **Additional context**
Add any other context about the problem here. Add any other context about the problem here.
### Please attach logs

View File

@@ -7,6 +7,5 @@ labels: feature-request
--- ---
**Describe your feature** **Describe your feature**
A clear and concise description of the expected behaviour. A clear and concise description of the expected behaviour.

View File

@@ -11,7 +11,7 @@ This program helps to change audio and subtitle lines of mkv files.
### Requirements ### Requirements
- Java 8 or higher - Java 11 or higher
- mkvtoolnix installation - mkvtoolnix installation
### Running ### Running

View File

@@ -32,14 +32,6 @@
</testResources> </testResources>
<plugins> <plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>

View File

@@ -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();
}
}

View File

@@ -1,6 +1,5 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl; package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import java.io.File; import java.io.File;
@@ -15,27 +14,17 @@ import java.util.stream.Stream;
@Log4j2 @Log4j2
public class MkvFileCollector implements FileCollector { public class MkvFileCollector implements FileCollector {
private static final String[] fileExtensions = new String[]{".mkv", ".mka", ".mks", ".mk3d"};
@Override @Override
public List<File> loadFiles(String path) { public List<File> loadFiles(String path) {
File file = new File(path); try (Stream<Path> paths = Files.walk(Paths.get(path))) {
if (file.isFile() && file.getAbsolutePath().endsWith(".mkv")) { return paths.filter(Files::isRegularFile)
return new ArrayList<>() {{ .map(Path::toFile)
add(file); .filter(file -> FileFilter.accept(file, fileExtensions))
}}; .collect(Collectors.toList());
} else if (file.isDirectory()) { } catch (IOException e) {
try (Stream<Path> paths = Files.walk(Paths.get(path))) { log.error("Couldn't find file or directory!", e);
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 {
return new ArrayList<>(); return new ArrayList<>();
} }
} }

View File

@@ -2,7 +2,6 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.*;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.LogUtils;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils; 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
log.error("File could not be found or loaded!"); 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)) .filter(elem -> !StringUtils.containsAnyIgnoreCase(elem.getTrackName(), forcedKeywords))
.collect(Collectors.toList()); .collect(Collectors.toList());
detectCurrentConfiguration(attributes, info, nonForcedTracks); detectDefaultTracks(attributes, info, nonForcedTracks);
detectDesiredConfiguration(info, nonForcedTracks); detectDesiredTracks(info, nonForcedTracks);
LogUtils.ifDebug(log, info); log.debug(info);
return 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<>(); Set<FileAttribute> detectedForcedSubtitleLanes = new HashSet<>();
for (FileAttribute attribute : attributes) { 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())) if (attribute.isDefaultTrack() && SUBTITLES.equals(attribute.getType()))
info.setDefaultSubtitleLane(attribute); info.getDefaultSubtitleLanes().add(attribute);
if (attribute.isForcedTrack() && SUBTITLES.equals(attribute.getType())) if (attribute.isForcedTrack() && SUBTITLES.equals(attribute.getType()))
detectedForcedSubtitleLanes.add(attribute); 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()) { for (AttributeConfig config : Config.getInstance().getAttributeConfig()) {
FileAttribute desiredAudio = null; FileAttribute desiredAudio = null;
FileAttribute desiredSubtitle = 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\" ", Config.getInstance().getPathFor(MkvToolNix.MKV_PROP_EDIT)));
sb.append(format("\"%s\" ", file.getAbsolutePath())); sb.append(format("\"%s\" ", file.getAbsolutePath()));
if (fileInfo.isAudioDifferent()) { if (fileInfo.isAudioDifferent()) {
if (fileInfo.getDefaultAudioLane() != null) { if (fileInfo.getDefaultAudioLanes() != null && !fileInfo.getDefaultSubtitleLanes().isEmpty()) {
sb.append(format(DISABLE_DEFAULT_TRACK, fileInfo.getDefaultAudioLane().getId())); for (FileAttribute track: fileInfo.getDefaultAudioLanes()) {
sb.append(format(DISABLE_DEFAULT_TRACK, track.getId()));
}
} }
sb.append(format(ENABLE_DEFAULT_TRACK, fileInfo.getDesiredAudioLane().getId())); sb.append(format(ENABLE_DEFAULT_TRACK, fileInfo.getDesiredAudioLane().getId()));
} }
if (fileInfo.isSubtitleDifferent()) { if (fileInfo.isSubtitleDifferent()) {
if (fileInfo.getDefaultSubtitleLane() != null) { if (fileInfo.getDefaultSubtitleLanes() != null && !fileInfo.getDefaultSubtitleLanes().isEmpty()) {
sb.append(format(DISABLE_DEFAULT_TRACK, fileInfo.getDefaultSubtitleLane().getId())); for (FileAttribute track: fileInfo.getDefaultSubtitleLanes()) {
sb.append(format(DISABLE_DEFAULT_TRACK, track.getId()));
}
} }
sb.append(format(ENABLE_DEFAULT_TRACK, fileInfo.getDesiredSubtitleLane().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(); InputStream inputstream = Runtime.getRuntime().exec(sb.toString()).getInputStream();
LogUtils.ifDebug(log, IOUtils.toString(new InputStreamReader(inputstream))); log.debug(IOUtils.toString(new InputStreamReader(inputstream)));
} }
} }

View File

@@ -1,25 +1,21 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.model; package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
@Log4j2 @Log4j2
@Getter @Getter
@AllArgsConstructor
public class AttributeConfig { public class AttributeConfig {
private final String audioLanguage; private final String audioLanguage;
private final String subtitleLanguage; private final String subtitleLanguage;
public AttributeConfig(String audioLanguage, String subtitleLanguage) {
this.audioLanguage = audioLanguage;
this.subtitleLanguage = subtitleLanguage;
}
@Override @Override
public String toString() { public String toString() {
final StringBuffer sb = new StringBuffer("AttributeConfig{"); return "AttributeConfig{"
sb.append("audioLanguage='").append(audioLanguage).append('\''); + "audioLanguage='" + audioLanguage + '\''
sb.append(", subtitleLanguage='").append(subtitleLanguage).append('\''); + ", subtitleLanguage='" + subtitleLanguage + '\'' +
sb.append('}'); '}';
return sb.toString();
} }
} }

View File

@@ -1,5 +1,8 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.model; package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public enum ConfigProperty { public enum ConfigProperty {
CONFIG_PATH("config", "Path to config file"), CONFIG_PATH("config", "Path to config file"),
LIBRARY("library", "Path to library"), LIBRARY("library", "Path to library"),
@@ -15,11 +18,6 @@ public enum ConfigProperty {
private final String property; private final String property;
private final String description; private final String description;
ConfigProperty(String property, String description) {
this.property = property;
this.description = description;
}
public String desc() { public String desc() {
return description; return description;
} }

View File

@@ -1,10 +1,12 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.model; package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
@Log4j2 @Log4j2
@Getter @Getter
@AllArgsConstructor
public class FileAttribute { public class FileAttribute {
private final int id; private final int id;
private final String language; private final String language;
@@ -13,25 +15,14 @@ public class FileAttribute {
private final boolean forcedTrack; private final boolean forcedTrack;
private final LaneType type; 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 @Override
public String toString() { public String toString() {
final StringBuffer sb = new StringBuffer("["); return "[" + "id=" + id +
sb.append("id=").append(id); ", language='" + language + '\'' +
sb.append(", language='").append(language).append('\''); ", trackName='" + trackName + '\'' +
sb.append(", trackName='").append(trackName).append('\''); ", defaultTrack=" + defaultTrack +
sb.append(", defaultTrack=").append(defaultTrack); ", forcedTrack=" + forcedTrack +
sb.append(", forcedTrack=").append(forcedTrack); ", type=" + type +
sb.append(", type=").append(type); ']';
sb.append(']');
return sb.toString();
} }
} }

View File

@@ -3,13 +3,14 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
@Getter @Getter
@Setter @Setter
public class FileInfoDto { public class FileInfoDto {
private FileAttribute defaultAudioLane; private Set<FileAttribute> defaultAudioLanes = new HashSet<>();
private FileAttribute defaultSubtitleLane; private Set<FileAttribute> defaultSubtitleLanes = new HashSet<>();
private Set<FileAttribute> desiredForcedSubtitleLanes; private Set<FileAttribute> desiredForcedSubtitleLanes;
private FileAttribute desiredAudioLane; private FileAttribute desiredAudioLane;
private FileAttribute desiredSubtitleLane; private FileAttribute desiredSubtitleLane;
@@ -19,7 +20,7 @@ public class FileInfoDto {
} }
public boolean isAlreadySuitable() { public boolean isAlreadySuitable() {
return desiredAudioLane == defaultAudioLane && desiredSubtitleLane == defaultSubtitleLane; return defaultAudioLanes.contains(desiredAudioLane) && defaultSubtitleLanes.contains(desiredSubtitleLane);
} }
public boolean isChangeNecessary() { public boolean isChangeNecessary() {
@@ -28,12 +29,12 @@ public class FileInfoDto {
public boolean isAudioDifferent() { public boolean isAudioDifferent() {
return desiredAudioLane != null && return desiredAudioLane != null &&
(defaultAudioLane == null || defaultAudioLane.getId() != desiredAudioLane.getId()); (defaultAudioLanes == null || !defaultAudioLanes.contains(desiredAudioLane));
} }
public boolean isSubtitleDifferent() { public boolean isSubtitleDifferent() {
return desiredSubtitleLane != null && return desiredSubtitleLane != null &&
(defaultSubtitleLane == null || defaultSubtitleLane.getId() != desiredSubtitleLane.getId()); (defaultSubtitleLanes == null || !defaultSubtitleLanes.contains(desiredSubtitleLane));
} }
public boolean areForcedTracksDifferent() { public boolean areForcedTracksDifferent() {
@@ -42,13 +43,11 @@ public class FileInfoDto {
@Override @Override
public String toString() { public String toString() {
final StringBuffer sb = new StringBuffer("["); return "[" + "defaultAudioLanes=" + defaultAudioLanes +
sb.append("defaultAudioLane=").append(defaultAudioLane); ", defaultSubtitleLanes=" + defaultSubtitleLanes +
sb.append(", defaultSubtitleLane=").append(defaultSubtitleLane); ", desiredForcedSubtitleLanes=" + desiredForcedSubtitleLanes +
sb.append(", desiredForcedSubtitleLanes=").append(desiredForcedSubtitleLanes); ", desiredAudioLane=" + desiredAudioLane +
sb.append(", desiredAudioLane=").append(desiredAudioLane); ", desiredSubtitleLane=" + desiredSubtitleLane +
sb.append(", desiredSubtitleLane=").append(desiredSubtitleLane); ']';
sb.append(']');
return sb.toString();
} }
} }

View File

@@ -3,12 +3,4 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
public enum LaneType { public enum LaneType {
AUDIO, AUDIO,
SUBTITLES; SUBTITLES;
LaneType() {
}
@Override
public String toString() {
return name();
}
} }

View File

@@ -1,15 +1,14 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.model; package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public enum MkvToolNix { public enum MkvToolNix {
MKV_MERGER("mkvmerge.exe"), MKV_MERGER("mkvmerge.exe"),
MKV_PROP_EDIT("mkvpropedit.exe"); MKV_PROP_EDIT("mkvpropedit.exe");
private final String file; private final String file;
MkvToolNix(String file) {
this.file = file;
}
@Override @Override
public String toString() { public String toString() {
return file; return file;

View 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);
}
}
}