1
0
mirror of https://github.com/aNNiMON/ffmpegbot synced 2024-09-19 22:54:20 +03:00

Show more info about media

This commit is contained in:
aNNiMON 2023-01-21 17:59:19 +02:00
parent 1700cde152
commit c38dc4323b
5 changed files with 66 additions and 19 deletions

View File

@ -1,6 +1,7 @@
package com.annimon.ffmpegbot; package com.annimon.ffmpegbot;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.StringJoiner;
public class TextUtils { public class TextUtils {
@ -16,4 +17,17 @@ public class TextUtils {
return new DecimalFormat("#,##0.#") return new DecimalFormat("#,##0.#")
.format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; .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();
}
} }

View File

@ -50,9 +50,7 @@ public class MediaProcessingBundle implements CommandBundle<For> {
final var session = new MediaSession(); final var session = new MediaSession();
session.setChatId(message.getChatId()); session.setChatId(message.getChatId());
session.setFileId(fileInfo.fileId()); session.fromFileInfo(fileInfo);
session.setFileType(fileInfo.fileType());
session.setOriginalFilename(fileInfo.filename());
session.setParams(Resolver.resolveParameters(fileInfo.fileType())); session.setParams(Resolver.resolveParameters(fileInfo.fileType()));
final var result = Methods.sendMessage() final var result = Methods.sendMessage()

View File

@ -1,4 +1,8 @@
package com.annimon.ffmpegbot.session; 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);
}
} }

View File

@ -7,8 +7,7 @@ import java.io.File;
import java.util.List; import java.util.List;
import java.util.StringJoiner; import java.util.StringJoiner;
import static com.annimon.ffmpegbot.TextUtils.readableFileSize; import static com.annimon.ffmpegbot.TextUtils.*;
import static com.annimon.ffmpegbot.TextUtils.safeHtml;
public class MediaSession { public class MediaSession {
// Session key // Session key
@ -18,6 +17,9 @@ public class MediaSession {
private FileType fileType; private FileType fileType;
private String fileId; private String fileId;
private String originalFilename; private String originalFilename;
private Long fileSize;
private Integer duration;
private String dimensions;
// Parameters // Parameters
private List<Parameter<?>> params; private List<Parameter<?>> params;
private final InputParameters inputParams = new InputParameters(); private final InputParameters inputParams = new InputParameters();
@ -27,6 +29,15 @@ public class MediaSession {
// Status // Status
private String 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() { public long getChatId() {
return chatId; return chatId;
} }
@ -63,8 +74,20 @@ public class MediaSession {
this.originalFilename = originalFilename; this.originalFilename = originalFilename;
} }
public String getOriginalFilename() { public void setFileSize(Long fileSize) {
return originalFilename; 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<Parameter<?>> getParams() { public List<Parameter<?>> getParams() {
@ -99,25 +122,29 @@ public class MediaSession {
this.outputFile = outputFile; this.outputFile = outputFile;
} }
public String getStatus() {
return status;
}
public void setStatus(String status) { public void setStatus(String status) {
this.status = status; this.status = status;
} }
public StringJoiner describe() { public StringJoiner describe() {
final var joiner = new StringJoiner("\n"); final var joiner = new StringJoiner("\n");
joiner.add("File ID: <code>%s</code>".formatted(safeHtml(fileId)));
joiner.add("Type: <code>%s</code>".formatted(fileType)); joiner.add("Type: <code>%s</code>".formatted(fileType));
if (fileSize != null && fileSize > 0) {
joiner.add("File size: <code>%s</code>".formatted(readableFileSize(fileSize)));
}
if (duration != null && duration > 0) {
joiner.add("Duration: <code>%s</code>".formatted(readableDuration(duration)));
}
if (dimensions != null) {
joiner.add("Dimensions: <code>%s</code>".formatted(dimensions));
}
joiner.merge(inputParams.describe()); joiner.merge(inputParams.describe());
if (originalFilename != null) { if (originalFilename != null) {
joiner.add("Filename: <code>%s</code>".formatted(safeHtml(originalFilename))); joiner.add("Filename: <code>%s</code>".formatted(safeHtml(originalFilename)));
} }
if (inputFile != null && inputFile.canRead()) { if (inputFile != null && inputFile.canRead()) {
joiner.add("Input file: <code>%s</code>".formatted(safeHtml(inputFile.getName()))); joiner.add("Input file: <code>%s</code>".formatted(safeHtml(inputFile.getName())));
joiner.add("Size: <code>%s</code>".formatted(readableFileSize(inputFile.length()))); joiner.add("Input Size: <code>%s</code>".formatted(readableFileSize(inputFile.length())));
} }
if (outputFile != null && outputFile.canRead()) { if (outputFile != null && outputFile.canRead()) {
joiner.add("Output size: <code>%s</code>".formatted(readableFileSize(outputFile.length()))); joiner.add("Output size: <code>%s</code>".formatted(readableFileSize(outputFile.length())));

View File

@ -14,19 +14,23 @@ public class Resolver {
public static FileInfo resolveFileInfo(@NotNull Message message) { public static FileInfo resolveFileInfo(@NotNull Message message) {
if (message.hasAnimation()) { if (message.hasAnimation()) {
final var att = message.getAnimation(); 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()) { } else if (message.hasAudio()) {
final var att = message.getAudio(); 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()) { } else if (message.hasVideo()) {
final var att = message.getVideo(); 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()) { } else if (message.hasVideoNote()) {
final var att = message.getVideoNote(); 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()) { } else if (message.hasVoice()) {
final var att = message.getVoice(); 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 { } else {
return null; return null;
} }