Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Railway.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ FROM rob93c/stickerify:$DOCKER_TAG
ARG STICKERIFY_TOKEN
ARG LOG_LEVEL
ARG CONCURRENT_PROCESSES
ARG VIDEO_BUFFER_SIZE
ENV STICKERIFY_TOKEN=$STICKERIFY_TOKEN \
LOG_LEVEL=$LOG_LEVEL \
CONCURRENT_PROCESSES=$CONCURRENT_PROCESSES
CONCURRENT_PROCESSES=$CONCURRENT_PROCESSES \
VIDEO_BUFFER_SIZE=$VIDEO_BUFFER_SIZE
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ test {
javaLauncher = providers.provider { new JlinkJavaLauncher(jlink.get()) }

useJUnitPlatform()
jvmArgs('--enable-final-field-mutation=ALL-UNNAMED')

testLogging {
events('started', 'passed', 'failed', 'skipped')
Expand All @@ -86,7 +87,7 @@ installDist.mustRunAfter(generateCohArchive)

application {
mainClass = 'com.github.stickerifier.stickerify.runner.Main'
applicationDefaultJvmArgs = ['-XX:+UseCompactObjectHeaders', '-XX:+UseShenandoahGC', '-XX:ShenandoahGCMode=generational']
applicationDefaultJvmArgs = ['-XX:+UseCompactObjectHeaders', '-XX:+UseShenandoahGC', '-XX:ShenandoahGCMode=generational', '--enable-final-field-mutation=ALL-UNNAMED']
}

distributions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public final class MediaHelper {
private static final int WEBP_ANIMATION_BIT_MASK = 0x02;
private static final String WEBP_EXTENDED_FILE_FORMAT = "VP8X";

private static final String VIDEO_BUFFER_SIZE = getVideoBufferSize();

/**
* Based on the type of passed-in file, it converts it into the proper media.
* If no conversion was needed, {@code null} is returned.
Expand Down Expand Up @@ -476,16 +478,25 @@ private static File convertToWebm(File file) throws MediaException, InterruptedE
"-i", file.getAbsolutePath(),
"-vf", "scale='if(gt(iw,ih),%1$d,%2$d)':'if(gt(iw,ih),%2$d,%1$d)',fps='min(%3$d,source_fps)'".formatted(MAX_SIDE_LENGTH, VIDEO_KEEP_ASPECT_RATIO, MAX_VIDEO_FRAMES),
"-c:v", "libvpx-" + VP9_CODEC,
"-b:v", "650K",
"-row-mt", "1",
"-threads", "2",
"-b:v", VIDEO_BUFFER_SIZE,
"-maxrate", VIDEO_BUFFER_SIZE,
"-bufsize", VIDEO_BUFFER_SIZE,
"-qmin", "25",
"-qmax", "63",
"-g", "120",
"-auto-alt-ref", "0",
"-pix_fmt", "yuv420p",
"-t", String.valueOf(MAX_VIDEO_DURATION_SECONDS),
"-an",
"-enc_time_base", "1/1000", // solves issues related to highly precise fps count
"-passlogfile", logPrefix
};

try {
ProcessHelper.executeCommand(buildFfmpegCommand(baseCommand, "-pass", "1", "-f", "webm", OsConstants.NULL_FILE));
ProcessHelper.executeCommand(buildFfmpegCommand(baseCommand, "-pass", "2", webmVideo.getAbsolutePath()));
ProcessHelper.executeCommand(buildFfmpegCommand(baseCommand, "-cpu-used", "8", "-pass", "1", "-f", "webm", OsConstants.NULL_FILE));
ProcessHelper.executeCommand(buildFfmpegCommand(baseCommand, "-cpu-used", "4", "-pass", "2", webmVideo.getAbsolutePath()));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
} catch (ProcessException e) {
try {
deleteFile(webmVideo);
Expand Down Expand Up @@ -519,6 +530,12 @@ private static String[] buildFfmpegCommand(String[] baseCommand, String... addit
return commands;
}

private static String getVideoBufferSize() {
var videoBufferSize = System.getenv("VIDEO_BUFFER_SIZE");

return videoBufferSize == null ? "600K" : videoBufferSize;
}

private MediaHelper() {
throw new UnsupportedOperationException();
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/customUnixStartScript.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

# from https://github.com/gradle/gradle/blob/68744f918ea82142a63c2904d346473799814be6/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# from https://github.com/gradle/gradle/blob/41203b0be36cabf983b0ebe9e402e1f37a67b0cc/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt

#
# Copyright © 2015 the original authors.
Expand Down Expand Up @@ -59,7 +59,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/${gitRef}/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#<% /*
# ... and if you're reading this, this IS the template just mentioned.
Expand Down Expand Up @@ -92,7 +92,7 @@
# (Template Engines) for details.
#
# (An example invocation of this template is from
# https://github.com/gradle/gradle/blob/HEAD/subprojects/build-init/src/main/java/org/gradle/api/tasks/wrapper/Wrapper.java
# https://github.com/gradle/gradle/blob/HEAD/platforms/software/build-init/src/main/java/org/gradle/api/tasks/wrapper/Wrapper.java
# within the Gradle project, which builds "gradlew".)
# */ %>
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down
33 changes: 11 additions & 22 deletions src/main/resources/customWindowsStartScript.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@rem from https://github.com/gradle/gradle/blob/68744f918ea82142a63c2904d346473799814be6/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/windowsStartScript.txt
@rem from https://github.com/gradle/gradle/blob/41203b0be36cabf983b0ebe9e402e1f37a67b0cc/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/windowsStartScript.txt

@rem
@rem Copyright 2015 the original author or authors.
Expand All @@ -25,8 +25,8 @@
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
@rem Set local scope for the variables, and ensure extensions are enabled
setlocal EnableExtensions

set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.\
Expand Down Expand Up @@ -57,7 +57,7 @@ echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail
"%COMSPEC%" /c exit 1
Comment thread
coderabbitai[bot] marked this conversation as resolved.

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
Expand All @@ -71,7 +71,7 @@ echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail
"%COMSPEC%" /c exit 1

:execute
@rem Setup the command line
Expand All @@ -82,21 +82,10 @@ set CLASSPATH=$classpath
<% if ( mainClassName.startsWith('--module ') ) { %>set MODULE_PATH=$modulePath<% } %>

@rem Execute ${applicationName}
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %${optsEnvironmentVar}% <% if ( appNameSystemProperty ) { %>"-D${appNameSystemProperty}=%APP_BASE_NAME%"<% } %><% if ( classpath ) {%> -classpath "%CLASSPATH%"<% } %> <% if ( mainClassName.startsWith('--module ') ) { %>--module-path "%MODULE_PATH%" <% } %>${mainClassName ?: entryPointArgs} %*
@rem endlocal doesn't take effect until after the line is parsed and variables are expanded
@rem which allows us to clear the local environment before executing the java command
endlocal & "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %${optsEnvironmentVar}% <% if ( appNameSystemProperty ) { %>"-D${appNameSystemProperty}=%APP_BASE_NAME%"<% } %><% if ( classpath ) {%> -classpath "%CLASSPATH%"<% } %> <% if ( mainClassName.startsWith('--module ') ) { %>--module-path "%MODULE_PATH%" <% } %>${mainClassName ?: entryPointArgs} %* & call :exitWithErrorLevel

:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable ${exitEnvironmentVar} if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%${exitEnvironmentVar}%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
:exitWithErrorLevel
@rem Use "%COMSPEC%" /c exit to allow operators to work properly in scripts
"%COMSPEC%" /c exit %ERRORLEVEL%
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private static void assertVideoConsistency(@Nullable File video, int expectedWid
() -> assertThat("video's frame rate is not correct", videoInfo.frameRate(), is(equalTo(expectedFrameRate))),
() -> assertThat("video must be encoded with the VP9 codec", videoInfo.codec(), is(equalTo(VP9_CODEC))),
() -> assertThat("video's duration is not correct", formatInfo.duration(), is(equalTo(expectedDuration))),
() -> assertThat("video's format must be matroska", formatInfo.format(), startsWith(MATROSKA_FORMAT)),
() -> assertThat("video's format must be Matroska", formatInfo.format(), startsWith(MATROSKA_FORMAT)),
() -> assertThat("video must have no audio stream", mediaInfo.audio(), is(nullValue())),
() -> assertThat("video size should not exceed 256 KB", formatInfo.size(), is(lessThanOrEqualTo(MAX_VIDEO_FILE_SIZE)))
);
Expand Down Expand Up @@ -272,6 +272,15 @@ void resizeAnimatedWebpVideo() {
assertThat(ex.getMessage(), equalTo("The file with image/webp MIME type is not supported"));
}

@Test
@Tag(Tags.VIDEO)
void resizeVideoWithHighlyAccurateFpsCount() throws Exception {
var mp4Video = loadResource("highly_accurate_fps_count.mp4");
var result = MediaHelper.convert(mp4Video);

assertVideoConsistency(result, 512, 434, 26F, 2.961F);
}

@Test
@Tag(Tags.ANIMATED_STICKER)
void noAnimatedStickerConversionNeeded() throws Exception {
Expand Down
Binary file added src/test/resources/highly_accurate_fps_count.mp4
Binary file not shown.
Loading