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