fix(media): propagate async FFmpeg results and failures
This commit is contained in:
@@ -32,18 +32,27 @@ public class CompressionService {
|
||||
Path outputFile,
|
||||
ClipOptions clipOptions,
|
||||
ProgressTracker progress
|
||||
) throws IOException, InterruptedException {
|
||||
) {
|
||||
logger.info("Compressing video from {} to {}", inputFile.toAbsolutePath(), outputFile.toAbsolutePath());
|
||||
|
||||
List<String> command = buildCommand(
|
||||
inputFile,
|
||||
outputFile,
|
||||
clipOptions
|
||||
);
|
||||
CommandOutput result = commandRunner.run(command, line -> commandRunner.setProgress(line, progress, clipOptions.getDuration()));
|
||||
progress.markComplete();
|
||||
try {
|
||||
List<String> command = buildCommand(
|
||||
inputFile,
|
||||
outputFile,
|
||||
clipOptions
|
||||
);
|
||||
CommandOutput result = commandRunner.run(command, line -> commandRunner.setProgress(line, progress, clipOptions.getDuration()));
|
||||
progress.markComplete();
|
||||
|
||||
return CompletableFuture.completedFuture(result);
|
||||
return CompletableFuture.completedFuture(result);
|
||||
} catch (IOException e) {
|
||||
logger.error("IO error on compress call: {}", e.toString());
|
||||
return CompletableFuture.failedFuture(e);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Thread error on compress call: {}", e.toString());
|
||||
Thread.currentThread().interrupt();
|
||||
return CompletableFuture.failedFuture(e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> buildFilters(Float fps, Integer width, Integer height) {
|
||||
|
||||
@@ -14,7 +14,6 @@ import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
@Service
|
||||
public class MetadataService {
|
||||
@@ -28,7 +27,7 @@ public class MetadataService {
|
||||
}
|
||||
|
||||
@Async("ffmpegTaskExecutor")
|
||||
public Future<ClipOptions> getVideoMetadata(Path file) {
|
||||
public CompletableFuture<ClipOptions> getVideoMetadata(Path file) {
|
||||
logger.info("Getting metadata for file {}", file);
|
||||
|
||||
List<String> command = List.of(
|
||||
@@ -53,10 +52,10 @@ public class MetadataService {
|
||||
JsonNode node = mapper.readTree(outputBuilder.toString());
|
||||
return CompletableFuture.completedFuture(parseVideoMetadata(node));
|
||||
} catch (IOException e) {
|
||||
throw new FFMPEGException("Error while getting video metadata: " + e);
|
||||
return CompletableFuture.failedFuture(e);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new FFMPEGException("Error while getting video metadata: " + e);
|
||||
return CompletableFuture.failedFuture(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.ddf.vodsystem.services.media;
|
||||
|
||||
import com.ddf.vodsystem.dto.CommandOutput;
|
||||
import com.ddf.vodsystem.dto.ProgressTracker;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -12,6 +14,7 @@ import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Service
|
||||
public class RemuxService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(RemuxService.class);
|
||||
private final CommandRunner commandRunner;
|
||||
|
||||
public RemuxService(CommandRunner commandRunner) {
|
||||
@@ -23,19 +26,28 @@ public class RemuxService {
|
||||
File outputFile,
|
||||
ProgressTracker remuxProgress,
|
||||
float length
|
||||
) throws IOException, InterruptedException {
|
||||
List<String> command = List.of(
|
||||
"ffmpeg",
|
||||
"-progress", "pipe:1",
|
||||
"-y",
|
||||
"-i", inputFile.getAbsolutePath(),
|
||||
"-c:v", "h264",
|
||||
"-c:a", "aac",
|
||||
"-f", "mp4",
|
||||
outputFile.getAbsolutePath()
|
||||
);
|
||||
) {
|
||||
try {
|
||||
List<String> command = List.of(
|
||||
"ffmpeg",
|
||||
"-progress", "pipe:1",
|
||||
"-y",
|
||||
"-i", inputFile.getAbsolutePath(),
|
||||
"-c:v", "h264",
|
||||
"-c:a", "aac",
|
||||
"-f", "mp4",
|
||||
outputFile.getAbsolutePath()
|
||||
);
|
||||
|
||||
return CompletableFuture.completedFuture(commandRunner.run(command, line ->
|
||||
commandRunner.setProgress(line, remuxProgress, length)));
|
||||
return CompletableFuture.completedFuture(commandRunner.run(command, line ->
|
||||
commandRunner.setProgress(line, remuxProgress, length)));
|
||||
} catch (IOException e) {
|
||||
logger.error("IO error on remux call: {}", e.toString());
|
||||
return CompletableFuture.failedFuture(e);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Thread error on remux call: {}", e.toString());
|
||||
Thread.currentThread().interrupt();
|
||||
return CompletableFuture.failedFuture(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.ddf.vodsystem.services.media;
|
||||
|
||||
import com.ddf.vodsystem.dto.CommandOutput;
|
||||
import com.ddf.vodsystem.dto.ProgressTracker;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -54,12 +55,11 @@ public class StreamActionsService {
|
||||
* to extract
|
||||
* @param outputFile the destination file the trimmed section will be
|
||||
* written to; overwritten if it already exists
|
||||
* @return a {@link CompletableFuture} that completes with
|
||||
* {@code outputFile} on success, or completes exceptionally on
|
||||
* failure
|
||||
* @return a {@link CompletableFuture} that completes with the ffmpeg
|
||||
* command output on success, or completes exceptionally on failure
|
||||
*/
|
||||
@Async("ffmpegTaskExecutor")
|
||||
public CompletableFuture<Path> saveSection(
|
||||
public CompletableFuture<CommandOutput> saveSection(
|
||||
List<Path> segments,
|
||||
float trimOffset,
|
||||
float duration,
|
||||
@@ -87,12 +87,19 @@ public class StreamActionsService {
|
||||
|
||||
logger.info("Saving section with ({} segments) to '{}'",
|
||||
segments.size(), outputFile);
|
||||
commandRunner.run(command, line -> commandRunner.setProgress(line, progressTracker, duration));
|
||||
progressTracker.markComplete();
|
||||
CommandOutput output = commandRunner.run(command, line -> commandRunner.setProgress(line, progressTracker, duration));
|
||||
|
||||
return CompletableFuture.completedFuture(outputFile);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to save section to '{}'", outputFile, e);
|
||||
progressTracker.markComplete();
|
||||
return CompletableFuture.completedFuture(output);
|
||||
} catch (IOException e) {
|
||||
logger.error("IO error on saveSection call: {}", e.toString());
|
||||
return CompletableFuture.failedFuture(e);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Thread error on saveSection call: {}", e.toString());
|
||||
Thread.currentThread().interrupt();
|
||||
return CompletableFuture.failedFuture(e);
|
||||
} catch (RuntimeException e) {
|
||||
logger.error("Failed to save section to '{}': {}", outputFile, e.toString());
|
||||
return CompletableFuture.failedFuture(e);
|
||||
}
|
||||
}
|
||||
@@ -130,4 +137,4 @@ public class StreamActionsService {
|
||||
// nginx hls_fragment_naming system uses seconds; convert to ms
|
||||
return value < 1_000_000_000_000L ? value * 1000L : value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.ddf.vodsystem.services.media;
|
||||
|
||||
import com.ddf.vodsystem.dto.CommandOutput;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
@@ -8,6 +9,7 @@ import org.springframework.stereotype.Service;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Service
|
||||
public class ThumbnailService {
|
||||
@@ -19,17 +21,25 @@ public class ThumbnailService {
|
||||
}
|
||||
|
||||
@Async("ffmpegTaskExecutor")
|
||||
public void createThumbnail(Path inputFile, Path outputFile, Float timeInVideo) throws IOException, InterruptedException {
|
||||
public CompletableFuture<CommandOutput> createThumbnail(Path inputFile, Path outputFile, Float timeInVideo) {
|
||||
logger.info("Creating thumbnail at {} seconds", timeInVideo);
|
||||
|
||||
List<String> command = List.of(
|
||||
"ffmpeg",
|
||||
"-ss", timeInVideo.toString(),
|
||||
"-i", inputFile.toAbsolutePath().toString(),
|
||||
"-frames:v", "1",
|
||||
outputFile.toAbsolutePath().toString()
|
||||
);
|
||||
try {
|
||||
List<String> command = List.of(
|
||||
"ffmpeg",
|
||||
"-ss", timeInVideo.toString(),
|
||||
"-i", inputFile.toAbsolutePath().toString(),
|
||||
"-frames:v", "1",
|
||||
outputFile.toAbsolutePath().toString()
|
||||
);
|
||||
|
||||
commandRunner.run(command);
|
||||
CommandOutput output = commandRunner.run(command);
|
||||
return CompletableFuture.completedFuture(output);
|
||||
} catch (IOException e) {
|
||||
return CompletableFuture.failedFuture(e);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return CompletableFuture.failedFuture(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user