Fix mkvpropedit call & improve logging

This commit is contained in:
2025-05-02 03:27:24 +02:00
parent b86c7b98a5
commit d24aedb0af
12 changed files with 48 additions and 32 deletions

View File

@@ -12,6 +12,8 @@ import jakarta.validation.Validation;
import jakarta.validation.Validator; import jakarta.validation.Validator;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import picocli.CommandLine; import picocli.CommandLine;
import java.util.Set; import java.util.Set;
@@ -39,6 +41,9 @@ public class Main implements Runnable {
@Override @Override
public void run() { public void run() {
if (config.isDebug()) {
Configurator.setRootLevel(Level.DEBUG);
}
validate(); validate();
Config.setInstance(config); Config.setInstance(config);
AttributeUpdaterKernel kernel = Config.getInstance().getCoherent() != null 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})") description = "Keywords to prefer specific subtitle tracks (Defaults will be overwritten; Default: ${DEFAULT-VALUE})")
private Set<String> preferredSubtitles; 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") @CommandLine.Option(names = {"-l", "--library"}, required = true, description = "path to library")
public void setLibraryPath(File libraryPath) { public void setLibraryPath(File libraryPath) {
if (!libraryPath.exists()) throw new CommandLine.ParameterException(spec.commandLine(), "Path does not exist: " + libraryPath.getAbsolutePath()); 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); YAML yaml = new YAML(lastExecutionFile);
return parse(yaml.getString(Config.getInstance().getNormalizedLibraryPath(), DateUtils.convert(DEFAULT_DATE))); return parse(yaml.getString(Config.getInstance().getNormalizedLibraryPath(), DateUtils.convert(DEFAULT_DATE)));
} catch (YamlInvalidContentException | IOException e) { } catch (YamlInvalidContentException | IOException e) {
log.error("Couldn't open last-execution.properties"); log.error("Couldn't open last-execution.properties", e);
return INVALID_DATE; return INVALID_DATE;
} }
} }

View File

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

View File

@@ -28,10 +28,10 @@ public class MkvFileProcessor implements FileProcessor {
private static final SubtitleTrackComparator subtitleTrackComparator = private static final SubtitleTrackComparator subtitleTrackComparator =
new SubtitleTrackComparator(Config.getInstance().getPreferredSubtitles().toArray(new String[0])); 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 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_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 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 ENABLE_FORCED_TRACK = "--edit track:%s --set flag-forced=1";
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
@@ -47,7 +47,7 @@ public class MkvFileProcessor implements FileProcessor {
file.getAbsolutePath() file.getAbsolutePath()
}; };
log.debug("{}", String.join(" ", command)); log.debug("Executing '{}': {}", file.getAbsolutePath(), String.join(" ", command));
InputStream inputStream = Runtime.getRuntime().exec(command) InputStream inputStream = Runtime.getRuntime().exec(command)
.getInputStream(); .getInputStream();
jsonMap = mapper.readValue(inputStream, Map.class); 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) { } catch (IOException e) {
log.error("File could not be found or loaded: ", e); log.error("File could not be found or loaded: ", e);
System.out.println("File could not be found or loaded: " + file.getAbsolutePath()); System.out.println("File could not be found or loaded: " + file.getAbsolutePath());
@@ -149,46 +149,50 @@ public class MkvFileProcessor implements FileProcessor {
*/ */
@Override @Override
public void update(File file, FileInfoDto fileInfo) throws IOException, MkvToolNixException { public void update(File file, FileInfoDto fileInfo) throws IOException, MkvToolNixException {
StringBuilder sb = new StringBuilder(); List<String> command = new ArrayList<>();
sb.append(format("\"%s\" ", Config.getInstance().getPathFor(MkvToolNix.MKV_PROP_EDIT))); command.add(Config.getInstance().getPathFor(MkvToolNix.MKV_PROP_EDIT));
sb.append(format("\"%s\" ", file.getAbsolutePath())); command.add(String.format(file.getAbsolutePath()));
if (fileInfo.isAudioDifferent()) { if (fileInfo.isAudioDifferent()) {
if (fileInfo.getExistingDefaultAudioLanes() != null && !fileInfo.getExistingDefaultAudioLanes().isEmpty()) { if (fileInfo.getExistingDefaultAudioLanes() != null && !fileInfo.getExistingDefaultAudioLanes().isEmpty()) {
for (FileAttribute track : fileInfo.getExistingDefaultAudioLanes()) { 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()) { if (!fileInfo.getExistingForcedAudioLanes().isEmpty()) {
for (FileAttribute track : fileInfo.getExistingForcedAudioLanes()) { 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.isSubtitleDifferent()) {
if (fileInfo.getExistingDefaultSubtitleLanes() != null && !fileInfo.getExistingDefaultSubtitleLanes().isEmpty()) { if (fileInfo.getExistingDefaultSubtitleLanes() != null && !fileInfo.getExistingDefaultSubtitleLanes().isEmpty()) {
for (FileAttribute track : fileInfo.getExistingDefaultSubtitleLanes()) { 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) { 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()) { if (fileInfo.areForcedTracksDifferent()) {
for (FileAttribute attribute : fileInfo.getDesiredForcedSubtitleLanes()) { for (FileAttribute track : fileInfo.getDesiredForcedSubtitleLanes()) {
sb.append(format(ENABLE_FORCED_TRACK, attribute.getId())); command.addAll(format(ENABLE_FORCED_TRACK, track.getId()));
} }
} }
log.debug(sb.toString()); log.debug("Executing '{}'", String.join(" ", command));
InputStream inputstream = Runtime.getRuntime().exec(sb.toString()).getInputStream(); InputStream inputstream = Runtime.getRuntime().exec(command.toArray(new String[0])).getInputStream();
String output = IOUtils.toString(new InputStreamReader(inputstream)); String output = IOUtils.toString(new InputStreamReader(inputstream));
log.debug(output); log.debug("Result: {}", output);
if (output.contains("Error")) throw new MkvToolNixException(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 { try {
processor.update(fileInfo.getFile(), fileInfo); processor.update(fileInfo.getFile(), fileInfo);
statistic.success(); statistic.success();
log.info("Updated {}", fileInfo.getFile().getAbsolutePath()); log.info("Commited {} to '{}'", fileInfo.getMatchedConfig().toStringShort(), fileInfo.getFile().getAbsolutePath());
} catch (IOException | MkvToolNixException e) { } catch (IOException | MkvToolNixException e) {
statistic.failedChanging(); 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) if (fileInfos.stream().allMatch(elem -> ("OFF".equals(config.getSubtitleLanguage()) || elem.getDesiredDefaultSubtitleLane() != null)
&& elem.getDesiredDefaultAudioLane() != 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); fileInfos.forEach(this::updateFile);
return; // match found, end process here return; // match found, end process here
} }

View File

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

View File

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

View File

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

View File

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

View File

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