Skip to content

Commit

Permalink
Improve general code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
RatzzFatzz committed Apr 24, 2022
1 parent a99abd5 commit 10954f0
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 118 deletions.
7 changes: 2 additions & 5 deletions .github/ISSUE_TEMPLATE/default-bug-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@ assignees: RatzzFatzz
---

**Environment**

Java Version: [e.g. 8, 11, 17]

MKV Audio Subtitle Changer version: [e.g. v1.1, v2.0]

**Describe the bug**

A clear and concise description of what the bug is.

**Expected behavior**

A clear and concise description of what you expected to happen.

**Additional context**

Add any other context about the problem here.

### Please attach logs
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/default-feature-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ labels: feature-request
---

**Describe your feature**

A clear and concise description of the expected behaviour.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This program helps to change audio and subtitle lines of mkv files.

### Requirements

- Java 8 or higher
- Java 11 or higher
- mkvtoolnix installation

### Running
Expand Down
8 changes: 0 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@
</testResources>

<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;

import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import lombok.extern.log4j.Log4j2;

import java.io.File;
Expand All @@ -15,27 +14,17 @@

@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<>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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;
Expand Down Expand Up @@ -62,7 +61,7 @@ public List<FileAttribute> loadAttributes(File file) {
}
}

LogUtils.ifDebug(log, fileAttributes);
log.debug(fileAttributes);
} catch (IOException e) {
e.printStackTrace();
log.error("File could not be found or loaded!");
Expand All @@ -77,19 +76,20 @@ public FileInfoDto filterAttributes(List<FileAttribute> attributes) {
.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);
}
Expand All @@ -101,7 +101,7 @@ private void detectCurrentConfiguration(List<FileAttribute> attributes, FileInfo
);
}

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;
Expand All @@ -125,14 +125,18 @@ public void update(File file, FileInfoDto fileInfo) throws IOException {
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()));
}
Expand All @@ -143,6 +147,6 @@ public void update(File file, FileInfoDto fileInfo) throws IOException {
}

InputStream inputstream = Runtime.getRuntime().exec(sb.toString()).getInputStream();
LogUtils.ifDebug(log, IOUtils.toString(new InputStreamReader(inputstream)));
log.debug(IOUtils.toString(new InputStreamReader(inputstream)));
}
}
Original file line number Diff line number Diff line change
@@ -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 + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -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"),
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 +
']';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
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;
Expand All @@ -19,7 +20,7 @@ public boolean isUnableToApplyConfig() {
}

public boolean isAlreadySuitable() {
return desiredAudioLane == defaultAudioLane && desiredSubtitleLane == defaultSubtitleLane;
return defaultAudioLanes.contains(desiredAudioLane) && defaultSubtitleLanes.contains(desiredSubtitleLane);
}

public boolean isChangeNecessary() {
Expand All @@ -28,12 +29,12 @@ public boolean isChangeNecessary() {

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() {
Expand All @@ -42,13 +43,11 @@ public boolean areForcedTracksDifferent() {

@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 +
']';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,4 @@
public enum LaneType {
AUDIO,
SUBTITLES;

LaneType() {
}

@Override
public String toString() {
return name();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

This file was deleted.

0 comments on commit 10954f0

Please sign in to comment.