* Complex filtergraphs are those which cannot be described as simply a linear processing chain * applied to one stream. This is the case, for example, when the graph has more than one input @@ -178,14 +178,25 @@ public FFmpeg setComplexFilter(final String complexFilter) { /** * Sets the 'generic' filter value (equivalent to the "-filter" command-line parameter). * - * @param filter a FilterGraph describing the filter to apply + * @param filter a filter to apply * @return this * @see Simple filtergraphs */ - public FFmpeg setFilter(final FilterGraph filter) { + public FFmpeg setFilter(final Filter filter) { return setFilter(filter.getValue()); } + /** + * Sets the 'generic' filter value (equivalent to the "-filter" command-line parameter). + * + * @param filterChain a filter chain to apply + * @return this + * @see Simple filtergraphs + */ + public FFmpeg setFilter(final FilterChain filterChain) { + return setFilter(filterChain.getValue()); + } + /** * Sets the 'generic' filter value (equivalent to the "-filter" command-line parameter). * @@ -197,19 +208,34 @@ public FFmpeg setFilter(final String filter) { return setFilter((String) null, filter); } + /** + * Sets a 'stream specific' filter value (equivalent to the "-av" / "-filter:a" or "-fv" / + * "-filter:v" command-line parameters). + * + * @param streamType the stream type to apply this filter to + * @param filter a filter to apply + * @return this + * @see Stream specifiers + * + * @see Simple filtergraphs + */ + public FFmpeg setFilter(final StreamType streamType, final Filter filter) { + return setFilter(streamType, filter.getValue()); + } + /** * Sets a 'stream specific' filter value (equivalent to the "-av" / "-filter:a" or "-fv" / * "-filter:v" command-line parameters). * * @param streamType the stream type to apply this filter to - * @param filterGraph a graph describing the filters to apply + * @param filterChain a filter chain to apply * @return this * @see Stream specifiers * * @see Simple filtergraphs */ - public FFmpeg setFilter(final StreamType streamType, final FilterGraph filterGraph) { - return setFilter(streamType, filterGraph.getValue()); + public FFmpeg setFilter(final StreamType streamType, final FilterChain filterChain) { + return setFilter(streamType, filterChain.getValue()); } /** @@ -233,14 +259,30 @@ public FFmpeg setFilter(final StreamType streamType, final String filter) { * * @param streamSpecifier a String specifying to which stream this filter must be applied * ("a" for audio, "v" "for video, or "" for generic 'filter') - * @param filterGraph a graph describing the filters to apply + * @param filter a filter + * @return this + * @see Stream specifiers + * + * @see Simple filtergraphs + */ + public FFmpeg setFilter(final String streamSpecifier, final Filter filter) { + return setFilter(streamSpecifier, filter.getValue()); + } + + /** + * Sets a 'stream specific' filter value (equivalent to the "-av" / "-filter:a" or "-fv" / + * "-filter:v" / "-filter" command-line parameters). + * + * @param streamSpecifier a String specifying to which stream this filter must be applied + * ("a" for audio, "v" "for video, or "" for generic 'filter') + * @param filterChain a filter chain describing the filters to apply * @return this * @see Stream specifiers * * @see Simple filtergraphs */ - public FFmpeg setFilter(final String streamSpecifier, final FilterGraph filterGraph) { - return setFilter(streamSpecifier, filterGraph.getValue()); + public FFmpeg setFilter(final String streamSpecifier, final FilterChain filterChain) { + return setFilter(streamSpecifier, filterChain.getValue()); } /** diff --git a/src/main/java/com/github/kokorin/jaffree/ffmpeg/Filter.java b/src/main/java/com/github/kokorin/jaffree/ffmpeg/Filter.java index 4ba1cf83..216849e0 100644 --- a/src/main/java/com/github/kokorin/jaffree/ffmpeg/Filter.java +++ b/src/main/java/com/github/kokorin/jaffree/ffmpeg/Filter.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Denis Kokorin + * Copyright 2021 Denis Kokorin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,173 +19,19 @@ import com.github.kokorin.jaffree.StreamType; -import java.util.ArrayList; -import java.util.List; - /** - * Represents ffmpeg filter. - *
- * Mainly this class exists to make ffmpeg filter graphs more readable for developers.
- *
- * @see ffmpeg filters documentation
+ * Implement {@link Filter} interface to provide custom ffmpeg filter.
+ * @see ffmpeg-filters
*/
-public class Filter {
- private final List
- * Arguments are escaped according to ffmpeg filter graph escaping rules.
- *
- * @param key key
- * @param value value
- * @return this
- * @see
- * filtergraph escaping
- */
- public Filter addArgument(final String key, final String value) {
- this.arguments.add(key + "=" + escape(value));
- return this;
- }
-
- /**
- * Adds already escaped filter key-value arguments.
- *
- * Passed arguments should be escaped according to ffmpeg filter graph escaping rules.
- *
- * @param key key
- * @param value value
- * @return this
- * @see
- * filtergraph escaping
- */
- public Filter addArgumentEscaped(final String key, final String value) {
- this.arguments.add(key + "=" + value);
- return this;
- }
-
- /**
- * Adds filter single argument.
- *
- * Argument is escaped according to ffmpeg filter graph escaping rules.
- *
- * @param value value
- * @return this
- * @see
- * filtergraph escaping
- */
- public Filter addArgument(final String value) {
- this.arguments.add(escape(value));
- return this;
- }
-
-
- /**
- * Adds filter single argument.
- *
- * Passed argument should be escaped according to ffmpeg filter graph escaping rules.
- *
- * @param value value
- * @return this
- * @see
- * filtergraph escaping
- */
- public Filter addArgumentEscaped(final String value) {
- this.arguments.add(value);
- return this;
- }
-
- /**
- * Adds filter output link.
- *
- * @param link outputl link name
- * @return this
- */
- public Filter addOutputLink(final String link) {
- this.outputLinks.add(link);
- return this;
- }
-
- /**
- * Prints filter description according to ffmpeg filtergraph syntax.
- *
- * @return filter description
- * @see
- * filtergraph syntax
- */
- public String getValue() {
- StringBuilder result = new StringBuilder();
-
- for (String inputLink : inputLinks) {
- result.append("[").append(inputLink).append("]");
- }
-
- result.append(name);
-
- boolean first = true;
- for (String argument : arguments) {
- if (first) {
- result.append("=");
- first = false;
- } else {
- result.append(":");
- }
- result.append(argument);
- }
-
- for (String outputLink : outputLinks) {
- result.append("[").append(outputLink).append("]");
- }
-
- return result.toString();
- }
-
+public interface Filter {
/**
* Creates {@link Filter} starting from specified stream type.
*
* @param streamType stream type
* @return Filter
*/
- public static Filter fromInputLink(final StreamType streamType) {
- return new Filter().addInputLink(streamType);
+ static GenericFilter fromInputLink(StreamType streamType) {
+ return new GenericFilter().addInputLink(streamType);
}
/**
@@ -196,8 +42,8 @@ public static Filter fromInputLink(final StreamType streamType) {
* @see
* stream specifiers
*/
- public static Filter fromInputLink(final String linkOrStreamSpecifier) {
- return new Filter().addInputLink(linkOrStreamSpecifier);
+ static GenericFilter fromInputLink(String linkOrStreamSpecifier) {
+ return new GenericFilter().addInputLink(linkOrStreamSpecifier);
}
/**
@@ -206,26 +52,16 @@ public static Filter fromInputLink(final String linkOrStreamSpecifier) {
* @param name filter
* @return Filter
*/
- public static Filter withName(final String name) {
- return new Filter().setName(name);
+ static GenericFilter withName(String name) {
+ return new GenericFilter().setName(name);
}
/**
- * A first level escaping affects the content of each filter option value, which may contain
- * the special character {@code}:{@code} used to separate values, or one of
- * the escaping characters {@code}\'{@code}.
+ * Prints filter description according to ffmpeg filtergraph syntax.
*
- * @param value value to be escaped
- * @return escaped value
+ * @return filter description
+ * @see
+ * filtergraph syntax
*/
- static String escape(final String value) {
- if (value == null) {
- return null;
- }
-
- return value
- .replace("\\", "\\\\")
- .replace(":", "\\:")
- .replace("'", "\\'");
- }
+ String getValue();
}
diff --git a/src/main/java/com/github/kokorin/jaffree/ffmpeg/GenericFilter.java b/src/main/java/com/github/kokorin/jaffree/ffmpeg/GenericFilter.java
new file mode 100644
index 00000000..d5e9d6c3
--- /dev/null
+++ b/src/main/java/com/github/kokorin/jaffree/ffmpeg/GenericFilter.java
@@ -0,0 +1,200 @@
+/*
+ * Copyright 2017 Denis Kokorin
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package com.github.kokorin.jaffree.ffmpeg;
+
+import com.github.kokorin.jaffree.StreamType;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Represents ffmpeg filter.
+ *
+ * Mainly this class exists to make ffmpeg filter graphs more readable for developers.
+ *
+ * @see ffmpeg filters documentation
+ */
+public class GenericFilter implements Filter {
+ private final List
+ * Arguments are escaped according to ffmpeg filter graph escaping rules.
+ *
+ * @param key key
+ * @param value value
+ * @return this
+ * @see
+ * filtergraph escaping
+ */
+ public GenericFilter addArgument(final String key, final String value) {
+ this.arguments.add(key + "=" + escape(value));
+ return this;
+ }
+
+ /**
+ * Adds already escaped filter key-value arguments.
+ *
+ * Passed arguments should be escaped according to ffmpeg filter graph escaping rules.
+ *
+ * @param key key
+ * @param value value
+ * @return this
+ * @see
+ * filtergraph escaping
+ */
+ public GenericFilter addArgumentEscaped(final String key, final String value) {
+ this.arguments.add(key + "=" + value);
+ return this;
+ }
+
+ /**
+ * Adds filter single argument.
+ *
+ * Argument is escaped according to ffmpeg filter graph escaping rules.
+ *
+ * @param value value
+ * @return this
+ * @see
+ * filtergraph escaping
+ */
+ public GenericFilter addArgument(final String value) {
+ this.arguments.add(escape(value));
+ return this;
+ }
+
+
+ /**
+ * Adds filter single argument.
+ *
+ * Passed argument should be escaped according to ffmpeg filter graph escaping rules.
+ *
+ * @param value value
+ * @return this
+ * @see
+ * filtergraph escaping
+ */
+ public GenericFilter addArgumentEscaped(final String value) {
+ this.arguments.add(value);
+ return this;
+ }
+
+ /**
+ * Adds filter output link.
+ *
+ * @param link outputl link name
+ * @return this
+ */
+ public GenericFilter addOutputLink(final String link) {
+ this.outputLinks.add(link);
+ return this;
+ }
+
+ /**
+ * Prints filter description according to ffmpeg filtergraph syntax.
+ *
+ * @return filter description
+ * @see
+ * filtergraph syntax
+ */
+ @Override
+ public String getValue() {
+ StringBuilder result = new StringBuilder();
+
+ for (String inputLink : inputLinks) {
+ result.append("[").append(inputLink).append("]");
+ }
+
+ result.append(name);
+
+ boolean first = true;
+ for (String argument : arguments) {
+ if (first) {
+ result.append("=");
+ first = false;
+ } else {
+ result.append(":");
+ }
+ result.append(argument);
+ }
+
+ for (String outputLink : outputLinks) {
+ result.append("[").append(outputLink).append("]");
+ }
+
+ return result.toString();
+ }
+
+ /**
+ * A first level escaping affects the content of each filter option value, which may contain
+ * the special character {@code}:{@code} used to separate values, or one of
+ * the escaping characters {@code}\'{@code}.
+ *
+ * @param value value to be escaped
+ * @return escaped value
+ */
+ static String escape(final String value) {
+ if (value == null) {
+ return null;
+ }
+
+ return value
+ .replace("\\", "\\\\")
+ .replace(":", "\\:")
+ .replace("'", "\\'");
+ }
+}
diff --git a/src/main/java/module-info.java b/src/main/java9/module-info.java
similarity index 100%
rename from src/main/java/module-info.java
rename to src/main/java9/module-info.java
diff --git a/src/test/java/com/github/kokorin/jaffree/ffmpeg/FilterTest.java b/src/test/java/com/github/kokorin/jaffree/ffmpeg/FilterTest.java
index 7ea730b4..26f144b5 100644
--- a/src/test/java/com/github/kokorin/jaffree/ffmpeg/FilterTest.java
+++ b/src/test/java/com/github/kokorin/jaffree/ffmpeg/FilterTest.java
@@ -7,7 +7,7 @@ public class FilterTest {
@Test
public void testGetValue() throws Exception {
String expected = "[0:1][0:2]amerge";
- String actual = new Filter()
+ String actual = new GenericFilter()
.addInputLink("0:1")
.addInputLink("0:2")
.setName("amerge")
@@ -21,7 +21,7 @@ public void testGetValue2() throws Exception {
// The following graph description will generate a red source with an opacity of 0.2,
// with size "qcif" and a frame rate of 10 frames per second.
String expected = "color=c=red@0.2:s=qcif:r=10";
- String actual = new Filter()
+ String actual = new GenericFilter()
.setName("color")
.addArgument("c", "red@0.2")
.addArgument("s", "qcif")
@@ -33,13 +33,13 @@ public void testGetValue2() throws Exception {
@Test
public void testEscape() throws Exception {
- Assert.assertEquals("\\\\", Filter.escape("\\"));
- Assert.assertEquals("\\'", Filter.escape("'"));
- Assert.assertEquals("\\:", Filter.escape(":"));
+ Assert.assertEquals("\\\\", GenericFilter.escape("\\"));
+ Assert.assertEquals("\\'", GenericFilter.escape("'"));
+ Assert.assertEquals("\\:", GenericFilter.escape(":"));
String text = "this is a 'string': may contain one, or more, special characters";
String expected = "this is a \\'string\\'\\: may contain one, or more, special characters";
- Assert.assertEquals(expected, Filter.escape(text));
+ Assert.assertEquals(expected, GenericFilter.escape(text));
}
}
\ No newline at end of file