refactor(directories): switch from File to Path objs
This commit is contained in:
@@ -8,8 +8,8 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -26,14 +26,18 @@ public class CompressionService {
|
||||
private final Pattern timePattern = Pattern.compile("out_time_ms=(\\d+)");
|
||||
|
||||
@Async("ffmpegTaskExecutor")
|
||||
public CompletableFuture<CommandOutput> compress(File inputFile,
|
||||
File outputFile,
|
||||
public CompletableFuture<CommandOutput> compress(Path inputFile,
|
||||
Path outputFile,
|
||||
ClipOptions clipOptions,
|
||||
ProgressTracker progress
|
||||
) throws IOException, InterruptedException {
|
||||
logger.info("Compressing video from {} to {}", inputFile.getAbsolutePath(), outputFile.getAbsolutePath());
|
||||
logger.info("Compressing video from {} to {}", inputFile.toAbsolutePath(), outputFile.toAbsolutePath());
|
||||
|
||||
List<String> command = buildCommand(inputFile, outputFile, clipOptions);
|
||||
List<String> command = buildCommand(
|
||||
inputFile,
|
||||
outputFile,
|
||||
clipOptions
|
||||
);
|
||||
CommandOutput result = CommandRunner.run(command, line -> setProgress(line, progress, clipOptions.getDuration()));
|
||||
progress.markComplete();
|
||||
|
||||
@@ -93,14 +97,14 @@ public class CompressionService {
|
||||
return command;
|
||||
}
|
||||
|
||||
private List<String> buildInputs(File inputFile, Float startPoint, Float length) {
|
||||
private List<String> buildInputs(Path inputFile, Float startPoint, Float length) {
|
||||
List<String> command = new ArrayList<>();
|
||||
|
||||
command.add("-ss");
|
||||
command.add(startPoint.toString());
|
||||
|
||||
command.add("-i");
|
||||
command.add(inputFile.getAbsolutePath());
|
||||
command.add(inputFile.toAbsolutePath().toString());
|
||||
|
||||
command.add("-t");
|
||||
command.add(Float.toString(length));
|
||||
@@ -108,7 +112,7 @@ public class CompressionService {
|
||||
return command;
|
||||
}
|
||||
|
||||
private List<String> buildCommand(File inputFile, File outputFile, ClipOptions clipOptions) {
|
||||
private List<String> buildCommand(Path inputFile, Path outputFile, ClipOptions clipOptions) {
|
||||
List<String> command = new ArrayList<>();
|
||||
command.add("ffmpeg");
|
||||
command.add("-progress");
|
||||
@@ -126,7 +130,7 @@ public class CompressionService {
|
||||
}
|
||||
|
||||
// Output file
|
||||
command.add(outputFile.getAbsolutePath());
|
||||
command.add(outputFile.toAbsolutePath().toString());
|
||||
return command;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Future;
|
||||
@@ -21,8 +21,8 @@ public class MetadataService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(MetadataService.class);
|
||||
|
||||
@Async("ffmpegTaskExecutor")
|
||||
public Future<ClipOptions> getVideoMetadata(File file) {
|
||||
logger.info("Getting metadata for file {}", file.getAbsolutePath());
|
||||
public Future<ClipOptions> getVideoMetadata(Path file) {
|
||||
logger.info("Getting metadata for file {}", file);
|
||||
|
||||
List<String> command = List.of(
|
||||
"ffprobe",
|
||||
@@ -30,7 +30,7 @@ public class MetadataService {
|
||||
"-print_format", "json",
|
||||
"-show_format", "-select_streams",
|
||||
"v:0", "-show_entries", "stream=duration,width,height,r_frame_rate:format=size,duration",
|
||||
"-i", file.getAbsolutePath()
|
||||
"-i", file.toAbsolutePath().toString()
|
||||
);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
@@ -51,48 +51,6 @@ public class MetadataService {
|
||||
}
|
||||
}
|
||||
|
||||
public void validateMetadata(ClipOptions inputFileMetadata, ClipOptions outputFileMetadata) {
|
||||
Float start = outputFileMetadata.getStartPoint();
|
||||
Float duration = outputFileMetadata.getDuration();
|
||||
Float fileSize = outputFileMetadata.getFileSize();
|
||||
Integer width = outputFileMetadata.getWidth();
|
||||
Integer height = outputFileMetadata.getHeight();
|
||||
Float fps = outputFileMetadata.getFps();
|
||||
|
||||
if (start == null) {
|
||||
outputFileMetadata.setStartPoint(0f);
|
||||
}
|
||||
|
||||
if (duration == null) {
|
||||
outputFileMetadata.setDuration(inputFileMetadata.getDuration());
|
||||
}
|
||||
|
||||
if (start != null && start < 0) {
|
||||
throw new IllegalArgumentException("Start point cannot be negative");
|
||||
}
|
||||
|
||||
if (duration != null && duration < 0) {
|
||||
throw new IllegalArgumentException("Duration cannot be negative");
|
||||
}
|
||||
|
||||
if (fileSize != null && fileSize < 100) {
|
||||
throw new IllegalArgumentException("File size cannot be less than 100kb");
|
||||
}
|
||||
|
||||
if (width != null && width < 1) {
|
||||
throw new IllegalArgumentException("Width cannot be less than 1");
|
||||
}
|
||||
|
||||
if (height != null && height < 1) {
|
||||
throw new IllegalArgumentException("Height cannot be less than 1");
|
||||
}
|
||||
|
||||
if (fps != null && fps < 1) {
|
||||
throw new IllegalArgumentException("FPS cannot be less than 1");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private ClipOptions parseVideoMetadata(JsonNode node) {
|
||||
ClipOptions metadata = new ClipOptions();
|
||||
metadata.setStartPoint(0f);
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -47,16 +47,16 @@ public class StreamActionsService {
|
||||
* failure
|
||||
*/
|
||||
@Async("ffmpegTaskExecutor")
|
||||
public CompletableFuture<File> saveSection(
|
||||
List<File> segments,
|
||||
public CompletableFuture<Path> saveSection(
|
||||
List<Path> segments,
|
||||
float trimOffset,
|
||||
float duration,
|
||||
File outputFile,
|
||||
Path outputFile,
|
||||
ProgressTracker progressTracker
|
||||
) {
|
||||
try {
|
||||
String concatInput = segments.stream()
|
||||
.map(File::getAbsolutePath)
|
||||
.map(p -> p.toAbsolutePath().toString())
|
||||
.collect(Collectors.joining("|"));
|
||||
|
||||
List<String> command = List.of(
|
||||
@@ -66,16 +66,16 @@ public class StreamActionsService {
|
||||
"-ss", String.valueOf(trimOffset),
|
||||
"-t", String.valueOf(duration),
|
||||
"-c", "copy",
|
||||
outputFile.getAbsolutePath()
|
||||
outputFile.toAbsolutePath().toString()
|
||||
);
|
||||
|
||||
logger.info("Saving section with ({} segments) to '{}'",
|
||||
segments.size(), outputFile.getAbsolutePath());
|
||||
segments.size(), outputFile);
|
||||
CommandRunner.run(command, line -> CommandRunner.setProgress(line, progressTracker, duration));
|
||||
|
||||
return CompletableFuture.completedFuture(outputFile);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to save section to '{}'", outputFile.getAbsolutePath(), e);
|
||||
logger.error("Failed to save section to '{}'", outputFile, e);
|
||||
return CompletableFuture.failedFuture(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@@ -14,15 +14,15 @@ public class ThumbnailService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ThumbnailService.class);
|
||||
|
||||
@Async("ffmpegTaskExecutor")
|
||||
public void createThumbnail(File inputFile, File outputFile, Float timeInVideo) throws IOException, InterruptedException {
|
||||
public void createThumbnail(Path inputFile, Path outputFile, Float timeInVideo) throws IOException, InterruptedException {
|
||||
logger.info("Creating thumbnail at {} seconds", timeInVideo);
|
||||
|
||||
List<String> command = List.of(
|
||||
"ffmpeg",
|
||||
"-ss", timeInVideo.toString(),
|
||||
"-i", inputFile.getAbsolutePath(),
|
||||
"-i", inputFile.toAbsolutePath().toString(),
|
||||
"-frames:v", "1",
|
||||
outputFile.getAbsolutePath()
|
||||
outputFile.toAbsolutePath().toString()
|
||||
);
|
||||
|
||||
CommandRunner.run(command);
|
||||
|
||||
Reference in New Issue
Block a user