test(ffmpeg): unit tests for core ffmpeg logic #8
50
mvnw
vendored
50
mvnw
vendored
@@ -19,7 +19,7 @@
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
# Apache Maven Wrapper startup batch script, version 3.3.2
|
||||
# Apache Maven Wrapper startup batch script, version 3.3.4
|
||||
#
|
||||
# Optional ENV vars
|
||||
# -----------------
|
||||
@@ -105,14 +105,17 @@ trim() {
|
||||
printf "%s" "${1}" | tr -d '[:space:]'
|
||||
}
|
||||
|
||||
scriptDir="$(dirname "$0")"
|
||||
scriptName="$(basename "$0")"
|
||||
|
||||
# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties
|
||||
while IFS="=" read -r key value; do
|
||||
case "${key-}" in
|
||||
distributionUrl) distributionUrl=$(trim "${value-}") ;;
|
||||
distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;;
|
||||
esac
|
||||
done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties"
|
||||
[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties"
|
||||
done <"$scriptDir/.mvn/wrapper/maven-wrapper.properties"
|
||||
[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties"
|
||||
|
||||
case "${distributionUrl##*/}" in
|
||||
maven-mvnd-*bin.*)
|
||||
@@ -130,7 +133,7 @@ maven-mvnd-*bin.*)
|
||||
distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip"
|
||||
;;
|
||||
maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;;
|
||||
*) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
|
||||
*) MVN_CMD="mvn${scriptName#mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
|
||||
esac
|
||||
|
||||
# apply MVNW_REPOURL and calculate MAVEN_HOME
|
||||
@@ -227,7 +230,7 @@ if [ -n "${distributionSha256Sum-}" ]; then
|
||||
echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
|
||||
exit 1
|
||||
elif command -v sha256sum >/dev/null; then
|
||||
if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then
|
||||
if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c - >/dev/null 2>&1; then
|
||||
distributionSha256Result=true
|
||||
fi
|
||||
elif command -v shasum >/dev/null; then
|
||||
@@ -252,8 +255,41 @@ if command -v unzip >/dev/null; then
|
||||
else
|
||||
tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar"
|
||||
fi
|
||||
printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url"
|
||||
mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME"
|
||||
|
||||
# Find the actual extracted directory name (handles snapshots where filename != directory name)
|
||||
actualDistributionDir=""
|
||||
|
||||
# First try the expected directory name (for regular distributions)
|
||||
if [ -d "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" ]; then
|
||||
if [ -f "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/bin/$MVN_CMD" ]; then
|
||||
actualDistributionDir="$distributionUrlNameMain"
|
||||
fi
|
||||
fi
|
||||
|
||||
# If not found, search for any directory with the Maven executable (for snapshots)
|
||||
if [ -z "$actualDistributionDir" ]; then
|
||||
# enable globbing to iterate over items
|
||||
set +f
|
||||
for dir in "$TMP_DOWNLOAD_DIR"/*; do
|
||||
if [ -d "$dir" ]; then
|
||||
if [ -f "$dir/bin/$MVN_CMD" ]; then
|
||||
actualDistributionDir="$(basename "$dir")"
|
||||
break
|
||||
fi
|
||||
fi
|
||||
done
|
||||
set -f
|
||||
fi
|
||||
|
||||
if [ -z "$actualDistributionDir" ]; then
|
||||
verbose "Contents of $TMP_DOWNLOAD_DIR:"
|
||||
verbose "$(ls -la "$TMP_DOWNLOAD_DIR")"
|
||||
die "Could not find Maven distribution directory in extracted archive"
|
||||
fi
|
||||
|
||||
verbose "Found extracted Maven distribution directory: $actualDistributionDir"
|
||||
printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$actualDistributionDir/mvnw.url"
|
||||
mv -- "$TMP_DOWNLOAD_DIR/$actualDistributionDir" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME"
|
||||
|
||||
clean || :
|
||||
exec_maven "$@"
|
||||
|
||||
56
mvnw.cmd
vendored
56
mvnw.cmd
vendored
@@ -19,7 +19,7 @@
|
||||
@REM ----------------------------------------------------------------------------
|
||||
|
||||
@REM ----------------------------------------------------------------------------
|
||||
@REM Apache Maven Wrapper startup batch script, version 3.3.2
|
||||
@REM Apache Maven Wrapper startup batch script, version 3.3.4
|
||||
@REM
|
||||
@REM Optional ENV vars
|
||||
@REM MVNW_REPOURL - repo url base for downloading maven distribution
|
||||
@@ -40,7 +40,7 @@
|
||||
@SET __MVNW_ARG0_NAME__=
|
||||
@SET MVNW_USERNAME=
|
||||
@SET MVNW_PASSWORD=
|
||||
@IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*)
|
||||
@IF NOT "%__MVNW_CMD__%"=="" ("%__MVNW_CMD__%" %*)
|
||||
@echo Cannot start maven from wrapper >&2 && exit /b 1
|
||||
@GOTO :EOF
|
||||
: end batch / begin powershell #>
|
||||
@@ -73,16 +73,30 @@ switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) {
|
||||
# apply MVNW_REPOURL and calculate MAVEN_HOME
|
||||
# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
|
||||
if ($env:MVNW_REPOURL) {
|
||||
$MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" }
|
||||
$distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')"
|
||||
$MVNW_REPO_PATTERN = if ($USE_MVND -eq $False) { "/org/apache/maven/" } else { "/maven/mvnd/" }
|
||||
$distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace "^.*$MVNW_REPO_PATTERN",'')"
|
||||
}
|
||||
$distributionUrlName = $distributionUrl -replace '^.*/',''
|
||||
$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$',''
|
||||
$MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain"
|
||||
|
||||
$MAVEN_M2_PATH = "$HOME/.m2"
|
||||
if ($env:MAVEN_USER_HOME) {
|
||||
$MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain"
|
||||
$MAVEN_M2_PATH = "$env:MAVEN_USER_HOME"
|
||||
}
|
||||
$MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join ''
|
||||
|
||||
if (-not (Test-Path -Path $MAVEN_M2_PATH)) {
|
||||
New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null
|
||||
}
|
||||
|
||||
$MAVEN_WRAPPER_DISTS = $null
|
||||
if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) {
|
||||
$MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists"
|
||||
} else {
|
||||
$MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists"
|
||||
}
|
||||
|
||||
$MAVEN_HOME_PARENT = "$MAVEN_WRAPPER_DISTS/$distributionUrlNameMain"
|
||||
$MAVEN_HOME_NAME = ([System.Security.Cryptography.SHA256]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join ''
|
||||
$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME"
|
||||
|
||||
if (Test-Path -Path "$MAVEN_HOME" -PathType Container) {
|
||||
@@ -134,7 +148,33 @@ if ($distributionSha256Sum) {
|
||||
|
||||
# unzip and move
|
||||
Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null
|
||||
Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null
|
||||
|
||||
# Find the actual extracted directory name (handles snapshots where filename != directory name)
|
||||
$actualDistributionDir = ""
|
||||
|
||||
# First try the expected directory name (for regular distributions)
|
||||
$expectedPath = Join-Path "$TMP_DOWNLOAD_DIR" "$distributionUrlNameMain"
|
||||
$expectedMvnPath = Join-Path "$expectedPath" "bin/$MVN_CMD"
|
||||
if ((Test-Path -Path $expectedPath -PathType Container) -and (Test-Path -Path $expectedMvnPath -PathType Leaf)) {
|
||||
$actualDistributionDir = $distributionUrlNameMain
|
||||
}
|
||||
|
||||
# If not found, search for any directory with the Maven executable (for snapshots)
|
||||
if (!$actualDistributionDir) {
|
||||
Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory | ForEach-Object {
|
||||
$testPath = Join-Path $_.FullName "bin/$MVN_CMD"
|
||||
if (Test-Path -Path $testPath -PathType Leaf) {
|
||||
$actualDistributionDir = $_.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$actualDistributionDir) {
|
||||
Write-Error "Could not find Maven distribution directory in extracted archive"
|
||||
}
|
||||
|
||||
Write-Verbose "Found extracted Maven distribution directory: $actualDistributionDir"
|
||||
Rename-Item -Path "$TMP_DOWNLOAD_DIR/$actualDistributionDir" -NewName $MAVEN_HOME_NAME | Out-Null
|
||||
try {
|
||||
Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null
|
||||
} catch {
|
||||
|
||||
5
pom.xml
5
pom.xml
@@ -85,6 +85,11 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -106,7 +106,7 @@ public class CompressionService {
|
||||
return command;
|
||||
}
|
||||
|
||||
private List<String> buildCommand(Path inputFile, Path outputFile, ClipOptions clipOptions) {
|
||||
protected List<String> buildCommand(Path inputFile, Path outputFile, ClipOptions clipOptions) {
|
||||
List<String> command = new ArrayList<>();
|
||||
command.add("ffmpeg");
|
||||
command.add("-progress");
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
package com.ddf.vodsystem.services.media;
|
||||
|
||||
import com.ddf.vodsystem.dto.ClipOptions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class CompressionServiceTest {
|
||||
@Mock
|
||||
private CommandRunner commandRunner;
|
||||
|
||||
private CompressionService compressionService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
compressionService = new CompressionService(commandRunner);
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildCommand_minimalClipOptions_smallCommand() {
|
||||
Path input = Path.of("/tmp/input.mp4");
|
||||
Path output = Path.of("/tmp/output.mp4");
|
||||
ClipOptions options = new ClipOptions();
|
||||
options.setTitle("hello");
|
||||
options.setStartPoint(10.0f);
|
||||
options.setDuration(30.0f);
|
||||
|
||||
List<String> command = compressionService.buildCommand(input, output, options);
|
||||
|
||||
assertThat(command).containsSubsequence("-ss", "10.0");
|
||||
assertThat(command).containsSubsequence("-t", "30.0");
|
||||
assertThat(command).containsSubsequence("-i", input.toAbsolutePath().toString());
|
||||
|
||||
assertThat(command).doesNotContain("-vf");
|
||||
assertThat(command).doesNotContain("-b:v");
|
||||
|
||||
assertThat(command).endsWith(output.toAbsolutePath().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildCommand_hasFileSize_hasBitrateOptions() {
|
||||
Path input = Path.of("/tmp/input.mp4");
|
||||
Path output = Path.of("/tmp/output.mp4");
|
||||
ClipOptions options = new ClipOptions();
|
||||
|
||||
options.setTitle("hello");
|
||||
options.setStartPoint(10.0f);
|
||||
options.setDuration(30.0f);
|
||||
options.setFileSize(10.0f);
|
||||
|
||||
List<String> command = compressionService.buildCommand(input, output, options);
|
||||
|
||||
assertThat(command).contains("-b:v", "-b:a");
|
||||
int bvIndex = command.indexOf("-b:v");
|
||||
int baIndex = command.indexOf("-b:a");
|
||||
|
||||
assertThat(command.get(bvIndex + 1)).isNotBlank().endsWith("k");
|
||||
assertThat(command.get(baIndex + 1)).isNotBlank().endsWith("k");
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildCommand_hasWidthHeight_hasVideoFilters() {
|
||||
Path input = Path.of("/tmp/input.mp4");
|
||||
Path output = Path.of("/tmp/output.mp4");
|
||||
ClipOptions options = new ClipOptions();
|
||||
|
||||
options.setTitle("hello");
|
||||
options.setStartPoint(10.0f);
|
||||
options.setDuration(30.0f);
|
||||
options.setWidth(1920);
|
||||
options.setHeight(1080);
|
||||
|
||||
List<String> command = compressionService.buildCommand(input, output, options);
|
||||
|
||||
assertThat(command).containsSequence("-vf", "scale=1920:1080");
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildCommand_onlyWidth_aspectRatioKept() {
|
||||
Path input = Path.of("/tmp/input.mp4");
|
||||
Path output = Path.of("/tmp/output.mp4");
|
||||
ClipOptions options = new ClipOptions();
|
||||
|
||||
options.setTitle("hello");
|
||||
options.setStartPoint(10.0f);
|
||||
options.setDuration(30.0f);
|
||||
options.setWidth(1920);
|
||||
|
||||
List<String> command = compressionService.buildCommand(input, output, options);
|
||||
|
||||
assertThat(command).containsSequence("-vf", "scale=1920:-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildCommand_onlyHeight_aspectRatioKept() {
|
||||
Path input = Path.of("/tmp/input.mp4");
|
||||
Path output = Path.of("/tmp/output.mp4");
|
||||
ClipOptions options = new ClipOptions();
|
||||
|
||||
options.setTitle("hello");
|
||||
options.setStartPoint(10.0f);
|
||||
options.setDuration(30.0f);
|
||||
options.setHeight(1080);
|
||||
|
||||
List<String> command = compressionService.buildCommand(input, output, options);
|
||||
|
||||
assertThat(command).containsSequence("-vf", "scale=-1:1080");
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildCommand_onlyHasFps_hasNoScaleFilter() {
|
||||
Path input = Path.of("/tmp/input.mp4");
|
||||
Path output = Path.of("/tmp/output.mp4");
|
||||
ClipOptions options = new ClipOptions();
|
||||
|
||||
options.setTitle("hello");
|
||||
options.setStartPoint(10.0f);
|
||||
options.setDuration(30.0f);
|
||||
options.setFps(60f);
|
||||
|
||||
List<String> command = compressionService.buildCommand(input, output, options);
|
||||
|
||||
assertThat(command).containsSequence("-vf", "fps=60.0");
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildCommand_noInputFile_throwsNullPointer() {
|
||||
Path output = Path.of("/tmp/output.mp4");
|
||||
ClipOptions options = new ClipOptions();
|
||||
|
||||
options.setTitle("hello");
|
||||
options.setStartPoint(10.0f);
|
||||
options.setDuration(30.0f);
|
||||
|
||||
assertThrows(NullPointerException.class, () ->
|
||||
compressionService.buildCommand(null, output, options)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildCommand_noOutputFile_throwsNullPointer() {
|
||||
Path input = Path.of("/tmp/output.mp4");
|
||||
ClipOptions options = new ClipOptions();
|
||||
|
||||
options.setTitle("hello");
|
||||
options.setStartPoint(10.0f);
|
||||
options.setDuration(30.0f);
|
||||
|
||||
assertThrows(NullPointerException.class, () ->
|
||||
compressionService.buildCommand(input, null, options)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user