001package net.bramp.ffmpeg.builder; 002 003import static com.google.common.base.Preconditions.checkArgument; 004import static com.google.common.base.Preconditions.checkNotNull; 005 006import java.util.ArrayList; 007import java.util.List; 008 009/** 010 * Represents an HLS variant stream mapping. 011 * 012 * <p>Used with {@code -var_stream_map} to group audio, video, and subtitle streams. 013 */ 014public class HlsVariant { 015 016 private final List<String> streams = new ArrayList<>(); 017 private String name; 018 019 /** Constructs an empty HLS variant. */ 020 public HlsVariant() {} 021 022 /** 023 * Adds a video stream to this variant. 024 * 025 * @param index The zero-based index of the video stream in the output. 026 * @return this 027 */ 028 public HlsVariant addVideo(int index) { 029 checkArgument(index >= 0, "index must be >= 0"); 030 streams.add("v:" + index); 031 return this; 032 } 033 034 /** 035 * Adds an audio stream to this variant. 036 * 037 * @param index The zero-based index of the audio stream in the output. 038 * @return this 039 */ 040 public HlsVariant addAudio(int index) { 041 checkArgument(index >= 0, "index must be >= 0"); 042 streams.add("a:" + index); 043 return this; 044 } 045 046 /** 047 * Adds a subtitle stream to this variant. 048 * 049 * @param index The zero-based index of the subtitle stream in the output. 050 * @return this 051 */ 052 public HlsVariant addSubtitle(int index) { 053 checkArgument(index >= 0, "index must be >= 0"); 054 streams.add("s:" + index); 055 return this; 056 } 057 058 /** 059 * Sets the name for this variant. 060 * 061 * @param name The variant name (e.g., "1080p"). 062 * @return this 063 */ 064 public HlsVariant setName(String name) { 065 this.name = checkNotNull(name); 066 return this; 067 } 068 069 @Override 070 public String toString() { 071 if (streams.isEmpty()) { 072 return ""; 073 } 074 StringBuilder sb = new StringBuilder(); 075 for (int i = 0; i < streams.size(); i++) { 076 if (i > 0) { 077 sb.append(","); 078 } 079 sb.append(streams.get(i)); 080 } 081 if (name != null && !name.isEmpty()) { 082 sb.append(",name:").append(name); 083 } 084 return sb.toString(); 085 } 086}