Skip to content

Commit eb1b901

Browse files
committed
refactor: add Path and File overloads to builder API and fix path concatenation bug
1 parent 15eebee commit eb1b901

5 files changed

Lines changed: 154 additions & 2 deletions

File tree

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import com.google.common.base.Preconditions;
1111
import com.google.common.base.Strings;
1212
import com.google.common.collect.ImmutableList;
13+
import java.io.File;
1314
import java.net.URI;
15+
import java.nio.file.Path;
1416
import java.util.ArrayList;
1517
import java.util.List;
1618
import java.util.concurrent.TimeUnit;
@@ -168,6 +170,30 @@ public T setPresetFilename(String presetFilename) {
168170
return getThis();
169171
}
170172

173+
/**
174+
* Sets a file to use containing presets.
175+
*
176+
* <p>Uses `-fpre`.
177+
*
178+
* @param presetFile the preset by file
179+
* @return this
180+
*/
181+
public T setPresetFilename(File presetFile) {
182+
return setPresetFilename(checkNotNull(presetFile).getPath());
183+
}
184+
185+
/**
186+
* Sets a file to use containing presets.
187+
*
188+
* <p>Uses `-fpre`.
189+
*
190+
* @param presetPath the preset by path
191+
* @return this
192+
*/
193+
public T setPresetFilename(Path presetPath) {
194+
return setPresetFilename(checkNotNull(presetPath).toString());
195+
}
196+
171197
/**
172198
* Sets a preset by name (this only works with some codecs).
173199
*
@@ -186,6 +212,26 @@ public T setFilename(String filename) {
186212
return getThis();
187213
}
188214

215+
/**
216+
* Sets the output filename.
217+
*
218+
* @param file The file
219+
* @return this
220+
*/
221+
public T setFilename(File file) {
222+
return setFilename(checkNotNull(file).getPath());
223+
}
224+
225+
/**
226+
* Sets the output filename.
227+
*
228+
* @param path The path
229+
* @return this
230+
*/
231+
public T setFilename(Path path) {
232+
return setFilename(checkNotNull(path).toString());
233+
}
234+
189235
public String getFilename() {
190236
return filename;
191237
}

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import com.google.common.base.Preconditions;
99
import com.google.common.base.Strings;
1010
import com.google.common.collect.ImmutableList;
11+
import java.io.File;
1112
import java.net.URI;
13+
import java.nio.file.Path;
14+
import java.nio.file.Paths;
1215
import java.util.ArrayList;
1316
import java.util.List;
1417
import java.util.Map;
@@ -104,6 +107,14 @@ public FFmpegBuilder setPassDirectory(String directory) {
104107
return this;
105108
}
106109

110+
public FFmpegBuilder setPassDirectory(File directory) {
111+
return setPassDirectory(checkNotNull(directory).getPath());
112+
}
113+
114+
public FFmpegBuilder setPassDirectory(Path directory) {
115+
return setPassDirectory(checkNotNull(directory).toString());
116+
}
117+
107118
public String getPassDirectory() {
108119
return this.pass_directory;
109120
}
@@ -153,6 +164,14 @@ public FFmpegFileInputBuilder addInput(String filename) {
153164
return this.doAddInput(new FFmpegFileInputBuilder(this, filename));
154165
}
155166

167+
public FFmpegFileInputBuilder addInput(File file) {
168+
return addInput(checkNotNull(file).getPath());
169+
}
170+
171+
public FFmpegFileInputBuilder addInput(Path path) {
172+
return addInput(checkNotNull(path).toString());
173+
}
174+
156175
public <T extends AbstractFFmpegInputBuilder<T>> FFmpegBuilder addInput(T input) {
157176
return this.doAddInput(input).done();
158177
}
@@ -179,6 +198,16 @@ public FFmpegFileInputBuilder setInput(String filename) {
179198
return addInput(filename);
180199
}
181200

201+
public FFmpegFileInputBuilder setInput(File file) {
202+
clearInputs();
203+
return addInput(file);
204+
}
205+
206+
public FFmpegFileInputBuilder setInput(Path path) {
207+
clearInputs();
208+
return addInput(path);
209+
}
210+
182211
public <T extends AbstractFFmpegInputBuilder<T>> FFmpegBuilder setInput(T input) {
183212
checkNotNull(input);
184213

@@ -306,6 +335,14 @@ public FFmpegOutputBuilder addOutput(String filename) {
306335
return output;
307336
}
308337

338+
public FFmpegOutputBuilder addOutput(File file) {
339+
return addOutput(checkNotNull(file).getPath());
340+
}
341+
342+
public FFmpegOutputBuilder addOutput(Path path) {
343+
return addOutput(checkNotNull(path).toString());
344+
}
345+
309346
/**
310347
* Adds new output file.
311348
*
@@ -336,6 +373,14 @@ public FFmpegHlsOutputBuilder addHlsOutput(String filename) {
336373
return output;
337374
}
338375

376+
public FFmpegHlsOutputBuilder addHlsOutput(File file) {
377+
return addHlsOutput(checkNotNull(file).getPath());
378+
}
379+
380+
public FFmpegHlsOutputBuilder addHlsOutput(Path path) {
381+
return addHlsOutput(checkNotNull(path).toString());
382+
}
383+
339384
/**
340385
* Adds an existing FFmpegOutputBuilder. This is similar to calling the other addOuput methods but
341386
* instead allows an existing FFmpegOutputBuilder to be used, and reused.
@@ -420,7 +465,7 @@ public List<String> build() {
420465
args.add("-pass", Integer.toString(pass));
421466

422467
if (pass_prefix != null) {
423-
args.add("-passlogfile", pass_directory + pass_prefix);
468+
args.add("-passlogfile", Paths.get(pass_directory, pass_prefix).toString());
424469
}
425470
}
426471

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import com.google.common.base.Preconditions;
88
import com.google.common.collect.ImmutableList;
9+
import java.io.File;
10+
import java.nio.file.Path;
911
import java.util.ArrayList;
1012
import java.util.List;
1113
import javax.annotation.CheckReturnValue;
@@ -58,6 +60,14 @@ public FFprobeBuilder setInput(String filename) {
5860
return this;
5961
}
6062

63+
public FFprobeBuilder setInput(File file) {
64+
return setInput(checkNotNull(file).getPath());
65+
}
66+
67+
public FFprobeBuilder setInput(Path path) {
68+
return setInput(checkNotNull(path).toString());
69+
}
70+
6171
public FFprobeBuilder addExtraArgs(String... values) {
6272
checkArgument(values != null, "extraArgs can not be null");
6373
checkArgument(values.length > 0, "one or more values must be supplied");

src/test/java/net/bramp/ffmpeg/builder/FFmpegBuilderTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import static org.junit.Assert.assertEquals;
1313

1414
import com.google.common.collect.ImmutableList;
15+
import java.io.File;
1516
import java.net.URI;
17+
import java.nio.file.Path;
1618
import java.util.List;
1719
import java.util.concurrent.TimeUnit;
1820
import net.bramp.ffmpeg.builder.FFmpegBuilder.Verbosity;
@@ -282,6 +284,51 @@ public void testURIAndFilenameOutput() {
282284
.build();
283285
}
284286

287+
@Test(expected = NullPointerException.class)
288+
public void testSetInputNullString() {
289+
new FFmpegBuilder().setInput((String) null);
290+
}
291+
292+
@Test(expected = NullPointerException.class)
293+
public void testSetInputNullFile() {
294+
new FFmpegBuilder().setInput((File) null);
295+
}
296+
297+
@Test(expected = NullPointerException.class)
298+
public void testSetInputNullPath() {
299+
new FFmpegBuilder().setInput((Path) null);
300+
}
301+
302+
@Test(expected = NullPointerException.class)
303+
public void testAddInputNullString() {
304+
new FFmpegBuilder().addInput((String) null);
305+
}
306+
307+
@Test(expected = NullPointerException.class)
308+
public void testAddInputNullFile() {
309+
new FFmpegBuilder().addInput((File) null);
310+
}
311+
312+
@Test(expected = NullPointerException.class)
313+
public void testAddInputNullPath() {
314+
new FFmpegBuilder().addInput((Path) null);
315+
}
316+
317+
@Test(expected = IllegalArgumentException.class)
318+
public void testAddOutputNullString() {
319+
new FFmpegBuilder().addOutput((String) null);
320+
}
321+
322+
@Test(expected = NullPointerException.class)
323+
public void testAddOutputNullFile() {
324+
new FFmpegBuilder().addOutput((File) null);
325+
}
326+
327+
@Test(expected = NullPointerException.class)
328+
public void testAddOutputNullPath() {
329+
new FFmpegBuilder().addOutput((Path) null);
330+
}
331+
285332
@Test(expected = IllegalArgumentException.class)
286333
public void testAddEmptyFilename() {
287334
List<String> unused = new FFmpegBuilder().setInput("input").done().addOutput("").done().build();

src/test/java/net/bramp/ffmpeg/builder/FFprobeBuilderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import static org.junit.Assert.assertThrows;
55

66
import com.google.common.collect.ImmutableList;
7+
import java.io.File;
8+
import java.nio.file.Path;
79
import java.util.List;
810
import org.junit.Test;
911

@@ -88,7 +90,9 @@ public void testSpecifyUserAgent() {
8890
@Test
8991
public void throwsExceptionIfInputIsNull() {
9092
final FFprobeBuilder builder = new FFprobeBuilder();
91-
assertThrows(NullPointerException.class, () -> builder.setInput(null));
93+
assertThrows(NullPointerException.class, () -> builder.setInput((String) null));
94+
assertThrows(NullPointerException.class, () -> builder.setInput((File) null));
95+
assertThrows(NullPointerException.class, () -> builder.setInput((Path) null));
9296
}
9397

9498
@Test

0 commit comments

Comments
 (0)