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.io.File;
010import java.nio.file.Path;
011import java.util.ArrayList;
012import java.util.List;
013import javax.annotation.CheckReturnValue;
014
015/** Builds a ffprobe command line. */
016public class FFprobeBuilder {
017  private boolean showFormat = true;
018  private boolean showStreams = true;
019  private boolean showChapters = true;
020  private boolean showFrames = false;
021  private boolean showPackets = false;
022  private String userAgent;
023  private String input;
024
025  private final List<String> extraArgs = new ArrayList<>();
026
027  /** Sets whether to show format information. */
028  public FFprobeBuilder setShowFormat(boolean showFormat) {
029    this.showFormat = showFormat;
030    return this;
031  }
032
033  /** Sets whether to show stream information. */
034  public FFprobeBuilder setShowStreams(boolean showStreams) {
035    this.showStreams = showStreams;
036    return this;
037  }
038
039  /** Sets whether to show chapter information. */
040  public FFprobeBuilder setShowChapters(boolean showChapters) {
041    this.showChapters = showChapters;
042    return this;
043  }
044
045  /** Sets whether to show frame information. */
046  public FFprobeBuilder setShowFrames(boolean showFrames) {
047    this.showFrames = showFrames;
048    return this;
049  }
050
051  /** Sets whether to show packet information. */
052  public FFprobeBuilder setShowPackets(boolean showPackets) {
053    this.showPackets = showPackets;
054    return this;
055  }
056
057  /** Sets the HTTP user agent string. */
058  public FFprobeBuilder setUserAgent(String userAgent) {
059    this.userAgent = userAgent;
060    return this;
061  }
062
063  /** Sets the input file or URL to probe. */
064  public FFprobeBuilder setInput(String filename) {
065    checkNotNull(filename);
066    this.input = filename;
067    return this;
068  }
069
070  /** Sets the input file to probe. */
071  public FFprobeBuilder setInput(File file) {
072    return setInput(checkNotNull(file).getPath());
073  }
074
075  /** Sets the input path to probe. */
076  public FFprobeBuilder setInput(Path path) {
077    return setInput(checkNotNull(path).toString());
078  }
079
080  /** Adds extra command-line arguments to the ffprobe command. */
081  public FFprobeBuilder addExtraArgs(String... values) {
082    checkArgument(values != null, "extraArgs can not be null");
083    checkArgument(values.length > 0, "one or more values must be supplied");
084    checkNotEmpty(values[0], "first extra arg may not be empty");
085
086    for (String value : values) {
087      extraArgs.add(checkNotNull(value));
088    }
089    return this;
090  }
091
092  /** Builds the FFprobe command-line arguments. */
093  @CheckReturnValue
094  public List<String> build() {
095    ImmutableList.Builder<String> args = new ImmutableList.Builder<>();
096
097    Preconditions.checkNotNull(input, "Input must be specified");
098
099    args.add("-v", "quiet").add("-print_format", "json").add("-show_error");
100
101    if (userAgent != null) {
102      args.add("-user_agent", userAgent);
103    }
104
105    args.addAll(extraArgs);
106
107    if (showFormat) {
108      args.add("-show_format");
109    }
110    if (showStreams) {
111      args.add("-show_streams");
112    }
113    if (showChapters) {
114      args.add("-show_chapters");
115    }
116    if (showPackets) {
117      args.add("-show_packets");
118    }
119    if (showFrames) {
120      args.add("-show_frames");
121    }
122
123    args.add(input);
124
125    return args.build();
126  }
127}