Skip to content

Commit

Permalink
Dynamically updating parameters list based on a user's input
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Jul 3, 2024
1 parent a046585 commit bc4e3af
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private void parameter(final CallbackQueryContext ctx, final MediaSession sessio
if (param != null && ctx.argumentsLength() == 2) {
final int index = Integer.parseInt(ctx.argument(1));
param.select(index);
parametersResolver.refine(session.getParams());
session.setSelectedParam(null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ protected BooleanParameter(String id, String name, Boolean value) {
super(id, name, List.of(false, true), value);
}

public boolean getValueAsPrimitive() {
return (value != null && value);
}

@Override
public String describeValue(Boolean value) {
if (value != null && value) {
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/com/annimon/ffmpegbot/parameters/OutputFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.annimon.ffmpegbot.commands.ffmpeg.Visitor;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class OutputFormat extends StringParameter {
public static final String ID = "output";
Expand All @@ -12,7 +14,29 @@ public class OutputFormat extends StringParameter {
public static final String VIDEO_NOTE = "VIDEO NOTE";

public OutputFormat(List<String> values, String initialValue) {
super(ID, "➡\uFE0F Output", values, initialValue);
super(ID, "➡️ Output", values, initialValue);
}

public OutputFormat disableFormat(String format) {
if (possibleValues.size() <= 1) return this;
final var values = possibleValues.stream()
.filter(f -> !Objects.equals(f, format))
.map(Objects::toString)
.toList();
if (possibleValues.size() == values.size()) {
return this;
}
return new OutputFormat(values, values.get(0));
}

public OutputFormat enableFormat(String format) {
boolean contains = possibleValues.stream()
.anyMatch(f -> Objects.equals(f, format));
if (contains) return this;

final var values = new ArrayList<String>(possibleValues);
values.add(format);
return new OutputFormat(values, values.get(0));
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/annimon/ffmpegbot/parameters/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public abstract class Parameter<T> {
protected final String displayName;
protected final List<? extends T> possibleValues;
protected T value;
private boolean enabled;

protected Parameter(String id, String displayName, List<? extends T> values, T value) {
this.id = id;
this.displayName = displayName;
this.possibleValues = values;
this.value = value;
this.enabled = true;
checkArgument(!values.isEmpty(), "possible values cannot be empty");
checkArgument(values.contains(value), "possible values must contain a value");
}
Expand All @@ -34,6 +36,17 @@ public int getPossibleValuesSize() {
return possibleValues.size();
}

public boolean isEnabled() {
return enabled;
}

public void enable() {
enabled = true;
}

public void disable() {
enabled = false;
}

public abstract <I> void accept(Visitor<I> visitor, I input);

Expand Down
48 changes: 36 additions & 12 deletions src/main/java/com/annimon/ffmpegbot/parameters/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,68 @@

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

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

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

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

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

public Parameter<?> findById(String id) {
return enabledParameters.get(id);
final var parameter = parameters.get(id);
if (parameter != null && parameter.isEnabled()) {
return parameter;
}
return null;
}

public <P extends Parameter<?>> Optional<P> findById(String id, Class<P> clazz) {
return Optional.ofNullable(findById(id))
.filter(clazz::isInstance)
.map(clazz::cast);
}

public void disable(String id) {
Optional.ofNullable(findById(id))
.ifPresent(p -> disabledParameters.put(p.id, p));
Optional.ofNullable(parameters.get(id))
.ifPresent(Parameter::disable);
}

public void disableAll(Collection<String> ids) {
ids.forEach(this::disable);
}

public void enable(String id) {
Optional.ofNullable(disabledParameters.get(id))
.ifPresent(this::add);
Optional.ofNullable(parameters.get(id))
.ifPresent(Parameter::enable);
}

public void enableAll(Collection<String> ids) {
ids.forEach(this::enable);
}

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

@Override
public int size() {
return enabledParameters.size();
return (int) enabledParameters().count();
}

private Stream<Parameter<?>> enabledParameters() {
return parameters.values()
.stream()
.filter(Parameter::isEnabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.annimon.ffmpegbot.session.FileType;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
import java.util.Set;

public class AudioResolver implements ParametersResolver {

Expand All @@ -28,6 +30,28 @@ public void resolve(@NotNull Parameters parameters, @NotNull FileInfo fileInfo)
}
}

@Override
public void refine(@NotNull Parameters parameters) {
parameters.findById(DisableAudio.ID, DisableAudio.class)
.ifPresent(p -> {
final Set<String> parameterIds = Set.of(
AudioBitrate.ID,
AudioCrystalizer.ID,
AudioEffect.ID,
AudioPitch.ID,
AudioVolume.ID
);
Optional<OutputFormat> outputFormat = parameters.findById(OutputFormat.ID, OutputFormat.class);
if (p.getValueAsPrimitive()) {
parameters.disableAll(parameterIds);
outputFormat.ifPresent(par -> parameters.add(par.disableFormat(OutputFormat.AUDIO)));
} else {
parameters.enableAll(parameterIds);
outputFormat.ifPresent(par -> parameters.add(par.enableFormat(OutputFormat.AUDIO)));
}
});
}

private void disableAudioParam(@NotNull Parameters parameters, @NotNull FileType fileType) {
final boolean canAudioBeDisabled = switch (fileType) {
case ANIMATION, AUDIO, VOICE -> false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ public void resolve(@NotNull Parameters parameters, @NotNull FileInfo fileInfo)
resolver.resolve(parameters, fileInfo);
}
}

@Override
public void refine(@NotNull Parameters parameters) {
for (ParametersResolver resolver : resolvers) {
resolver.refine(parameters);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.annimon.ffmpegbot.parameters.resolvers;

import com.annimon.ffmpegbot.parameters.AudioStreamByLanguage;
import com.annimon.ffmpegbot.parameters.Parameters;
import com.annimon.ffmpegbot.parameters.*;
import com.annimon.ffmpegbot.session.FileInfo;
import com.annimon.ffmpegbot.session.FileType;
import org.jetbrains.annotations.NotNull;
Expand All @@ -19,4 +18,16 @@ public void resolve(@NotNull Parameters parameters, @NotNull FileInfo fileInfo)
parameters.add(new AudioStreamByLanguage());
}
}

@Override
public void refine(@NotNull Parameters parameters) {
parameters.findById(DisableAudio.ID, DisableAudio.class)
.ifPresent(p -> {
if (p.getValueAsPrimitive()) {
parameters.disable(AudioStreamByLanguage.ID);
} else {
parameters.enable(AudioStreamByLanguage.ID);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,15 @@
public interface ParametersResolver {

void resolve(@NotNull Parameters parameters, @NotNull FileInfo fileInfo);

/**
* Refines all parameters based on user input.
* <p>
* Example: if Disable audio is ON -> removes all audio parameters.
*
* @param parameters all parameters
*/
default void refine(@NotNull Parameters parameters) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.annimon.ffmpegbot.session.FileInfo;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Objects;
import java.util.Set;

public class VideoResolver implements ParametersResolver {

Expand All @@ -25,4 +27,25 @@ public void resolve(@NotNull Parameters parameters, @NotNull FileInfo fileInfo)
));
}
}

@Override
public void refine(@NotNull Parameters parameters) {
parameters.findById(OutputFormat.ID, OutputFormat.class)
.map(Parameter::getValue)
.ifPresent(format -> {
final Set<String> parameterIds = Set.of(
Contrast.ID,
Gamma.ID,
Saturation.ID,
VideoBitrate.ID,
VideoScale.ID,
VideoFrameRate.ID
);
if (Objects.equals(format, OutputFormat.AUDIO)) {
parameters.disableAll(parameterIds);
} else {
parameters.enableAll(parameterIds);
}
});
}
}

0 comments on commit bc4e3af

Please sign in to comment.