diff --git a/src/main/java/com/annimon/ffmpegbot/TextUtils.java b/src/main/java/com/annimon/ffmpegbot/TextUtils.java index 03c9ad5..467f450 100644 --- a/src/main/java/com/annimon/ffmpegbot/TextUtils.java +++ b/src/main/java/com/annimon/ffmpegbot/TextUtils.java @@ -1,6 +1,7 @@ package com.annimon.ffmpegbot; import java.text.DecimalFormat; +import java.util.StringJoiner; public class TextUtils { @@ -16,4 +17,17 @@ public class TextUtils { return new DecimalFormat("#,##0.#") .format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; } + + public static String readableDuration(int value) { + if (value <= 0) return "0"; + if (value < 60) return value + "s"; + final var joiner = new StringJoiner(":"); + final int hours = value / 3600; + final int minutes = (value % 3600) / 60; + final int seconds = value % 60; + if (hours > 0) joiner.add("%02d".formatted(hours)); + joiner.add("%02d".formatted(minutes)); + joiner.add("%02d".formatted(seconds)); + return joiner.toString(); + } } diff --git a/src/main/java/com/annimon/ffmpegbot/commands/ffmpeg/MediaProcessingBundle.java b/src/main/java/com/annimon/ffmpegbot/commands/ffmpeg/MediaProcessingBundle.java index bdae8e1..7cc87bf 100644 --- a/src/main/java/com/annimon/ffmpegbot/commands/ffmpeg/MediaProcessingBundle.java +++ b/src/main/java/com/annimon/ffmpegbot/commands/ffmpeg/MediaProcessingBundle.java @@ -50,9 +50,7 @@ public class MediaProcessingBundle implements CommandBundle { final var session = new MediaSession(); session.setChatId(message.getChatId()); - session.setFileId(fileInfo.fileId()); - session.setFileType(fileInfo.fileType()); - session.setOriginalFilename(fileInfo.filename()); + session.fromFileInfo(fileInfo); session.setParams(Resolver.resolveParameters(fileInfo.fileType())); final var result = Methods.sendMessage() diff --git a/src/main/java/com/annimon/ffmpegbot/session/FileInfo.java b/src/main/java/com/annimon/ffmpegbot/session/FileInfo.java index 04c75ef..fe23843 100644 --- a/src/main/java/com/annimon/ffmpegbot/session/FileInfo.java +++ b/src/main/java/com/annimon/ffmpegbot/session/FileInfo.java @@ -1,4 +1,8 @@ package com.annimon.ffmpegbot.session; -public record FileInfo(FileType fileType, String fileId, String filename) { +public record FileInfo(FileType fileType, String fileId, String filename, + Long fileSize, Integer duration, Integer width, Integer height) { + public FileInfo(FileType fileType, String fileId, String filename, Long fileSize, Integer duration) { + this(fileType, fileId, filename, fileSize, duration, null, null); + } } diff --git a/src/main/java/com/annimon/ffmpegbot/session/MediaSession.java b/src/main/java/com/annimon/ffmpegbot/session/MediaSession.java index 6d7dc64..f7b3be9 100644 --- a/src/main/java/com/annimon/ffmpegbot/session/MediaSession.java +++ b/src/main/java/com/annimon/ffmpegbot/session/MediaSession.java @@ -7,8 +7,7 @@ import java.io.File; import java.util.List; import java.util.StringJoiner; -import static com.annimon.ffmpegbot.TextUtils.readableFileSize; -import static com.annimon.ffmpegbot.TextUtils.safeHtml; +import static com.annimon.ffmpegbot.TextUtils.*; public class MediaSession { // Session key @@ -18,6 +17,9 @@ public class MediaSession { private FileType fileType; private String fileId; private String originalFilename; + private Long fileSize; + private Integer duration; + private String dimensions; // Parameters private List> params; private final InputParameters inputParams = new InputParameters(); @@ -27,6 +29,15 @@ public class MediaSession { // Status private String status; + public void fromFileInfo(FileInfo fileInfo) { + this.setFileId(fileInfo.fileId()); + this.setFileType(fileInfo.fileType()); + this.setFileSize(fileInfo.fileSize()); + this.setDuration(fileInfo.duration()); + this.setDimensions(fileInfo.width(), fileInfo.height()); + this.setOriginalFilename(fileInfo.filename()); + } + public long getChatId() { return chatId; } @@ -63,8 +74,20 @@ public class MediaSession { this.originalFilename = originalFilename; } - public String getOriginalFilename() { - return originalFilename; + public void setFileSize(Long fileSize) { + this.fileSize = fileSize; + } + + public void setDuration(Integer duration) { + this.duration = duration; + } + + public void setDimensions(Integer width, Integer height) { + if (width == null && height == null) { + this.dimensions = null; + } else { + this.dimensions = (width != null ? width : "?") + "x" + (height != null ? height : "?"); + } } public List> getParams() { @@ -99,25 +122,29 @@ public class MediaSession { this.outputFile = outputFile; } - public String getStatus() { - return status; - } - public void setStatus(String status) { this.status = status; } public StringJoiner describe() { final var joiner = new StringJoiner("\n"); - joiner.add("File ID: %s".formatted(safeHtml(fileId))); joiner.add("Type: %s".formatted(fileType)); + if (fileSize != null && fileSize > 0) { + joiner.add("File size: %s".formatted(readableFileSize(fileSize))); + } + if (duration != null && duration > 0) { + joiner.add("Duration: %s".formatted(readableDuration(duration))); + } + if (dimensions != null) { + joiner.add("Dimensions: %s".formatted(dimensions)); + } joiner.merge(inputParams.describe()); if (originalFilename != null) { joiner.add("Filename: %s".formatted(safeHtml(originalFilename))); } if (inputFile != null && inputFile.canRead()) { joiner.add("Input file: %s".formatted(safeHtml(inputFile.getName()))); - joiner.add("Size: %s".formatted(readableFileSize(inputFile.length()))); + joiner.add("Input Size: %s".formatted(readableFileSize(inputFile.length()))); } if (outputFile != null && outputFile.canRead()) { joiner.add("Output size: %s".formatted(readableFileSize(outputFile.length()))); diff --git a/src/main/java/com/annimon/ffmpegbot/session/Resolver.java b/src/main/java/com/annimon/ffmpegbot/session/Resolver.java index 833dbb7..623c74d 100644 --- a/src/main/java/com/annimon/ffmpegbot/session/Resolver.java +++ b/src/main/java/com/annimon/ffmpegbot/session/Resolver.java @@ -14,19 +14,23 @@ public class Resolver { public static FileInfo resolveFileInfo(@NotNull Message message) { if (message.hasAnimation()) { final var att = message.getAnimation(); - return new FileInfo(FileType.ANIMATION, att.getFileId(), att.getFileName()); + return new FileInfo(FileType.ANIMATION, att.getFileId(), att.getFileName(), + att.getFileSize(), att.getDuration(), att.getWidth(), att.getHeight()); } else if (message.hasAudio()) { final var att = message.getAudio(); - return new FileInfo(FileType.AUDIO, att.getFileId(), att.getFileName()); + return new FileInfo(FileType.AUDIO, att.getFileId(), att.getFileName(), att.getFileSize(), att.getDuration()); } else if (message.hasVideo()) { final var att = message.getVideo(); - return new FileInfo(FileType.VIDEO, att.getFileId(), att.getFileName()); + return new FileInfo(FileType.VIDEO, att.getFileId(), att.getFileName(), + att.getFileSize(), att.getDuration(), att.getWidth(), att.getHeight()); } else if (message.hasVideoNote()) { final var att = message.getVideoNote(); - return new FileInfo(FileType.VIDEO_NOTE, att.getFileId(), null); + final Long fileSize = att.getFileSize() != null ? (Long.valueOf(att.getFileSize())) : null; + return new FileInfo(FileType.VIDEO_NOTE, att.getFileId(), null, + fileSize, att.getDuration(), att.getLength(), att.getLength()); } else if (message.hasVoice()) { final var att = message.getVoice(); - return new FileInfo(FileType.VOICE, att.getFileId(), null); + return new FileInfo(FileType.VOICE, att.getFileId(), null, att.getFileSize(), att.getDuration()); } else { return null; }