Skip to content

Commit 38041fc

Browse files
committed
feat: Extract input and output specifier into their own method
This allows classes extending AbstractFFmpegStreamBuilder to overwrite only how input/output specifiers are generated without duplicating flags
1 parent 449b005 commit 38041fc

File tree

5 files changed

+143
-36
lines changed

5 files changed

+143
-36
lines changed

src/main/java/net/bramp/ffmpeg/builder/AbstractFFmpegInputBuilder.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,6 @@ public EncodingOptions buildOptions() {
4848
return null;
4949
}
5050

51-
@Override
52-
protected List<String> build(FFmpegBuilder parent, int pass) {
53-
ImmutableList.Builder<String> args = new ImmutableList.Builder<>();
54-
55-
addGlobalFlags(parent, args);
56-
57-
// TODO: Handle input options
58-
59-
args.addAll(buildInputString());
60-
61-
return args.build();
62-
}
63-
6451
@Override
6552
protected void addGlobalFlags(FFmpegBuilder parent, ImmutableList.Builder<String> args) {
6653
if (this.readAtNativeFrameRate) {
@@ -69,6 +56,4 @@ protected void addGlobalFlags(FFmpegBuilder parent, ImmutableList.Builder<String
6956

7057
super.addGlobalFlags(parent, args);
7158
}
72-
73-
protected abstract List<String> buildInputString();
7459
}

src/main/java/net/bramp/ffmpeg/builder/AbstractFFmpegOutputBuilder.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,24 @@ protected void addAudioFlags(ImmutableList.Builder<String> args) {
397397
}
398398
}
399399

400+
@Override
401+
protected void addSourceTarget(int pass, ImmutableList.Builder<String> args) {
402+
if (filename != null && uri != null) {
403+
throw new IllegalStateException("Only one of filename and uri can be set");
404+
}
405+
406+
// Output
407+
if (pass == 1) {
408+
args.add(DEVNULL);
409+
} else if (filename != null) {
410+
args.add(filename);
411+
} else if (uri != null) {
412+
args.add(uri.toString());
413+
} else {
414+
assert false;
415+
}
416+
}
417+
400418
@CheckReturnValue
401419
@Override
402420
protected T getThis() {

src/main/java/net/bramp/ffmpeg/builder/AbstractFFmpegStreamBuilder.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -584,24 +584,13 @@ protected List<String> build(FFmpegBuilder parent, int pass) {
584584

585585
args.addAll(extra_args);
586586

587-
if (filename != null && uri != null) {
588-
throw new IllegalStateException("Only one of filename and uri can be set");
589-
}
590-
591-
// Output
592-
if (pass == 1) {
593-
args.add(DEVNULL);
594-
} else if (filename != null) {
595-
args.add(filename);
596-
} else if (uri != null) {
597-
args.add(uri.toString());
598-
} else {
599-
assert false;
600-
}
587+
addSourceTarget(pass, args);
601588

602589
return args.build();
603590
}
604591

592+
protected abstract void addSourceTarget(int pass, ImmutableList.Builder<String> args);
593+
605594
protected void addGlobalFlags(FFmpegBuilder parent, ImmutableList.Builder<String> args) {
606595
if (strict != FFmpegBuilder.Strict.NORMAL) {
607596
args.add("-strict", strict.toString());

src/main/java/net/bramp/ffmpeg/builder/FFmpegFileInputBuilder.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package net.bramp.ffmpeg.builder;
22

3-
import net.bramp.ffmpeg.options.EncodingOptions;
3+
import com.google.common.collect.ImmutableList;
44
import net.bramp.ffmpeg.probe.FFmpegProbeResult;
55

6-
import java.util.Arrays;
7-
import java.util.Collections;
8-
import java.util.List;
9-
106
public class FFmpegFileInputBuilder extends AbstractFFmpegInputBuilder<FFmpegFileInputBuilder> {
117
private final String filename;
128

@@ -26,7 +22,18 @@ public FFmpegFileInputBuilder(FFmpegBuilder parent, String filename, FFmpegProbe
2622
}
2723

2824
@Override
29-
protected List<String> buildInputString() {
30-
return Arrays.asList("-i", filename);
25+
protected void addSourceTarget(int pass, ImmutableList.Builder<String> args) {
26+
if (filename != null && uri != null) {
27+
throw new IllegalStateException("Only one of filename and uri can be set");
28+
}
29+
30+
// Input
31+
if (filename != null) {
32+
args.add("-i", filename);
33+
} else if (uri != null) {
34+
args.add("-i", uri.toString());
35+
} else {
36+
assert false;
37+
}
3138
}
3239
}

src/test/java/net/bramp/ffmpeg/InputOutputTest.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,112 @@ public void readAtNativeFrameRateMultiple() {
130130

131131
assertThat(command, is(ImmutableList.of("-y", "-v", "error", "-re", "-i", "input.mp4", "-re", "-i", "input.mkv", "output.mp4")));
132132
}
133+
134+
@Test
135+
public void outputCodec() {
136+
List<String> command = new FFmpegBuilder()
137+
.addInput("input.mp4")
138+
.done()
139+
.addOutput("output.mp4")
140+
.setVideoCodec("libx264")
141+
.setAudioCodec("aac")
142+
.setSubtitleCodec("vtt")
143+
.done()
144+
.build();
145+
146+
assertThat(command, is(ImmutableList.of("-y", "-v", "error", "-i", "input.mp4", "-vcodec", "libx264", "-acodec", "aac", "-scodec", "vtt", "output.mp4")));
147+
}
148+
149+
@Test
150+
public void inputCodec() {
151+
List<String> command = new FFmpegBuilder()
152+
.addInput("input.mp4")
153+
.setVideoCodec("libx264")
154+
.setAudioCodec("aac")
155+
.setSubtitleCodec("vtt")
156+
.done()
157+
.addOutput("output.mp4")
158+
.done()
159+
.build();
160+
161+
assertThat(command, is(ImmutableList.of("-y", "-v", "error", "-vcodec", "libx264", "-acodec", "aac", "-scodec", "vtt", "-i", "input.mp4", "output.mp4")));
162+
}
163+
164+
@Test
165+
public void inputVideoDisabled() {
166+
List<String> command = new FFmpegBuilder()
167+
.addInput("input.mp4")
168+
.disableVideo()
169+
.done()
170+
.addOutput("output.mp4")
171+
.done()
172+
.build();
173+
174+
assertThat(command, is(ImmutableList.of("-y", "-v", "error", "-vn", "-i", "input.mp4", "output.mp4")));
175+
}
176+
177+
@Test
178+
public void outputVideoDisabled() {
179+
List<String> command = new FFmpegBuilder()
180+
.addInput("input.mp4")
181+
.done()
182+
.addOutput("output.mp4")
183+
.disableVideo()
184+
.done()
185+
.build();
186+
187+
assertThat(command, is(ImmutableList.of("-y", "-v", "error", "-i", "input.mp4", "-vn", "output.mp4")));
188+
}
189+
190+
@Test
191+
public void inputAudioDisabled() {
192+
List<String> command = new FFmpegBuilder()
193+
.addInput("input.mp4")
194+
.disableAudio()
195+
.done()
196+
.addOutput("output.mp4")
197+
.done()
198+
.build();
199+
200+
assertThat(command, is(ImmutableList.of("-y", "-v", "error", "-an", "-i", "input.mp4", "output.mp4")));
201+
}
202+
203+
@Test
204+
public void outputAudioDisabled() {
205+
List<String> command = new FFmpegBuilder()
206+
.addInput("input.mp4")
207+
.done()
208+
.addOutput("output.mp4")
209+
.disableAudio()
210+
.done()
211+
.build();
212+
213+
assertThat(command, is(ImmutableList.of("-y", "-v", "error", "-i", "input.mp4", "-an", "output.mp4")));
214+
}
215+
216+
@Test
217+
public void inputSubtitleDisabled() {
218+
List<String> command = new FFmpegBuilder()
219+
.addInput("input.mp4")
220+
.disableSubtitle()
221+
.done()
222+
.addOutput("output.mp4")
223+
.done()
224+
.build();
225+
226+
assertThat(command, is(ImmutableList.of("-y", "-v", "error", "-sn", "-i", "input.mp4", "output.mp4")));
227+
}
228+
229+
@Test
230+
public void outputSubtitleDisabled() {
231+
List<String> command = new FFmpegBuilder()
232+
.addInput("input.mp4")
233+
.done()
234+
.addOutput("output.mp4")
235+
.disableSubtitle()
236+
.done()
237+
.build();
238+
239+
assertThat(command, is(ImmutableList.of("-y", "-v", "error", "-i", "input.mp4", "-sn", "output.mp4")));
240+
}
133241
}

0 commit comments

Comments
 (0)