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 <a
017 * href="https://www.ffmpeg.org/ffmpeg-all.html#Main-options">Main options</a>.
018 */
019@Immutable
020public class MetadataSpecifier {
021
022  private 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  /** Returns the metadata specifier string. */
037  public String spec() {
038    return spec;
039  }
040
041  /** Validates and returns the given metadata key. */
042  public static String checkValidKey(String key) {
043    checkNotNull(key);
044    checkArgument(!key.isEmpty(), "key must not be empty");
045    checkArgument(key.matches("\\w+"), "key must only contain letters, numbers or _");
046    return key;
047  }
048
049  /** Creates a global metadata specifier. */
050  public static MetadataSpecifier global() {
051    return new MetadataSpecifier("g");
052  }
053
054  /** Creates a chapter metadata specifier for the given index. */
055  public static MetadataSpecifier chapter(int index) {
056    return new MetadataSpecifier("c", index);
057  }
058
059  /** Creates a program metadata specifier for the given index. */
060  public static MetadataSpecifier program(int index) {
061    return new MetadataSpecifier("p", index);
062  }
063
064  /** Creates a stream metadata specifier for the given stream index. */
065  public static MetadataSpecifier stream(int index) {
066    return new MetadataSpecifier("s", StreamSpecifier.stream(index));
067  }
068
069  /** Creates a stream metadata specifier for the given stream type. */
070  public static MetadataSpecifier stream(StreamSpecifierType type) {
071    return new MetadataSpecifier("s", StreamSpecifier.stream(type));
072  }
073
074  /** Creates a stream metadata specifier for the given type and index. */
075  public static MetadataSpecifier stream(StreamSpecifierType stream_type, int stream_index) {
076    return new MetadataSpecifier("s", StreamSpecifier.stream(stream_type, stream_index));
077  }
078
079  /** Creates a stream metadata specifier from an existing stream specifier. */
080  public static MetadataSpecifier stream(StreamSpecifier spec) {
081    checkNotNull(spec);
082    return new MetadataSpecifier("s", spec);
083  }
084}