46 lines
1.5 KiB
Java
46 lines
1.5 KiB
Java
package com.github.gtache.plex;
|
|
|
|
import com.github.gtache.ffmpeg.Compression;
|
|
import com.github.gtache.ffmpeg.Converter;
|
|
import com.github.gtache.ffmpeg.Format;
|
|
import com.github.gtache.ffmpeg.Quality;
|
|
|
|
import javax.inject.Inject;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
import java.util.Objects;
|
|
import java.util.regex.Pattern;
|
|
|
|
public class PlexCompresser implements Compresser {
|
|
|
|
private static final Pattern EXTENSION_PATTERN = Pattern.compile("\\.([A-Za-z0-9]{2,3})$");
|
|
private final Converter converter;
|
|
|
|
@Inject
|
|
PlexCompresser(final Converter converter) {
|
|
this.converter = Objects.requireNonNull(converter);
|
|
}
|
|
|
|
@Override
|
|
public void compress(final String file) throws IOException {
|
|
final var matcher = EXTENSION_PATTERN.matcher(file);
|
|
if (matcher.find()) {
|
|
final var extension = matcher.group(1);
|
|
final var newName = matcher.replaceAll("-archived." + extension);
|
|
converter.convertTo(file, newName, Format.H265, Quality.HIGH, Compression.HIGH);
|
|
} else {
|
|
throw new IllegalArgumentException("File doesn't have an extension : " + file);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void compressDirectory(final String path) throws IOException {
|
|
try (final var files = Files.list(Paths.get(path))) {
|
|
for (final var file : files.toList()) {
|
|
compress(file.toFile().getAbsolutePath());
|
|
}
|
|
}
|
|
}
|
|
}
|