REFACTOR to be more modular

This commit is contained in:
2025-05-09 18:56:24 +02:00
parent de382feeff
commit 4d59456292
7 changed files with 36 additions and 68 deletions

View File

@@ -1,6 +1,6 @@
package com.ddf.vodsystem.controllers; package com.ddf.vodsystem.controllers;
import com.ddf.vodsystem.entities.EditDTO; import com.ddf.vodsystem.entities.ClipConfig;
import com.ddf.vodsystem.services.EditService; import com.ddf.vodsystem.services.EditService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@@ -17,8 +17,8 @@ public class EditController {
} }
@PostMapping("edit/{uuid}") @PostMapping("edit/{uuid}")
public ResponseEntity<String> edit(@PathVariable("uuid") String uuid, @ModelAttribute EditDTO editDTO) { public ResponseEntity<String> edit(@PathVariable("uuid") String uuid, @ModelAttribute ClipConfig clipConfig) {
editService.edit(uuid, editDTO); editService.edit(uuid, clipConfig);
return new ResponseEntity<>(uuid, HttpStatus.OK); return new ResponseEntity<>(uuid, HttpStatus.OK);
} }

View File

@@ -3,7 +3,7 @@ package com.ddf.vodsystem.entities;
import lombok.Data; import lombok.Data;
@Data @Data
public class EditDTO { public class ClipConfig {
private Float startPoint; private Float startPoint;
private Float endPoint; private Float endPoint;
private Float fps; private Float fps;

View File

@@ -1,5 +1,6 @@
package com.ddf.vodsystem.services; package com.ddf.vodsystem.services;
import com.ddf.vodsystem.entities.ClipConfig;
import lombok.Data; import lombok.Data;
import java.io.BufferedReader; import java.io.BufferedReader;
@@ -27,17 +28,14 @@ public class CompressionService {
@Getter @Setter @Getter @Setter
private File outputFile; private File outputFile;
@Getter @Setter @Getter @Setter
private Float startPoint; private ClipConfig clipConfig;
@Getter @Setter
private Float endPoint; private final Float startPoint;
@Getter @Setter private final Float endPoint;
private Integer width; private final Integer width;
@Getter @Setter private final Integer height;
private Integer height; private final Float fps;
@Getter @Setter private final Float fileSize;
private Float fps;
@Getter @Setter
private 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;
@@ -46,7 +44,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) { public CompressionService(File file, File output, ClipConfig clipConfig) {
command = new ArrayList<>(); command = new ArrayList<>();
command.add("ffmpeg"); command.add("ffmpeg");
command.add("-progress"); command.add("-progress");
@@ -55,15 +53,16 @@ public class CompressionService {
this.inputFile = file; this.inputFile = file;
this.outputFile = output; 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();
} }
public void setResolution(Integer width, Integer height) {
this.width = width;
this.height = height;
}
private void buildFilters() { private void buildFilters() {
List<String> filters = new ArrayList<>(); List<String> filters = new ArrayList<>();

View File

@@ -1,7 +1,7 @@
package com.ddf.vodsystem.services; package com.ddf.vodsystem.services;
import com.ddf.vodsystem.entities.EditDTO; import com.ddf.vodsystem.entities.ClipConfig;
import com.ddf.vodsystem.entities.Job; import com.ddf.vodsystem.tools.Job;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
@@ -12,40 +12,18 @@ public class EditService {
this.jobService = jobService; this.jobService = jobService;
} }
public void edit(String uuid, EditDTO editDTO) { public void edit(String uuid, ClipConfig clipConfig) {
Job job = jobService.get(uuid); Job job = jobService.get(uuid);
if (editDTO.getStartPoint() != null) { if (clipConfig.getStartPoint() != null) {
if (editDTO.getStartPoint() < 0) { if (clipConfig.getStartPoint() < 0) {
throw new IllegalArgumentException("Start point cannot be negative"); throw new IllegalArgumentException("Start point cannot be negative");
} }
job.setStartPoint(editDTO.getStartPoint());
} }
if (editDTO.getEndPoint() != null) { job.setClipConfig(clipConfig);
job.setEndPoint(editDTO.getEndPoint());
}
if (editDTO.getFps() != null) {
job.setFps(editDTO.getFps());
}
if (editDTO.getWidth() != null) {
job.setWidth(editDTO.getWidth());
}
if (editDTO.getHeight() != null) {
job.setHeight(editDTO.getHeight());
}
if (editDTO.getFileSize() != null) {
if (editDTO.getFileSize() < 0) {
throw new IllegalArgumentException("File size cannot be negative");
}
job.setFileSize(editDTO.getFileSize());
}
} }
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.entities.Job; import com.ddf.vodsystem.tools.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;
@@ -12,7 +12,7 @@ import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
@Service @Service
class JobService { 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<>();
@@ -39,7 +39,7 @@ class JobService {
} }
@PostConstruct @PostConstruct
public void startProcessingLoop() { private void startProcessingLoop() {
Thread thread = new Thread(() -> { Thread thread = new Thread(() -> {
while (true) { while (true) {
if (!jobQueue.isEmpty()) { if (!jobQueue.isEmpty()) {

View File

@@ -1,6 +1,6 @@
package com.ddf.vodsystem.services; package com.ddf.vodsystem.services;
import com.ddf.vodsystem.entities.Job; import com.ddf.vodsystem.tools.Job;
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;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

View File

@@ -1,5 +1,7 @@
package com.ddf.vodsystem.entities; package com.ddf.vodsystem.tools;
import com.ddf.vodsystem.entities.ClipConfig;
import com.ddf.vodsystem.entities.JobStatus;
import com.ddf.vodsystem.services.CompressionService; import com.ddf.vodsystem.services.CompressionService;
import lombok.Data; import lombok.Data;
import java.io.File; import java.io.File;
@@ -16,12 +18,7 @@ public class Job implements Runnable {
private File file; private File file;
// configs // configs
private Float startPoint; private ClipConfig clipConfig;
private Float endPoint;
private Float fps;
private Integer width;
private Integer height;
private Float fileSize;
// job status // job status
private JobStatus status = JobStatus.PENDING; private JobStatus status = JobStatus.PENDING;
@@ -37,13 +34,7 @@ public class Job implements Runnable {
logger.info("Job {} started", uuid); logger.info("Job {} started", uuid);
this.status = JobStatus.RUNNING; this.status = JobStatus.RUNNING;
CompressionService f = new CompressionService(file, new File("output.mp4")); CompressionService f = new CompressionService(file, new File("output.mp4"), clipConfig);
f.setStartPoint(startPoint);
f.setEndPoint(endPoint);
f.setFps(fps);
f.setWidth(width);
f.setHeight(height);
f.setFileSize(fileSize);
try { try {
f.run(); f.run();