From 81f0afe567c86567c3ce3b3b6c419163ce884330 Mon Sep 17 00:00:00 2001 From: aNNiMON Date: Thu, 19 Oct 2023 18:05:47 +0300 Subject: [PATCH] Allow converting video to video note --- .../resolvers/OutputFormatResolver.java | 23 ++++++++++++++++++- .../annimon/ffmpegbot/session/FileInfo.java | 4 ++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/annimon/ffmpegbot/parameters/resolvers/OutputFormatResolver.java b/src/main/java/com/annimon/ffmpegbot/parameters/resolvers/OutputFormatResolver.java index c46fe2d..b148614 100644 --- a/src/main/java/com/annimon/ffmpegbot/parameters/resolvers/OutputFormatResolver.java +++ b/src/main/java/com/annimon/ffmpegbot/parameters/resolvers/OutputFormatResolver.java @@ -4,6 +4,8 @@ import com.annimon.ffmpegbot.parameters.OutputFormat; import com.annimon.ffmpegbot.parameters.Parameter; import com.annimon.ffmpegbot.session.FileInfo; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.List; import static com.annimon.ffmpegbot.parameters.OutputFormat.*; @@ -12,12 +14,31 @@ public class OutputFormatResolver implements ParametersResolver { @Override public void resolve(@NotNull List> parameters, @NotNull FileInfo fileInfo) { final var outputFormat = switch (fileInfo.fileType()) { - case VIDEO -> new OutputFormat(List.of(VIDEO, AUDIO), VIDEO); + case VIDEO -> forVideo(fileInfo); case VIDEO_NOTE -> new OutputFormat(List.of(VIDEO_NOTE, VIDEO, AUDIO), VIDEO_NOTE); + case ANIMATION -> forAnimation(fileInfo); default -> null; }; if (outputFormat != null) { parameters.add(outputFormat); } } + + @NotNull + private static OutputFormat forVideo(@NotNull FileInfo fileInfo) { + final var types = new ArrayList(3); + types.add(VIDEO); + if (fileInfo.getDuration() < 60 && fileInfo.getExtension().equals("mp4")) { + types.add(VIDEO_NOTE); + } + types.add(AUDIO); + return new OutputFormat(types, VIDEO); + } + + @Nullable + private static OutputFormat forAnimation(@NotNull FileInfo fileInfo) { + if (fileInfo.getExtension().equals("mp4")) return null; + // webm sticker: + return new OutputFormat(List.of(VIDEO, VIDEO_NOTE), VIDEO); + } } diff --git a/src/main/java/com/annimon/ffmpegbot/session/FileInfo.java b/src/main/java/com/annimon/ffmpegbot/session/FileInfo.java index d520f3b..adec021 100644 --- a/src/main/java/com/annimon/ffmpegbot/session/FileInfo.java +++ b/src/main/java/com/annimon/ffmpegbot/session/FileInfo.java @@ -9,6 +9,10 @@ public record FileInfo(FileType fileType, String fileId, String filename, this(fileType, fileId, filename, fileSize, duration, null, null); } + public int getDuration() { + return duration != null ? duration : 0; + } + public String getExtension() { return FilenameUtils.getExtension(filename).toLowerCase(Locale.ROOT); }