Skip to content

Commit

Permalink
Merge pull request kokorin#264 from kokorin/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
kokorin authored Dec 30, 2021
2 parents a00e443 + 0186c42 commit e27ae88
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 77 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.14.1</version>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.10</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -235,7 +235,7 @@
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.9.0.2155</version>
<version>3.9.1.2184</version>
</plugin>

<plugin>
Expand All @@ -251,7 +251,7 @@
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>9.0.1</version>
<version>9.2.1</version>
</dependency>
</dependencies>
<executions>
Expand Down
45 changes: 25 additions & 20 deletions src/main/java/com/github/kokorin/jaffree/Rational.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public Rational negate() {
* @return {@code this + val}
*/
public Rational add(final Number value) {
Rational that = toRational(value);
Rational that = valueOf(value);
return new Rational(
this.numerator * that.denominator + that.numerator * this.denominator,
this.denominator * that.denominator
Expand All @@ -136,7 +136,7 @@ public Rational add(final Number value) {
* @return {@code this - val}
*/
public Rational subtract(final Number value) {
Rational that = toRational(value);
Rational that = valueOf(value);
return add(that.negate());
}

Expand All @@ -147,7 +147,7 @@ public Rational subtract(final Number value) {
* @return {@code this * value}
*/
public Rational multiply(final Number value) {
Rational that = toRational(value);
Rational that = valueOf(value);
return new Rational(this.numerator * that.numerator, this.denominator * that.denominator);
}

Expand All @@ -158,7 +158,7 @@ public Rational multiply(final Number value) {
* @return {@code this / value}
*/
public Rational divide(final Number value) {
return multiply(toRational(value).inverse());
return multiply(valueOf(value).inverse());
}

/**
Expand All @@ -178,7 +178,7 @@ public Rational inverse() {
* @return {@code this < value}
*/
public boolean lessThan(final Number that) {
return compareTo(toRational(that)) < 0;
return compareTo(valueOf(that)) < 0;
}

/**
Expand All @@ -188,7 +188,7 @@ public boolean lessThan(final Number that) {
* @return {@code this <= value}
*/
public boolean lessThanOrEqual(final Number that) {
return compareTo(toRational(that)) <= 0;
return compareTo(valueOf(that)) <= 0;
}

/**
Expand All @@ -198,7 +198,7 @@ public boolean lessThanOrEqual(final Number that) {
* @return {@code this > value}
*/
public boolean greaterThan(final Number that) {
return compareTo(toRational(that)) > 0;
return compareTo(valueOf(that)) > 0;
}

/**
Expand All @@ -208,7 +208,7 @@ public boolean greaterThan(final Number that) {
* @return {@code this >= value}
*/
public boolean greaterThanOrEqual(final Number that) {
return compareTo(toRational(that)) >= 0;
return compareTo(valueOf(that)) >= 0;
}

/**
Expand Down Expand Up @@ -303,6 +303,23 @@ public static Rational valueOf(final double value) {
return new Rational(numerator, denominator);
}

/**
* Returns a {@link Rational} whose value is equal to that of the specified {@code Number}.
*
* @param value value of the {@link Rational} to return.
* @return a {@link Rational} with the specified value.
*/
public static Rational valueOf(final Number value) {
if (value instanceof Rational) {
return (Rational) value;
}

if (value instanceof Double || value instanceof Float) {
return valueOf(value.doubleValue());
}

return valueOf(value.longValue());
}

/**
* Parses {@link Rational}.
Expand Down Expand Up @@ -343,18 +360,6 @@ public static Rational valueOf(final String value, final String delimiter)
}
}

private static Rational toRational(final Number value) {
if (value instanceof Rational) {
return (Rational) value;
}

if (value instanceof Double || value instanceof Float) {
return valueOf(value.doubleValue());
}

return valueOf(value.longValue());
}

/**
* Returns greatest common divisor.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public FilterChain addFilter(final Filter filter) {
* @param filtersToAdd filters to add
* @return this
*/
public FilterChain addFilters(final List<Filter> filtersToAdd) {
public FilterChain addFilters(final List<? extends Filter> filtersToAdd) {
filters.addAll(filtersToAdd);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public FilterGraph addFilterChain(final FilterChain chain) {
* @param chainsToAdd filter chains to add
* @return this
*/
public FilterGraph addFilterChains(final List<FilterChain> chainsToAdd) {
public FilterGraph addFilterChains(final List<? extends FilterChain> chainsToAdd) {
chains.addAll(chainsToAdd);
return this;
}
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/com/github/kokorin/jaffree/ffmpeg/GenericFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public GenericFilter addArgumentEscaped(final String key, final String value) {
*
* @param value value
* @return this
* @see <a href="https://ffmpeg.org/ffmpeg-filters.html#toc-Notes-on-filtergraph-escaping">
* @see <a href="https://ffmpeg.org/ffmpeg-filters.html#Notes-on-filtergraph-escaping">
* filtergraph escaping</a>
*/
public GenericFilter addArgument(final String value) {
Expand All @@ -125,7 +125,7 @@ public GenericFilter addArgument(final String value) {
*
* @param value value
* @return this
* @see <a href="https://ffmpeg.org/ffmpeg-filters.html#toc-Notes-on-filtergraph-escaping">
* @see <a href="https://ffmpeg.org/ffmpeg-filters.html#Notes-on-filtergraph-escaping">
* filtergraph escaping</a>
*/
public GenericFilter addArgumentEscaped(final String value) {
Expand All @@ -148,7 +148,7 @@ public GenericFilter addOutputLink(final String link) {
* Prints filter description according to ffmpeg filtergraph syntax.
*
* @return filter description
* @see <a href="https://ffmpeg.org/ffmpeg-filters.html#toc-Filtergraph-syntax-1">
* @see <a href="https://ffmpeg.org/ffmpeg-filters.html#Filtergraph-syntax-1">
* filtergraph syntax</a>
*/
@Override
Expand Down Expand Up @@ -180,12 +180,16 @@ public String getValue() {
}

/**
* 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}.
* An escaping affects the content of each filter option value, which may contain the special
* character.
* <p>
* This method implements 1st and 2nd level escaping. 3rd level escaping (shell command) is done
* by Java Process API.
*
* @param value value to be escaped
* @return escaped value
* @see <a href="https://ffmpeg.org/ffmpeg-filters.html#Notes-on-filtergraph-escaping">
* filtergraph escaping</a>
*/
static String escape(final String value) {
if (value == null) {
Expand All @@ -194,7 +198,11 @@ static String escape(final String value) {

return value
.replace("\\", "\\\\")
.replace(":", "\\:")
.replace("'", "\\'");
.replace(":", "\\\\:")
.replace(",", "\\,")
.replace("[", "\\[")
.replace("]", "\\]")
.replace(";", "\\;")
.replace("'", "\\\\\\'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,9 @@ public Rational convert(final Object value) {
if (value == null || value.equals("") || value.equals("0/0") || value.equals("N/A")) {
return null;
}
if (value instanceof Double) {
return Rational.valueOf((Double) value);
}
if (value instanceof Float) {
return Rational.valueOf((Float) value);
}

if (value instanceof Number) {
return Rational.valueOf(((Number) value).longValue());
return Rational.valueOf((Number) value);
}

try {
Expand Down
10 changes: 7 additions & 3 deletions src/test/java/com/github/kokorin/jaffree/RationalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ public void valueOf() {
assertEquals(new Rational(1L, 10_000_000_000_000_000L), Rational.valueOf(1. / 10_000_000_000_000_000L));
assertEquals(new Rational(10_000_000_000_000_000L, 1L), Rational.valueOf(10_000_000_000_000_000.));

assertEquals(Rational.valueOf(1L), Rational.valueOf("1"));
assertEquals(Rational.valueOf(1L), Rational.valueOf("1/1"));
assertEquals(new Rational(1L, 10L), Rational.valueOf("1/10"));
assertEquals(Rational.ONE, Rational.valueOf("1"));
assertEquals(Rational.ONE, Rational.valueOf("1/1"));
assertEquals(Rational.ONE, Rational.valueOf((Number) 1));
assertEquals(Rational.ONE, Rational.valueOf((Number) 1L));
assertEquals(Rational.ONE, Rational.valueOf((Number) 1.0));
assertEquals(Rational.ONE, Rational.valueOf((Number) 1.0f));
assertEquals(Rational.ONE.divide(10), Rational.valueOf("1/10"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.github.kokorin.jaffree.Artifacts;
import com.github.kokorin.jaffree.Config;
import com.github.kokorin.jaffree.LogLevel;
import com.github.kokorin.jaffree.StreamType;
import com.github.kokorin.jaffree.ffprobe.FFprobe;
import com.github.kokorin.jaffree.ffprobe.FFprobeResult;
import com.github.kokorin.jaffree.ffprobe.Stream;
Expand All @@ -14,6 +16,8 @@
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertNotNull;

public class FFmpegFilterTest {

/**
Expand Down Expand Up @@ -108,14 +112,14 @@ public void testMosaic() throws Exception {
.addOutput(UrlOutput.toPath(outputPath))
.execute();

Assert.assertNotNull(result);
assertNotNull(result);

FFprobeResult probe = FFprobe.atPath(Config.FFMPEG_BIN)
.setInput(outputPath)
.setShowStreams(true)
.execute();

Assert.assertNotNull(probe);
assertNotNull(probe);

int width = 0;
int height = 0;
Expand Down Expand Up @@ -146,10 +150,14 @@ public void testConcatWithReencode() throws Exception {
Path outputPath = tempDir.resolve("concat.mp4");

FFmpegResult result = FFmpeg.atPath(Config.FFMPEG_BIN)
.addInput(UrlInput.fromPath(Artifacts.VIDEO_MP4).setDuration(5, TimeUnit.SECONDS))
.addInput(
UrlInput.fromPath(Artifacts.VIDEO_MKV).setPositionEof(-5, TimeUnit.SECONDS))

UrlInput.fromPath(Artifacts.VIDEO_MP4)
.setDuration(5, TimeUnit.SECONDS)
)
.addInput(
UrlInput.fromPath(Artifacts.VIDEO_MKV)
.setPositionEof(-5, TimeUnit.SECONDS)
)
.setComplexFilter(FilterGraph.of(
FilterChain.of(
Filter.fromInputLink("0:v")
Expand All @@ -174,19 +182,71 @@ public void testConcatWithReencode() throws Exception {
)
.execute();

Assert.assertNotNull(result);
assertNotNull(result);

FFprobeResult probe = FFprobe.atPath(Config.FFMPEG_BIN)
.setInput(outputPath)
.setShowStreams(true)
.execute();
assertNotNull(probe);

double duration = 0.0;
for (Stream stream : probe.getStreams()) {
duration = Math.max(duration, stream.getDuration());
}

Assert.assertNotNull(probe);
Assert.assertEquals(10.0, duration, 0.1);
}

@Test
public void drawTextWithSpecialCharacters() throws Exception {
Path tempDir = Files.createTempDirectory("jaffree");
Path outputPath = tempDir.resolve("draw_text.mp4");

FFmpegResult result = FFmpeg.atPath(Config.FFMPEG_BIN)
.addInput(
UrlInput.fromPath(Artifacts.VIDEO_MP4)
.setDuration(15, TimeUnit.SECONDS)
)
.setComplexFilter(
FilterGraph.of(
FilterChain.of(
Filter.withName("drawtext")
.addInputLink(StreamType.VIDEO)
.addArgument("text",
"this is a 'string': may contain one, or more," +
" special characters like: [ or ] or = or even ;")
.addArgument("box", "1")
.addArgument("boxborderw", "5")
.addArgument("boxcolor", "red")
.addArgument("fontsize", "24")
),
FilterChain.of(
Filter.withName("afade")
.addInputLink(StreamType.AUDIO)
.addArgument("t", "in")
.addArgument("ss", "0")
.addArgument("d", "10")
)
)
)
.addOutput(UrlOutput.toPath(outputPath))
.execute();


assertNotNull(result);

FFprobeResult probe = FFprobe.atPath(Config.FFMPEG_BIN)
.setInput(outputPath)
.setShowStreams(true)
.execute();
assertNotNull(probe);

double duration = 0.0;
for (Stream stream : probe.getStreams()) {
duration = Math.max(duration, stream.getDuration());
}

Assert.assertEquals(15.0, duration, 0.1);
}
}
Loading

0 comments on commit e27ae88

Please sign in to comment.