2 Commits

Author SHA1 Message Date
3205969d3b Update readme 2025-05-02 03:33:14 +02:00
d24aedb0af Fix mkvpropedit call & improve logging 2025-05-02 03:27:24 +02:00
13 changed files with 49 additions and 32 deletions

View File

@@ -31,6 +31,7 @@ Windows & Linux (installed): `mkvaudiosubtitlechanger --library "X:/Files" --att
Keywords to identify commentary tracks (Defaults will be overwritten; Default: commentary, director)
-d, --filter-date=<filterDate>
only consider files created newer than entered date (format: "dd.MM.yyyy-HH:mm:ss")
--debug Enable debug logging
-e, --excluded-directory=<excludedDirectories>...
Directories to be excluded, combines with config file
--forced-keywords=<forcedKeywords>[, <forcedKeywords>...]...

View File

@@ -12,6 +12,8 @@ import jakarta.validation.Validation;
import jakarta.validation.Validator;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import picocli.CommandLine;
import java.util.Set;
@@ -39,6 +41,9 @@ public class Main implements Runnable {
@Override
public void run() {
if (config.isDebug()) {
Configurator.setRootLevel(Level.DEBUG);
}
validate();
Config.setInstance(config);
AttributeUpdaterKernel kernel = Config.getInstance().getCoherent() != null

View File

@@ -72,6 +72,9 @@ public class Config {
description = "Keywords to prefer specific subtitle tracks (Defaults will be overwritten; Default: ${DEFAULT-VALUE})")
private Set<String> preferredSubtitles;
@CommandLine.Option(names = {"--debug"}, description = "Enable debug logging")
private boolean debug;
@CommandLine.Option(names = {"-l", "--library"}, required = true, description = "path to library")
public void setLibraryPath(File libraryPath) {
if (!libraryPath.exists()) throw new CommandLine.ParameterException(spec.commandLine(), "Path does not exist: " + libraryPath.getAbsolutePath());

View File

@@ -40,7 +40,7 @@ public class DateValidator extends ConfigValidator<Date> {
YAML yaml = new YAML(lastExecutionFile);
return parse(yaml.getString(Config.getInstance().getNormalizedLibraryPath(), DateUtils.convert(DEFAULT_DATE)));
} catch (YamlInvalidContentException | IOException e) {
log.error("Couldn't open last-execution.properties");
log.error("Couldn't open last-execution.properties", e);
return INVALID_DATE;
}
}

View File

@@ -41,7 +41,7 @@ public class FileFilter {
BasicFileAttributes attributes = Files.readAttributes(pathName.toPath(), BasicFileAttributes.class);
return isNewer(DateUtils.convert(attributes.creationTime().toMillis()));
} catch (IOException e) {
log.warn("File attributes could not be read.", e);
log.warn("File attributes could not be read", e);
}
return true;
}

View File

@@ -28,10 +28,10 @@ public class MkvFileProcessor implements FileProcessor {
private static final SubtitleTrackComparator subtitleTrackComparator =
new SubtitleTrackComparator(Config.getInstance().getPreferredSubtitles().toArray(new String[0]));
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 DISABLE_FORCED_TRACK = "--edit track:%s --set flag-forced=0 ";
private static final String ENABLE_FORCED_TRACK = "--edit track:%s --set flag-forced=1 ";
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 DISABLE_FORCED_TRACK = "--edit track:%s --set flag-forced=0";
private static final String ENABLE_FORCED_TRACK = "--edit track:%s --set flag-forced=1";
@SuppressWarnings("unchecked")
@Override
@@ -47,7 +47,7 @@ public class MkvFileProcessor implements FileProcessor {
file.getAbsolutePath()
};
log.debug("{}", String.join(" ", command));
log.debug("Executing '{}': {}", file.getAbsolutePath(), String.join(" ", command));
InputStream inputStream = Runtime.getRuntime().exec(command)
.getInputStream();
jsonMap = mapper.readValue(inputStream, Map.class);
@@ -69,7 +69,7 @@ public class MkvFileProcessor implements FileProcessor {
}
}
log.debug(fileAttributes.toString());
log.debug("File attributes of '{}': {}", file.getAbsolutePath(), fileAttributes.toString());
} catch (IOException e) {
log.error("File could not be found or loaded: ", e);
System.out.println("File could not be found or loaded: " + file.getAbsolutePath());
@@ -149,46 +149,50 @@ public class MkvFileProcessor implements FileProcessor {
*/
@Override
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()));
List<String> command = new ArrayList<>();
command.add(Config.getInstance().getPathFor(MkvToolNix.MKV_PROP_EDIT));
command.add(String.format(file.getAbsolutePath()));
if (fileInfo.isAudioDifferent()) {
if (fileInfo.getExistingDefaultAudioLanes() != null && !fileInfo.getExistingDefaultAudioLanes().isEmpty()) {
for (FileAttribute track : fileInfo.getExistingDefaultAudioLanes()) {
sb.append(format(DISABLE_DEFAULT_TRACK, track.getId()));
command.addAll(format(DISABLE_DEFAULT_TRACK, track.getId()));
}
}
sb.append(format(ENABLE_DEFAULT_TRACK, fileInfo.getDesiredDefaultAudioLane().getId()));
command.addAll(format(ENABLE_DEFAULT_TRACK, fileInfo.getDesiredDefaultAudioLane().getId()));
}
if (!fileInfo.getExistingForcedAudioLanes().isEmpty()) {
for (FileAttribute track : fileInfo.getExistingForcedAudioLanes()) {
sb.append(format(DISABLE_FORCED_TRACK, track.getId()));
command.addAll(format(DISABLE_FORCED_TRACK, track.getId()));
}
}
if (fileInfo.isSubtitleDifferent()) {
if (fileInfo.getExistingDefaultSubtitleLanes() != null && !fileInfo.getExistingDefaultSubtitleLanes().isEmpty()) {
for (FileAttribute track : fileInfo.getExistingDefaultSubtitleLanes()) {
sb.append(format(DISABLE_DEFAULT_TRACK, track.getId()));
command.addAll(format(DISABLE_DEFAULT_TRACK, track.getId()));
}
}
if (fileInfo.getDesiredDefaultSubtitleLane() != null) {
sb.append(format(ENABLE_DEFAULT_TRACK, fileInfo.getDesiredDefaultSubtitleLane().getId()));
command.addAll(format(ENABLE_DEFAULT_TRACK, fileInfo.getDesiredDefaultSubtitleLane().getId()));
}
}
if (fileInfo.areForcedTracksDifferent()) {
for (FileAttribute attribute : fileInfo.getDesiredForcedSubtitleLanes()) {
sb.append(format(ENABLE_FORCED_TRACK, attribute.getId()));
for (FileAttribute track : fileInfo.getDesiredForcedSubtitleLanes()) {
command.addAll(format(ENABLE_FORCED_TRACK, track.getId()));
}
}
log.debug(sb.toString());
InputStream inputstream = Runtime.getRuntime().exec(sb.toString()).getInputStream();
log.debug("Executing '{}'", String.join(" ", command));
InputStream inputstream = Runtime.getRuntime().exec(command.toArray(new String[0])).getInputStream();
String output = IOUtils.toString(new InputStreamReader(inputstream));
log.debug(output);
log.debug("Result: {}", output);
if (output.contains("Error")) throw new MkvToolNixException(output);
}
private List<String> format(String format, Object... args) {
return Arrays.asList(String.format(format, args).split(" "));
}
}

View File

@@ -148,10 +148,10 @@ public abstract class AttributeUpdaterKernel {
try {
processor.update(fileInfo.getFile(), fileInfo);
statistic.success();
log.info("Updated {}", fileInfo.getFile().getAbsolutePath());
log.info("Commited {} to '{}'", fileInfo.getMatchedConfig().toStringShort(), fileInfo.getFile().getAbsolutePath());
} catch (IOException | MkvToolNixException e) {
statistic.failedChanging();
log.warn("File couldn't be updated: '{}', Error: {}", fileInfo.getFile().getAbsoluteFile(), e.getMessage());
log.warn("Couldn't commit {} to '{}'", fileInfo.getMatchedConfig().toStringShort(), fileInfo.getFile().getAbsoluteFile(), e);
}
}

View File

@@ -85,7 +85,7 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel {
if (fileInfos.stream().allMatch(elem -> ("OFF".equals(config.getSubtitleLanguage()) || elem.getDesiredDefaultSubtitleLane() != null)
&& elem.getDesiredDefaultAudioLane() != null)) {
log.info("Found {}/{} match for {}", config.getAudioLanguage(), config.getSubtitleLanguage(), file.getAbsolutePath());
log.info("Found {} match for {}", config.toStringShort(), file.getAbsolutePath());
fileInfos.forEach(this::updateFile);
return; // match found, end process here
}

View File

@@ -26,6 +26,10 @@ public class AttributeConfig {
return Objects.hash(audioLanguage, subtitleLanguage);
}
public String toStringShort() {
return audioLanguage + ":" + subtitleLanguage;
}
@Override
public String toString() {
return "AttributeConfig{"

View File

@@ -6,9 +6,9 @@ Configuration:
fileName: ${sys:user.home}/.local/mkvaudiosubtitlechanger/logs/application.log
filePattern: ${sys:user.home}/.local/mkvaudiosubtitlechanger/logs/archive/application-%d{yyyy-MM-dd}-%i.log.gz
PatternLayout:
Pattern: "%d{DEFAULT} | %-5level | %thread | %msg %n %throwable"
Pattern: "%d{DEFAULT} | %-5level | %thread | %C{1} | %msg %n %throwable"
ThresholdFilter:
level: info
level: debug
Policies:
OnStartupTriggeringPolicy:
minSize: 0

View File

@@ -4,7 +4,7 @@ Configuration:
Console:
name: Console_Out
PatternLayout:
Pattern: "%d{DEFAULT} | %-5level | %thread | %msg %n %throwable"
Pattern: "%d{DEFAULT} | %-5level | %thread | %C{1} | %msg %n %throwable"
ThresholdFilter:
level: debug
RollingFile:
@@ -12,7 +12,7 @@ Configuration:
fileName: logs/application.log
filePattern: logs/archive/application-%d{yyyy-MM-dd}-%i.log.gz
PatternLayout:
Pattern: "%d{DEFAULT} | %-5level | %thread | %msg %n %throwable"
Pattern: "%d{DEFAULT} | %-5level | %thread | %C{1} | %msg %n %throwable"
ThresholdFilter:
level: debug
Policies:

View File

@@ -6,9 +6,9 @@ Configuration:
fileName: ${sys:user.home}/AppData/Roaming/MKVAudioSubtitleChanger/logs/application.log
filePattern: ${sys:user.home}/AppData/Roaming/MKVAudioSubtitleChanger/logs/archive/application-%d{yyyy-MM-dd}-%i.log.gz
PatternLayout:
Pattern: "%d{DEFAULT} | %-5level | %thread | %msg %n %throwable"
Pattern: "%d{DEFAULT} | %-5level | %thread | %C{1} | %msg %n %throwable"
ThresholdFilter:
level: info
level: debug
Policies:
OnStartupTriggeringPolicy:
minSize: 0

View File

@@ -6,9 +6,9 @@ Configuration:
fileName: logs/application.log
filePattern: logs/archive/application-%d{yyyy-MM-dd}-%i.log.gz
PatternLayout:
Pattern: "%d{DEFAULT} | %-5level | %thread | %msg %n %throwable"
Pattern: "%d{DEFAULT} | %-5level | %thread | %C{1} | %msg %n %throwable"
ThresholdFilter:
level: info
level: debug
Policies:
OnStartupTriggeringPolicy:
minSize: 0