001package net.bramp.ffmpeg.builder;
002
003import static com.google.common.base.Preconditions.checkNotNull;
004import static net.bramp.ffmpeg.builder.MetadataSpecifier.checkValidKey;
005
006/** <a href="https://ffmpeg.org/ffmpeg.html#Stream-specifiers">Stream specifier</a>. */
007public class StreamSpecifier {
008
009  private final String spec;
010
011  private StreamSpecifier(String spec) {
012    this.spec = spec;
013  }
014
015  /** Returns the stream specifier string. */
016  public String spec() {
017    return spec;
018  }
019
020  /**
021   * Matches the stream with this index.
022   *
023   * @param index The stream index
024   * @return A new StreamSpecifier
025   */
026  public static StreamSpecifier stream(int index) {
027    return new StreamSpecifier(String.valueOf(index));
028  }
029
030  /**
031   * Matches all streams of this type.
032   *
033   * @param type The stream type
034   * @return A new StreamSpecifier
035   */
036  public static StreamSpecifier stream(StreamSpecifierType type) {
037    checkNotNull(type);
038    return new StreamSpecifier(type.toString());
039  }
040
041  /**
042   * Matches the stream number stream_index of this type.
043   *
044   * @param type The stream type
045   * @param index The stream index
046   * @return A new StreamSpecifier
047   */
048  public static StreamSpecifier stream(StreamSpecifierType type, int index) {
049    checkNotNull(type);
050    return new StreamSpecifier(type.toString() + ":" + index);
051  }
052
053  /**
054   * Matches all streams in the program.
055   *
056   * @param program_id The program id
057   * @return A new StreamSpecifier
058   */
059  public static StreamSpecifier program(int program_id) {
060    return new StreamSpecifier("p:" + program_id);
061  }
062
063  /**
064   * Matches the stream with number stream_index in the program with the id program_id.
065   *
066   * @param program_id The program id
067   * @param stream_index The stream index
068   * @return A new StreamSpecifier
069   */
070  public static StreamSpecifier program(int program_id, int stream_index) {
071    return new StreamSpecifier("p:" + program_id + ":" + stream_index);
072  }
073
074  /**
075   * Match the stream by stream id (e.g. PID in MPEG-TS container).
076   *
077   * @param stream_id The stream id
078   * @return A new StreamSpecifier
079   */
080  public static StreamSpecifier id(int stream_id) {
081    return new StreamSpecifier("i:" + stream_id);
082  }
083
084  /**
085   * Matches all streams with the given metadata tag.
086   *
087   * @param key The metadata tag
088   * @return A new StreamSpecifier
089   */
090  public static StreamSpecifier tag(String key) {
091    return new StreamSpecifier("m:" + checkValidKey(key));
092  }
093
094  /**
095   * Matches streams with the metadata tag key having the specified value.
096   *
097   * @param key The metadata tag
098   * @param value The metatdata's value
099   * @return A new StreamSpecifier
100   */
101  public static StreamSpecifier tag(String key, String value) {
102    checkValidKey(key);
103    checkNotNull(value);
104    return new StreamSpecifier("m:" + key + ":" + value);
105  }
106
107  /**
108   * Matches streams with usable configuration, the codec must be defined and the essential
109   * information such as video dimension or audio sample rate must be present.
110   *
111   * @return A new StreamSpecifier
112   */
113  public static StreamSpecifier usable() {
114    return new StreamSpecifier("u");
115  }
116}