REFACTOR job to be a DTO, CompressionService is now stateless

This commit is contained in:
2025-05-11 22:26:32 +02:00
parent 518a1f3f9f
commit 7e75e16749
7 changed files with 78 additions and 121 deletions

View File

@@ -0,0 +1,29 @@
package com.ddf.vodsystem.entities;
import lombok.Data;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Data
public class Job {
private static final Logger logger = LoggerFactory.getLogger(Job.class);
private String uuid;
private File inputFile;
private File outputFile;
// configs
private ClipConfig clipConfig;
// job status
private JobStatus status = JobStatus.PENDING;
private Float progress = 0.0f;
public Job(String uuid, File inputFile, File outputFile) {
this.uuid = uuid;
this.inputFile = inputFile;
this.outputFile = outputFile;
}
}

View File

@@ -1,7 +1,8 @@
package com.ddf.vodsystem.services; package com.ddf.vodsystem.services;
import com.ddf.vodsystem.entities.ClipConfig; import com.ddf.vodsystem.entities.ClipConfig;
import lombok.Data; import com.ddf.vodsystem.entities.JobStatus;
import com.ddf.vodsystem.entities.Job;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@@ -11,32 +12,14 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import static java.lang.Long.parseLong; @Service
public class CompressionService { public class CompressionService {
private static final Logger logger = LoggerFactory.getLogger(CompressionService.class); private static final Logger logger = LoggerFactory.getLogger(CompressionService.class);
private List<String> command;
@Getter @Setter
private File inputFile;
@Getter @Setter
private File outputFile;
@Getter @Setter
private ClipConfig clipConfig;
private final Float startPoint;
private final Float endPoint;
private final Integer width;
private final Integer height;
private final Float fps;
private final Float fileSize;
private static final float AUDIO_RATIO = 0.15f; private static final float AUDIO_RATIO = 0.15f;
private static final float MAX_AUDIO_BITRATE = 128f; private static final float MAX_AUDIO_BITRATE = 128f;
private static final float BITRATE_MULTIPLIER = 0.9f; private static final float BITRATE_MULTIPLIER = 0.9f;
@@ -44,26 +27,7 @@ public class CompressionService {
private Pattern timePattern = Pattern.compile("time=([\\d:.]+)"); private Pattern timePattern = Pattern.compile("time=([\\d:.]+)");
private long out_time_ms; private long out_time_ms;
public CompressionService(File file, File output, ClipConfig clipConfig) { private void buildFilters(ArrayList<String> command, Float fps, Integer width, Integer height) {
command = new ArrayList<>();
command.add("ffmpeg");
command.add("-progress");
command.add("pipe:1");
command.add("-y");
this.inputFile = file;
this.outputFile = output;
this.clipConfig = clipConfig;
this.startPoint = clipConfig.getStartPoint();
this.endPoint = clipConfig.getEndPoint();
this.fps = clipConfig.getFps();
this.fileSize = clipConfig.getFileSize();
this.width = clipConfig.getWidth();
this.height = clipConfig.getHeight();
}
private void buildFilters() {
List<String> filters = new ArrayList<>(); List<String> filters = new ArrayList<>();
if (fps != null) { if (fps != null) {
@@ -82,8 +46,7 @@ public class CompressionService {
} }
} }
private void buildBitrate() { private void buildBitrate(ArrayList<String> command, Float length, Float fileSize) {
float length = endPoint - startPoint;
float bitrate = ((fileSize * 8) / length) * BITRATE_MULTIPLIER; float bitrate = ((fileSize * 8) / length) * BITRATE_MULTIPLIER;
float audio_bitrate = bitrate * AUDIO_RATIO; float audio_bitrate = bitrate * AUDIO_RATIO;
@@ -102,29 +65,37 @@ public class CompressionService {
command.add(audio_bitrate + "k"); command.add(audio_bitrate + "k");
} }
private void buildInputs(){ private void buildInputs(ArrayList<String> command, File inputFile, Float startPoint, Float endPoint) {
if (startPoint != null) { if (startPoint == null) {
command.add("-ss"); startPoint = 0f;
command.add(startPoint.toString());
} }
command.add("-ss");
command.add(startPoint.toString());
command.add("-i"); command.add("-i");
command.add(inputFile.getAbsolutePath()); command.add(inputFile.getAbsolutePath());
if (endPoint != null) { if (endPoint != null) {
Float length = endPoint - startPoint;
command.add("-t"); command.add("-t");
command.add(length.toString());
Float duration = endPoint - startPoint;
command.add(duration.toString());
} }
} }
private ProcessBuilder buildCommand() { private ProcessBuilder buildCommand(File inputFile, File outputFile, ClipConfig clipConfig) {
buildInputs(); ArrayList<String> command = new ArrayList<>();
buildFilters(); command.add("ffmpeg");
command.add("-progress");
command.add("pipe:1");
command.add("-y");
if (fileSize != null) { Float length = clipConfig.getEndPoint() - clipConfig.getStartPoint();
buildBitrate(); buildInputs(command, inputFile, clipConfig.getStartPoint(), clipConfig.getEndPoint());
buildFilters(command, clipConfig.getFps(), clipConfig.getWidth(), clipConfig.getHeight());
if (clipConfig.getFileSize() != null) {
buildBitrate(command, length, clipConfig.getFileSize());
} }
// Output file // Output file
@@ -134,22 +105,22 @@ public class CompressionService {
return new ProcessBuilder(command); return new ProcessBuilder(command);
} }
public void run() throws IOException, InterruptedException { public void run(Job job) throws IOException, InterruptedException {
logger.info("FFMPEG starting..."); logger.info("FFMPEG starting...");
ProcessBuilder pb = buildCommand();
ProcessBuilder pb = buildCommand(job.getInputFile(), job.getOutputFile(), job.getClipConfig());
pb.redirectErrorStream(true); pb.redirectErrorStream(true);
Process process = pb.start(); Process process = pb.start();
job.setStatus(JobStatus.RUNNING);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
logger.debug(line); logger.debug(line);
// if (line.startsWith("out_time_ms=")) {
// out_time_ms = parseLong(line.substring("out_time_ms=".length()));
// }
} }
job.setStatus(JobStatus.FINISHED);
logger.info("FFMPEG finished"); logger.info("FFMPEG finished");
} }

View File

@@ -3,7 +3,7 @@ package com.ddf.vodsystem.services;
import com.ddf.vodsystem.entities.JobStatus; import com.ddf.vodsystem.entities.JobStatus;
import com.ddf.vodsystem.exceptions.JobNotFinished; import com.ddf.vodsystem.exceptions.JobNotFinished;
import com.ddf.vodsystem.exceptions.JobNotFound; import com.ddf.vodsystem.exceptions.JobNotFound;
import com.ddf.vodsystem.tools.Job; import com.ddf.vodsystem.entities.Job;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;

View File

@@ -1,7 +1,7 @@
package com.ddf.vodsystem.services; package com.ddf.vodsystem.services;
import com.ddf.vodsystem.entities.ClipConfig; import com.ddf.vodsystem.entities.ClipConfig;
import com.ddf.vodsystem.tools.Job; import com.ddf.vodsystem.entities.Job;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
@@ -22,8 +22,6 @@ public class EditService {
} }
job.setClipConfig(clipConfig); job.setClipConfig(clipConfig);
} }
public void jobReady(String uuid) { public void jobReady(String uuid) {

View File

@@ -1,6 +1,6 @@
package com.ddf.vodsystem.services; package com.ddf.vodsystem.services;
import com.ddf.vodsystem.tools.Job; import com.ddf.vodsystem.entities.Job;
import com.ddf.vodsystem.entities.JobStatus; import com.ddf.vodsystem.entities.JobStatus;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
@@ -16,6 +17,11 @@ public class JobService {
private static final Logger logger = LoggerFactory.getLogger(JobService.class); private static final Logger logger = LoggerFactory.getLogger(JobService.class);
private final HashMap<String, Job> jobs = new HashMap<>(); private final HashMap<String, Job> jobs = new HashMap<>();
private final LinkedList<Job> jobQueue = new LinkedList<>(); private final LinkedList<Job> jobQueue = new LinkedList<>();
private final CompressionService compressionService;
public JobService(CompressionService compressionService) {
this.compressionService = compressionService;
}
public void add(Job job) { public void add(Job job) {
logger.info("Added job: {}", job.getUuid()); logger.info("Added job: {}", job.getUuid());
@@ -43,10 +49,15 @@ public class JobService {
Thread thread = new Thread(() -> { Thread thread = new Thread(() -> {
while (true) { while (true) {
if (!jobQueue.isEmpty()) { if (!jobQueue.isEmpty()) {
Job task = jobQueue.poll(); Job job = jobQueue.poll();
logger.info("Starting job {}", task.getUuid()); logger.info("Starting job {}", job.getUuid());
task.run(); // Execute the task
try {
compressionService.run(job);// Execute the task
} catch (IOException | InterruptedException e) {
logger.error("Error while running job {}", job.getUuid(), e);
}
} }
} }

View File

@@ -1,6 +1,6 @@
package com.ddf.vodsystem.services; package com.ddf.vodsystem.services;
import com.ddf.vodsystem.tools.Job; import com.ddf.vodsystem.entities.Job;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;

View File

@@ -1,52 +0,0 @@
package com.ddf.vodsystem.tools;
import com.ddf.vodsystem.entities.ClipConfig;
import com.ddf.vodsystem.entities.JobStatus;
import com.ddf.vodsystem.services.CompressionService;
import lombok.Data;
import java.io.File;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Data
public class Job implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(Job.class);
private String uuid;
private File inputFile;
private File outputFile;
// configs
private ClipConfig clipConfig;
// job status
private JobStatus status = JobStatus.PENDING;
private Float progress = 0.0f;
public Job(String uuid, File inputFile, File outputFile) {
this.uuid = uuid;
this.inputFile = inputFile;
this.outputFile = outputFile;
}
@Override
public void run() {
logger.info("Job {} started", uuid);
this.status = JobStatus.RUNNING;
CompressionService f = new CompressionService(inputFile, outputFile, clipConfig);
try {
f.run();
} catch (IOException | InterruptedException e) {
logger.error(e.getMessage());
}
this.status = JobStatus.FINISHED;
inputFile.delete();
logger.info("Job {} finished", uuid);
}
}