Skip to content

Commit

Permalink
Merge pull request #185 from kokorin/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
kokorin authored May 31, 2021
2 parents 2a1815b + b7fca22 commit 7f028ba
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 212 deletions.
23 changes: 5 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,10 @@
</goals>
<phase>compile</phase>
<configuration>
<excludes>
<!--Any value will override excludes set by default plugin
configuration-->
<exclude>do-not-exclude-module-info.java</exclude>
</excludes>
<compileSourceRoots>
<sourceRoot>${project.basedir}/src/main/java</sourceRoot>
<sourceRoot>${project.basedir}/src/main/java9</sourceRoot>
</compileSourceRoots>
<release>9</release>
</configuration>
</execution>
Expand All @@ -133,11 +132,6 @@
<goal>compile</goal>
</goals>
<phase>compile</phase>
<configuration>
<excludes>
<exclude>module-info.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
Expand Down Expand Up @@ -234,13 +228,6 @@
<goal>compile</goal>
</goals>
<phase>compile</phase>
<configuration>
<excludes>
<!--This file contains java 9 module description,
it can't be compiled with JDK 8-->
<exclude>module-info.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
Expand All @@ -264,7 +251,7 @@
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.42</version>
<version>8.43</version>
</dependency>
</dependencies>
<executions>
Expand Down
60 changes: 51 additions & 9 deletions src/main/java/com/github/kokorin/jaffree/ffmpeg/FFmpeg.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public FFmpeg addArguments(final String key, final String value) {
}

/**
* Adds complex filter graph to ffmpeg arugments list.
* Adds complex filter graph to ffmpeg arguments list.
* <p>
* 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
Expand Down Expand Up @@ -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 <a href="https://ffmpeg.org/ffmpeg-all.html#Simple-filtergraphs">Simple filtergraphs</a>
*/
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 <a href="https://ffmpeg.org/ffmpeg-all.html#Simple-filtergraphs">Simple filtergraphs</a>
*/
public FFmpeg setFilter(final FilterChain filterChain) {
return setFilter(filterChain.getValue());
}

/**
* Sets the 'generic' filter value (equivalent to the "-filter" command-line parameter).
*
Expand All @@ -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 <a href="http://ffmpeg.org/ffmpeg-all.html#toc-Stream-specifiers-1">Stream specifiers
* </a>
* @see <a href="https://ffmpeg.org/ffmpeg-all.html#Simple-filtergraphs">Simple filtergraphs</a>
*/
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 <a href="http://ffmpeg.org/ffmpeg-all.html#toc-Stream-specifiers-1">Stream specifiers
* </a>
* @see <a href="https://ffmpeg.org/ffmpeg-all.html#Simple-filtergraphs">Simple filtergraphs</a>
*/
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());
}

/**
Expand All @@ -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 <a href="http://ffmpeg.org/ffmpeg-all.html#toc-Stream-specifiers-1">Stream specifiers
* </a>
* @see <a href="https://ffmpeg.org/ffmpeg-all.html#Simple-filtergraphs">Simple filtergraphs</a>
*/
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 <a href="http://ffmpeg.org/ffmpeg-all.html#toc-Stream-specifiers-1">Stream specifiers
* </a>
* @see <a href="https://ffmpeg.org/ffmpeg-all.html#Simple-filtergraphs">Simple filtergraphs</a>
*/
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());
}

/**
Expand Down
194 changes: 15 additions & 179 deletions src/main/java/com/github/kokorin/jaffree/ffmpeg/Filter.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,173 +19,19 @@

import com.github.kokorin.jaffree.StreamType;

import java.util.ArrayList;
import java.util.List;

/**
* Represents ffmpeg filter.
* <p>
* Mainly this class exists to make ffmpeg filter graphs more readable for developers.
*
* @see <a href="https://ffmpeg.org/ffmpeg-filters.html">ffmpeg filters documentation</a>
* Implement {@link Filter} interface to provide custom ffmpeg filter.
* @see <a href="https://ffmpeg.org/ffmpeg-filters.html">ffmpeg-filters</a>
*/
public class Filter {
private final List<String> inputLinks = new ArrayList<>();
private String name;
private final List<String> arguments = new ArrayList<>();
private final List<String> outputLinks = new ArrayList<>();

/**
* Adds filter input link.
*
* @param streamType stream type
* @return this
*/
public Filter addInputLink(final StreamType streamType) {
this.inputLinks.add(streamType.code());
return this;
}

/**
* Adds an input link to a filter.
*
* @param linkOrStreamSpecifier link name or stream specifier
* @return this
* @see <a href="https://ffmpeg.org/ffmpeg.html#Stream-specifiers">
* stream specifiers</a>
*/
public Filter addInputLink(final String linkOrStreamSpecifier) {
this.inputLinks.add(linkOrStreamSpecifier);
return this;
}

/**
* Sets filter to use.
*
* @param name filter name
* @return this
*/
public Filter setName(final String name) {
this.name = name;
return this;
}

/**
* Adds filter key-value arguments.
* <p>
* Arguments are escaped according to ffmpeg filter graph escaping rules.
*
* @param key key
* @param value value
* @return this
* @see <a href="https://ffmpeg.org/ffmpeg-filters.html#toc-Notes-on-filtergraph-escaping">
* filtergraph escaping</a>
*/
public Filter addArgument(final String key, final String value) {
this.arguments.add(key + "=" + escape(value));
return this;
}

/**
* Adds already escaped filter key-value arguments.
* <p>
* Passed arguments should be escaped according to ffmpeg filter graph escaping rules.
*
* @param key key
* @param value value
* @return this
* @see <a href="https://ffmpeg.org/ffmpeg-filters.html#toc-Notes-on-filtergraph-escaping">
* filtergraph escaping</a>
*/
public Filter addArgumentEscaped(final String key, final String value) {
this.arguments.add(key + "=" + value);
return this;
}

/**
* Adds filter single argument.
* <p>
* Argument is escaped according to ffmpeg filter graph escaping rules.
*
* @param value value
* @return this
* @see <a href="https://ffmpeg.org/ffmpeg-filters.html#toc-Notes-on-filtergraph-escaping">
* filtergraph escaping</a>
*/
public Filter addArgument(final String value) {
this.arguments.add(escape(value));
return this;
}


/**
* Adds filter single argument.
* <p>
* Passed argument should be escaped according to ffmpeg filter graph escaping rules.
*
* @param value value
* @return this
* @see <a href="https://ffmpeg.org/ffmpeg-filters.html#toc-Notes-on-filtergraph-escaping">
* filtergraph escaping</a>
*/
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 <a href="https://ffmpeg.org/ffmpeg-filters.html#toc-Filtergraph-syntax-1">
* filtergraph syntax</a>
*/
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);
}

/**
Expand All @@ -196,8 +42,8 @@ public static Filter fromInputLink(final StreamType streamType) {
* @see <a href="https://ffmpeg.org/ffmpeg.html#Stream-specifiers">
* stream specifiers</a>
*/
public static Filter fromInputLink(final String linkOrStreamSpecifier) {
return new Filter().addInputLink(linkOrStreamSpecifier);
static GenericFilter fromInputLink(String linkOrStreamSpecifier) {
return new GenericFilter().addInputLink(linkOrStreamSpecifier);
}

/**
Expand All @@ -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 <a href="https://ffmpeg.org/ffmpeg-filters.html#toc-Filtergraph-syntax-1">
* filtergraph syntax</a>
*/
static String escape(final String value) {
if (value == null) {
return null;
}

return value
.replace("\\", "\\\\")
.replace(":", "\\:")
.replace("'", "\\'");
}
String getValue();
}
Loading

0 comments on commit 7f028ba

Please sign in to comment.