Generate thumbnails for Jellyfin shows

This commit is contained in:
2021-05-25 23:10:09 +02:00
parent b3bc21cc21
commit db5ceb7aea

View File

@@ -30,6 +30,8 @@ public class ThumbnailService {
FileInfo fileInfo = (FileInfo) fileInfoBase;
if (fileInfo.filename.endsWith(".zip")) {
return this.generateThumbnailFromZip(fileInfo, thumbPath);
} else {
return this.generateThumbnailForFile(fileInfo, thumbPath);
}
} else if (fileInfoBase instanceof FolderInfo) {
return this.generateThumbnailForFolder((FolderInfo) fileInfoBase, thumbPath);
@@ -37,6 +39,32 @@ public class ThumbnailService {
return false;
}
public boolean generateThumbnailForFile(FileInfo fileInfo, Path thumbPath) {
Path filePath = Paths.get(fileApiConfiguration.directory()
+ URLDecoder.decode(fileInfo.directUrl, StandardCharsets.UTF_8));
Path imgPath = filePath.resolveSibling(
filePath.getFileName().toString().replaceAll("\\.[^.]*$", "-thumb.jpg"));
if (Files.exists(imgPath)) {
try {
thumbPath.getParent().toFile().mkdirs();
ConvertCmd convert = new ConvertCmd();
IMOperation op = new IMOperation();
op.addImage(imgPath.toString());
op.resize(200, null);
op.addImage(thumbPath.toString());
convert.run(op);
return true;
} catch (IOException | InterruptedException | IM4JavaException e) {
e.printStackTrace();
}
}
return false;
}
public boolean generateThumbnailFromZip(FileInfo fileInfo, Path thumbPath) {
Path zipPath = Paths.get(fileApiConfiguration.directory()
+ URLDecoder.decode(fileInfo.directUrl, StandardCharsets.UTF_8));
@@ -77,10 +105,30 @@ public class ThumbnailService {
+ URLDecoder.decode(folderInfo.directUrl, StandardCharsets.UTF_8));
try {
Optional<Path> imgPath = Files.list(folderPath).sorted()
.filter(p -> !Files.isDirectory(p)
&& (p.toString().endsWith(".png") || p.toString().endsWith(".jpg")))
.findFirst();
Optional<Path> imgPath;
Path posterPath = folderPath.resolve("poster.jpg");
if (Files.exists(posterPath)) {
// Check for series poster
imgPath = Optional.of(posterPath);
} else if (folderPath.getFileName().toString().startsWith("Season ")
|| (folderPath.getFileName().toString().equals("Specials"))) {
// Season folder
String folderName = folderPath.getFileName().toString();
if (folderName.startsWith("Season ")) {
String seasonNumber =
String.format("%02d", Integer.parseInt(folderName.substring(7)));
imgPath = Optional
.of(folderPath.resolveSibling("season" + seasonNumber + "-poster.jpg"));
} else {
imgPath = Optional.of(folderPath.resolveSibling("season-specials-poster.jpg"));
}
} else {
imgPath = Files.list(folderPath).sorted()
.filter(p -> !Files.isDirectory(p)
&& (p.toString().endsWith(".png") || p.toString().endsWith(".jpg")))
.findFirst();
}
if (imgPath.isPresent()) {
thumbPath.getParent().toFile().mkdirs();
@@ -93,8 +141,8 @@ public class ThumbnailService {
return true;
}
} catch (IOException | InterruptedException | IM4JavaException ignored) {
ignored.printStackTrace();
} catch (IOException | InterruptedException | IM4JavaException e) {
e.printStackTrace();
}
return false;