Skip to content

Commit

Permalink
Store parameters as map
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Jul 2, 2024
1 parent 4389c3c commit 1ba8208
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.annimon.ffmpegbot.file.FileDownloadException;
import com.annimon.ffmpegbot.file.FileDownloader;
import com.annimon.ffmpegbot.parameters.Parameter;
import com.annimon.ffmpegbot.file.FilePath;
import com.annimon.ffmpegbot.parameters.Parameters;
import com.annimon.ffmpegbot.parameters.resolvers.GlobalParametersResolver;
import com.annimon.ffmpegbot.parameters.resolvers.ParametersResolver;
import com.annimon.ffmpegbot.session.MediaSession;
Expand All @@ -19,7 +19,6 @@
import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.api.objects.Message;

import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
Expand Down Expand Up @@ -54,7 +53,7 @@ public void handleMessage(final @NotNull CommonAbsSender sender, final @NotNull
session.setChatId(message.getChatId());
session.fromFileInfo(fileInfo);

final var params = new ArrayList<Parameter<?>>();
final var params = new Parameters();
parametersResolver.resolve(params, fileInfo);
session.setParams(params);

Expand All @@ -78,10 +77,7 @@ private void parameter(final CallbackQueryContext ctx, final MediaSession sessio
session.setSelectedParam(null);
} else {
final String id = ctx.argument(0);
final var param = session.getParams().stream()
.filter(p -> p.getId().equals(id))
.findFirst().orElse(null);

final var param = session.getParams().findById(id);
session.setSelectedParam(param);

if (param != null && ctx.argumentsLength() == 2) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.annimon.ffmpegbot.commands.ffmpeg;

import com.annimon.ffmpegbot.parameters.Parameter;
import com.annimon.ffmpegbot.parameters.Parameters;
import com.annimon.ffmpegbot.session.MediaSession;
import com.annimon.ffmpegbot.session.YtDlpSession;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
Expand All @@ -26,7 +27,7 @@ public static InlineKeyboardMarkup createKeyboard(MediaSession session) {
}
}

private static InlineKeyboardMarkup createParamsListKeyboard(List<Parameter<?>> params) {
private static InlineKeyboardMarkup createParamsListKeyboard(Parameters params) {
final var keyboard = new ArrayList<List<InlineKeyboardButton>>();
final var it = params.iterator();
while (it.hasNext()) {
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/annimon/ffmpegbot/parameters/Parameters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.annimon.ffmpegbot.parameters;

import org.jetbrains.annotations.NotNull;
import java.util.*;

public class Parameters extends AbstractCollection<Parameter<?>> {

private final Map<String, Parameter<?>> parameters;

public Parameters() {
parameters = new LinkedHashMap<>();
}

@Override
public boolean add(Parameter<?> parameter) {
return !Objects.equals(parameter, parameters.put(parameter.id, parameter));
}

public Parameter<?> findById(String id) {
return parameters.get(id);
}

@NotNull
@Override
public Iterator<Parameter<?>> iterator() {
return parameters.values().iterator();
}

@Override
public int size() {
return parameters.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class AudioResolver implements ParametersResolver {

@Override
public void resolve(@NotNull List<Parameter<?>> parameters, @NotNull FileInfo fileInfo) {
public void resolve(@NotNull Parameters parameters, @NotNull FileInfo fileInfo) {
final boolean hasAudio = switch (fileInfo.fileType()) {
case ANIMATION -> false;
case AUDIO, VOICE -> true;
Expand All @@ -28,7 +28,7 @@ public void resolve(@NotNull List<Parameter<?>> parameters, @NotNull FileInfo fi
}
}

private void disableAudioParam(@NotNull List<Parameter<?>> parameters, @NotNull FileType fileType) {
private void disableAudioParam(@NotNull Parameters parameters, @NotNull FileType fileType) {
final boolean canAudioBeDisabled = switch (fileType) {
case ANIMATION, AUDIO, VOICE -> false;
case VIDEO, VIDEO_NOTE -> true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.annimon.ffmpegbot.parameters.resolvers;

import com.annimon.ffmpegbot.parameters.Parameter;
import com.annimon.ffmpegbot.parameters.Parameters;
import com.annimon.ffmpegbot.session.FileInfo;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
Expand All @@ -19,7 +19,7 @@ public GlobalParametersResolver() {
}

@Override
public void resolve(@NotNull List<Parameter<?>> parameters, @NotNull FileInfo fileInfo) {
public void resolve(@NotNull Parameters parameters, @NotNull FileInfo fileInfo) {
for (ParametersResolver resolver : resolvers) {
resolver.resolve(parameters, fileInfo);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.annimon.ffmpegbot.parameters.resolvers;

import com.annimon.ffmpegbot.parameters.AudioStreamByLanguage;
import com.annimon.ffmpegbot.parameters.Parameter;
import com.annimon.ffmpegbot.parameters.Parameters;
import com.annimon.ffmpegbot.session.FileInfo;
import com.annimon.ffmpegbot.session.FileType;
import org.jetbrains.annotations.NotNull;
import java.util.List;

public class MultiAudioStreamsResolver implements ParametersResolver {

@Override
public void resolve(@NotNull List<Parameter<?>> parameters, @NotNull FileInfo fileInfo) {
public void resolve(@NotNull Parameters parameters, @NotNull FileInfo fileInfo) {
// TODO: ffprobe check for actual codec
if (!fileInfo.fileType().equals(FileType.VIDEO)) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.annimon.ffmpegbot.parameters.resolvers;

import com.annimon.ffmpegbot.parameters.OutputFormat;
import com.annimon.ffmpegbot.parameters.Parameter;
import com.annimon.ffmpegbot.parameters.Parameters;
import com.annimon.ffmpegbot.session.FileInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -12,7 +12,7 @@
public class OutputFormatResolver implements ParametersResolver {

@Override
public void resolve(@NotNull List<Parameter<?>> parameters, @NotNull FileInfo fileInfo) {
public void resolve(@NotNull Parameters parameters, @NotNull FileInfo fileInfo) {
final var outputFormat = switch (fileInfo.fileType()) {
case VIDEO -> forVideo(fileInfo);
case VIDEO_NOTE -> new OutputFormat(List.of(VIDEO_NOTE, VIDEO, AUDIO), VIDEO_NOTE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.annimon.ffmpegbot.parameters.resolvers;

import com.annimon.ffmpegbot.parameters.Parameter;
import com.annimon.ffmpegbot.parameters.Parameters;
import com.annimon.ffmpegbot.session.FileInfo;
import org.jetbrains.annotations.NotNull;
import java.util.List;

public interface ParametersResolver {

void resolve(@NotNull List<Parameter<?>> parameters, @NotNull FileInfo fileInfo);
void resolve(@NotNull Parameters parameters, @NotNull FileInfo fileInfo);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.annimon.ffmpegbot.parameters.resolvers;

import com.annimon.ffmpegbot.parameters.Parameter;
import com.annimon.ffmpegbot.parameters.Parameters;
import com.annimon.ffmpegbot.parameters.SpeedFactor;
import com.annimon.ffmpegbot.session.FileInfo;
import org.jetbrains.annotations.NotNull;
import java.util.List;

public class SpeedFactorResolver implements ParametersResolver {

@Override
public void resolve(@NotNull List<Parameter<?>> parameters, @NotNull FileInfo fileInfo) {
public void resolve(@NotNull Parameters parameters, @NotNull FileInfo fileInfo) {
// For anything except static images
parameters.add(new SpeedFactor());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class VideoResolver implements ParametersResolver {

@Override
public void resolve(@NotNull List<Parameter<?>> parameters, @NotNull FileInfo fileInfo) {
public void resolve(@NotNull Parameters parameters, @NotNull FileInfo fileInfo) {
final boolean hasVideo = switch (fileInfo.fileType()) {
case ANIMATION, VIDEO, VIDEO_NOTE -> true;
default -> false;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/annimon/ffmpegbot/session/MediaSession.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.annimon.ffmpegbot.session;

import com.annimon.ffmpegbot.parameters.Parameter;
import com.annimon.ffmpegbot.parameters.Parameters;

import java.io.File;
import java.util.List;
import java.util.StringJoiner;

import static com.annimon.ffmpegbot.TextUtils.*;
Expand All @@ -18,7 +18,7 @@ public final class MediaSession extends Session {
private String resolution;
// Parameters
private Parameter<?> selectedParam;
private List<Parameter<?>> params;
private Parameters params;
// Files
private File inputFile;
private File outputFile;
Expand Down Expand Up @@ -82,11 +82,11 @@ public void setResolution(Integer width, Integer height) {
}
}

public List<Parameter<?>> getParams() {
public Parameters getParams() {
return params;
}

public void setParams(List<Parameter<?>> params) {
public void setParams(Parameters params) {
this.params = params;
}

Expand Down

0 comments on commit 1ba8208

Please sign in to comment.