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

Allow converting video to video note

This commit is contained in:
aNNiMON 2023-10-19 18:05:47 +03:00
parent a28ca6ded8
commit 81f0afe567
2 changed files with 26 additions and 1 deletions

View File

@ -4,6 +4,8 @@ import com.annimon.ffmpegbot.parameters.OutputFormat;
import com.annimon.ffmpegbot.parameters.Parameter; import com.annimon.ffmpegbot.parameters.Parameter;
import com.annimon.ffmpegbot.session.FileInfo; import com.annimon.ffmpegbot.session.FileInfo;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import static com.annimon.ffmpegbot.parameters.OutputFormat.*; import static com.annimon.ffmpegbot.parameters.OutputFormat.*;
@ -12,12 +14,31 @@ public class OutputFormatResolver implements ParametersResolver {
@Override @Override
public void resolve(@NotNull List<Parameter<?>> parameters, @NotNull FileInfo fileInfo) { public void resolve(@NotNull List<Parameter<?>> parameters, @NotNull FileInfo fileInfo) {
final var outputFormat = switch (fileInfo.fileType()) { 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 VIDEO_NOTE -> new OutputFormat(List.of(VIDEO_NOTE, VIDEO, AUDIO), VIDEO_NOTE);
case ANIMATION -> forAnimation(fileInfo);
default -> null; default -> null;
}; };
if (outputFormat != null) { if (outputFormat != null) {
parameters.add(outputFormat); parameters.add(outputFormat);
} }
} }
@NotNull
private static OutputFormat forVideo(@NotNull FileInfo fileInfo) {
final var types = new ArrayList<String>(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);
}
} }

View File

@ -9,6 +9,10 @@ public record FileInfo(FileType fileType, String fileId, String filename,
this(fileType, fileId, filename, fileSize, duration, null, null); this(fileType, fileId, filename, fileSize, duration, null, null);
} }
public int getDuration() {
return duration != null ? duration : 0;
}
public String getExtension() { public String getExtension() {
return FilenameUtils.getExtension(filename).toLowerCase(Locale.ROOT); return FilenameUtils.getExtension(filename).toLowerCase(Locale.ROOT);
} }