001package net.bramp.ffmpeg.builder;
002
003// metadata_spec can be:
004// g global (If metadata specifier is omitted, it defaults to global.)
005// s[:stream_spec]
006// c:chapter_index
007// p:program_index
008// index is meant to be zero based, by negitive is allowed as dummy values
009
010import static com.google.common.base.Preconditions.checkArgument;
011import static com.google.common.base.Preconditions.checkNotNull;
012
013import com.google.errorprone.annotations.Immutable;
014
015/**
016 * Metadata spec, as described in the "map_metadata" section of
017 * https://www.ffmpeg.org/ffmpeg-all.html#Main-options
018 */
019@Immutable
020public class MetadataSpecifier {
021
022  final String spec;
023
024  private MetadataSpecifier(String spec) {
025    this.spec = checkNotNull(spec);
026  }
027
028  private MetadataSpecifier(String prefix, int index) {
029    this.spec = checkNotNull(prefix) + ":" + index;
030  }
031
032  private MetadataSpecifier(String prefix, StreamSpecifier spec) {
033    this.spec = checkNotNull(prefix) + ":" + checkNotNull(spec).spec();
034  }
035
036  public String spec() {
037    return spec;
038  }
039
040  public static String checkValidKey(String key) {
041    checkNotNull(key);
042    checkArgument(!key.isEmpty(), "key must not be empty");
043    checkArgument(key.matches("\\w+"), "key must only contain letters, numbers or _");
044    return key;
045  }
046
047  public static MetadataSpecifier global() {
048    return new MetadataSpecifier("g");
049  }
050
051  public static MetadataSpecifier chapter(int index) {
052    return new MetadataSpecifier("c", index);
053  }
054
055  public static MetadataSpecifier program(int index) {
056    return new MetadataSpecifier("p", index);
057  }
058
059  public static MetadataSpecifier stream(int index) {
060    return new MetadataSpecifier("s", StreamSpecifier.stream(index));
061  }
062
063  public static MetadataSpecifier stream(StreamSpecifierType type) {
064    return new MetadataSpecifier("s", StreamSpecifier.stream(type));
065  }
066
067  public static MetadataSpecifier stream(StreamSpecifierType stream_type, int stream_index) {
068    return new MetadataSpecifier("s", StreamSpecifier.stream(stream_type, stream_index));
069  }
070
071  public static MetadataSpecifier stream(StreamSpecifier spec) {
072    checkNotNull(spec);
073    return new MetadataSpecifier("s", spec);
074  }
075}