001package net.bramp.ffmpeg.builder;
002
003import static com.google.common.base.Preconditions.checkArgument;
004import static com.google.common.base.Preconditions.checkNotNull;
005import static net.bramp.ffmpeg.Preconditions.checkNotEmpty;
006
007import com.google.common.base.Preconditions;
008import com.google.common.collect.ImmutableList;
009import java.util.ArrayList;
010import java.util.List;
011import javax.annotation.CheckReturnValue;
012
013/** Builds a ffprobe command line */
014public class FFprobeBuilder {
015  private boolean showFormat = true;
016  private boolean showStreams = true;
017  private boolean showChapters = true;
018  private boolean showFrames = false;
019  private boolean showPackets = false;
020  private String userAgent;
021  private String input;
022
023  private final List<String> extraArgs = new ArrayList<>();
024
025  public FFprobeBuilder setShowFormat(boolean showFormat) {
026    this.showFormat = showFormat;
027    return this;
028  }
029
030  public FFprobeBuilder setShowStreams(boolean showStreams) {
031    this.showStreams = showStreams;
032    return this;
033  }
034
035  public FFprobeBuilder setShowChapters(boolean showChapters) {
036    this.showChapters = showChapters;
037    return this;
038  }
039
040  public FFprobeBuilder setShowFrames(boolean showFrames) {
041    this.showFrames = showFrames;
042    return this;
043  }
044
045  public FFprobeBuilder setShowPackets(boolean showPackets) {
046    this.showPackets = showPackets;
047    return this;
048  }
049
050  public FFprobeBuilder setUserAgent(String userAgent) {
051    this.userAgent = userAgent;
052    return this;
053  }
054
055  public FFprobeBuilder setInput(String filename) {
056    checkNotNull(filename);
057    this.input = filename;
058    return this;
059  }
060
061  public FFprobeBuilder addExtraArgs(String... values) {
062    checkArgument(values != null, "extraArgs can not be null");
063    checkArgument(values.length > 0, "one or more values must be supplied");
064    checkNotEmpty(values[0], "first extra arg may not be empty");
065
066    for (String value : values) {
067      extraArgs.add(checkNotNull(value));
068    }
069    return this;
070  }
071
072  @CheckReturnValue
073  public List<String> build() {
074    ImmutableList.Builder<String> args = new ImmutableList.Builder<>();
075
076    Preconditions.checkNotNull(input, "Input must be specified");
077
078    args.add("-v", "quiet").add("-print_format", "json").add("-show_error");
079
080    if (userAgent != null) {
081      args.add("-user_agent", userAgent);
082    }
083
084    args.addAll(extraArgs);
085
086    if (showFormat) args.add("-show_format");
087    if (showStreams) args.add("-show_streams");
088    if (showChapters) args.add("-show_chapters");
089    if (showPackets) args.add("-show_packets");
090    if (showFrames) args.add("-show_frames");
091
092    args.add(input);
093
094    return args.build();
095  }
096}