Skip to content

Commit 246c9d1

Browse files
committed
fix(hls): Move setFormat into the OutputBuilder
1 parent 78273a3 commit 246c9d1

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ apidocs
2121
*.log
2222
*.log.mbtree
2323
output.mp4
24+
tmp/

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class FFmpegBuilder {
2727

2828
public enum Strict {
29-
VERY, // strictly conform to a older more strict version of the specifications or reference
29+
VERY, // strictly conform to an older more strict version of the specifications or reference
3030
// software
3131
STRICT, // strictly conform to all the things in the specificiations no matter what consequences
3232
NORMAL, // normal
@@ -269,16 +269,14 @@ public FFmpegOutputBuilder addOutput(URI uri) {
269269
* <pre>
270270
* <code>List&lt;String&gt; args = new FFmpegBuilder()
271271
* .addHlsOutput(&quot;output.m3u8&quot;)
272-
* .build();</code>
272+
* .done().build();</code>
273273
* </pre>
274274
*
275275
* @param filename output file path
276276
*
277277
* @return A new {@link FFmpegHlsOutputBuilder}
278278
*/
279279
public FFmpegHlsOutputBuilder addHlsOutput(String filename) {
280-
checkArgument(format == null || format.equals("hls"),"The format is already set to a value other than hls.");
281-
if(format == null) setFormat("hls");
282280
FFmpegHlsOutputBuilder output = new FFmpegHlsOutputBuilder(this, filename);
283281
outputs.add(output);
284282
return output;
@@ -316,7 +314,7 @@ public FFmpegOutputBuilder addStdoutOutput() {
316314

317315
@CheckReturnValue
318316
public List<String> build() {
319-
ImmutableList.Builder<String> args = new ImmutableList.Builder<String>();
317+
ImmutableList.Builder<String> args = new ImmutableList.Builder<>();
320318

321319
Preconditions.checkArgument(!inputs.isEmpty(), "At least one input must be specified");
322320
Preconditions.checkArgument(!outputs.isEmpty(), "At least one output must be specified");

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

+10
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,18 @@ public class FFmpegHlsOutputBuilder extends AbstractFFmpegOutputBuilder<FFmpegHl
1919

2020
protected FFmpegHlsOutputBuilder(FFmpegBuilder parent, String filename) {
2121
super(parent, filename);
22+
setFormat("hls");
2223
}
2324

25+
@Override
26+
public FFmpegHlsOutputBuilder setFormat(String format) {
27+
if (format == null || !format.equals("hls")) {
28+
throw new IllegalArgumentException("Format cannot be set to anything else except 'hls' for FFmpegHlsOutputBuilder");
29+
}
30+
super.setFormat(format);
31+
32+
return this;
33+
}
2434

2535
/**
2636
* Set the target segment length. Default value is 2 seconds.

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

+37-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package net.bramp.ffmpeg.builder;
22

33
import com.google.common.collect.ImmutableList;
4+
5+
import java.nio.file.Files;
6+
import java.nio.file.Paths;
47
import java.util.concurrent.TimeUnit;
8+
9+
import net.bramp.ffmpeg.FFmpeg;
10+
import net.bramp.ffmpeg.fixtures.Samples;
511
import org.junit.Test;
612
import java.io.IOException;
713
import java.util.List;
814

915
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertTrue;
1017

1118
public class FFmpegHlsOutputBuilderTest {
12-
public FFmpegHlsOutputBuilderTest() throws IOException {
19+
public FFmpegHlsOutputBuilderTest() {
1320
}
1421

1522
@Test
@@ -25,10 +32,10 @@ public void testAddHlsOutput() {
2532
.setHlsSegmentFileName("file%03d.ts")
2633
.done()
2734
.build();
28-
assertEquals(
29-
args, ImmutableList.of("-y", "-v", "error", "-f", "hls", "-i", "input", "-hls_time", "00:00:00.005",
35+
36+
assertEquals(ImmutableList.of("-y", "-v", "error", "-i", "input", "-f", "hls", "-hls_time", "00:00:00.005",
3037
"-hls_segment_filename", "file%03d.ts", "-hls_init_time", "00:00:00.003",
31-
"-hls_list_size", "3", "-hls_base_url", "test1234/", "output.m3u8"));
38+
"-hls_list_size", "3", "-hls_base_url", "test1234/", "output.m3u8"), args);
3239
}
3340

3441
@Test
@@ -46,11 +53,33 @@ public void mixedHlsAndDefault() {
4653
.setHlsSegmentFileName("file%03d.ts")
4754
.done()
4855
.build();
49-
assertEquals(
50-
args, ImmutableList.of("-y", "-v", "error", "-f","hls", "-i", "input","-b:v","3","-vf","TEST","-hls_time", "00:00:00.005",
56+
57+
assertEquals(ImmutableList.of("-y", "-v", "error", "-i", "input","-f","hls","-b:v","3","-vf","TEST","-hls_time", "00:00:00.005",
5158
"-hls_segment_filename", "file%03d.ts", "-hls_init_time", "00:00:00.003",
52-
"-hls_list_size", "3", "-hls_base_url", "test1234/", "output.m3u8"));
59+
"-hls_list_size", "3", "-hls_base_url", "test1234/", "output.m3u8"), args);
5360
}
5461

62+
@Test
63+
public void testConvertVideoToHls() throws IOException {
64+
Files.createDirectories(Paths.get("tmp/"));
65+
Files.deleteIfExists(Paths.get("tmp/output.m3u8"));
66+
Files.deleteIfExists(Paths.get("tmp/file000.m3u8"));
67+
68+
List<String> command = new FFmpegBuilder()
69+
.setInput(Samples.TEST_PREFIX + Samples.base_big_buck_bunny_720p_1mb)
70+
.addHlsOutput("tmp/output.m3u8")
71+
.setHlsTime(5, TimeUnit.SECONDS)
72+
.setHlsBaseUrl("test1234/")
73+
.setVideoBitRate(1000)
74+
.setHlsListSize(3)
75+
.setHlsInitTime(3, TimeUnit.MILLISECONDS)
76+
.setHlsSegmentFileName("tmp/file%03d.ts")
77+
.done()
78+
.build();
5579

56-
}
80+
new FFmpeg().run(command);
81+
82+
assertTrue(Files.exists(Paths.get("tmp/output.m3u8")));
83+
assertTrue(Files.exists(Paths.get("tmp/file000.ts")));
84+
}
85+
}

0 commit comments

Comments
 (0)