Backend MP4 conversion (#23)
* ADD conversion queue * ADD RemuxService for MP4 conversion * REMOVE unused conversion queue * REORGANISE Job-related classes * ADD Job stages * REVERT to old commit, using Spring Async instead * ADD asynchronous processing for video tasks * PATCH and streamline progress tracking * ADD asynchronous video processing and job restructuring * REFACTOR job service method * ADD job remux functionality * ADD remuxing endpoint * PATCH complete flag not updating in API response * ADD progress type in frontend * ADD reset functionality for job status * PATCH missing progress bar for subsequent exports * REDESIGN settings box * ADD tracking video file conversion in frontend * PATCH extension bug * REMOVE autowired decorator
This commit is contained in:
@@ -2,10 +2,12 @@ package com.ddf.vodsystem;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
@EnableAsync
|
||||
public class VodSystemApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.ddf.vodsystem.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
@Configuration
|
||||
public class AsyncConfig {
|
||||
|
||||
@Bean(name = "ffmpegTaskExecutor")
|
||||
public Executor taskExecutor() {
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
executor.setCorePoolSize(4);
|
||||
executor.setMaxPoolSize(8);
|
||||
executor.setQueueCapacity(100);
|
||||
executor.setThreadNamePrefix("ffmpegExecutor-");
|
||||
executor.initialize();
|
||||
return executor;
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public class SecurityConfig {
|
||||
.requestMatchers("/api/v1/download/clip/**").authenticated()
|
||||
.requestMatchers("/api/v1/auth/login", "/api/v1/auth/user").permitAll()
|
||||
.requestMatchers("/api/v1/upload", "/api/v1/download/**").permitAll()
|
||||
.requestMatchers("/api/v1/edit/**", "/api/v1/process/**", "/api/v1/progress/**").permitAll()
|
||||
.requestMatchers("/api/v1/edit/**", "/api/v1/process/**", "/api/v1/progress/**", "/api/v1/convert/**").permitAll()
|
||||
.requestMatchers("/api/v1/metadata/**").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
)
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package com.ddf.vodsystem.controllers;
|
||||
|
||||
import com.ddf.vodsystem.dto.JobStatus;
|
||||
import com.ddf.vodsystem.dto.VideoMetadata;
|
||||
import com.ddf.vodsystem.services.EditService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import com.ddf.vodsystem.dto.APIResponse;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -26,22 +25,20 @@ public class EditController {
|
||||
}
|
||||
|
||||
@GetMapping("/process/{uuid}")
|
||||
public ResponseEntity<APIResponse<Void>> convert(@PathVariable("uuid") String uuid) {
|
||||
public ResponseEntity<APIResponse<Void>> process(@PathVariable("uuid") String uuid) {
|
||||
editService.process(uuid);
|
||||
return ResponseEntity.ok(new APIResponse<>(SUCCESS, "Processing started for UUID: " + uuid, null));
|
||||
}
|
||||
|
||||
@GetMapping("/progress/{uuid}")
|
||||
public ResponseEntity<APIResponse<ProgressResponse>> getProgress(@PathVariable("uuid") String uuid) {
|
||||
float progress = editService.getProgress(uuid);
|
||||
|
||||
ProgressResponse progressResponse = new ProgressResponse(progress);
|
||||
return ResponseEntity.ok(new APIResponse<>(SUCCESS, "Progress for UUID: " + uuid, progressResponse));
|
||||
@GetMapping("/convert/{uuid}")
|
||||
public ResponseEntity<APIResponse<Void>> convert(@PathVariable("uuid") String uuid) {
|
||||
editService.convert(uuid);
|
||||
return ResponseEntity.ok(new APIResponse<>(SUCCESS, "Conversion started for UUID: " + uuid, null));
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class ProgressResponse {
|
||||
private float progress;
|
||||
@GetMapping("/progress/{uuid}")
|
||||
public ResponseEntity<APIResponse<JobStatus>> getProgress(@PathVariable("uuid") String uuid) {
|
||||
JobStatus status = editService.getProgress(uuid);
|
||||
return ResponseEntity.ok(new APIResponse<>(SUCCESS, "Progress for UUID: " + uuid, status));
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
package com.ddf.vodsystem.entities;
|
||||
package com.ddf.vodsystem.dto;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import com.ddf.vodsystem.dto.ProgressTracker;
|
||||
import com.ddf.vodsystem.dto.VideoMetadata;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
|
||||
import lombok.Data;
|
||||
@@ -22,8 +20,7 @@ public class Job {
|
||||
private SecurityContext securityContext;
|
||||
|
||||
// job status
|
||||
private JobStatus status = JobStatus.NOT_READY;
|
||||
private ProgressTracker progress = new ProgressTracker(0.0f);
|
||||
private JobStatus status = new JobStatus();
|
||||
|
||||
public Job(String uuid, File inputFile, File outputFile, VideoMetadata inputVideoMetadata) {
|
||||
this.uuid = uuid;
|
||||
11
src/main/java/com/ddf/vodsystem/dto/JobStatus.java
Normal file
11
src/main/java/com/ddf/vodsystem/dto/JobStatus.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.ddf.vodsystem.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class JobStatus {
|
||||
private ProgressTracker process = new ProgressTracker();
|
||||
private ProgressTracker conversion = new ProgressTracker();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,30 @@
|
||||
package com.ddf.vodsystem.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class ProgressTracker {
|
||||
private float progress;
|
||||
private float progress = 0.0f;
|
||||
private boolean isComplete = false;
|
||||
|
||||
public ProgressTracker(float initialProgress) {
|
||||
this.progress = initialProgress;
|
||||
public synchronized float getProgress() {
|
||||
return progress;
|
||||
}
|
||||
|
||||
public void setProgress(float newProgress) {
|
||||
public synchronized boolean isComplete() {
|
||||
return isComplete;
|
||||
}
|
||||
|
||||
public synchronized void setProgress(float newProgress) {
|
||||
if (newProgress < 0 || newProgress > 1) {
|
||||
throw new IllegalArgumentException("Progress must be between 0 and 1");
|
||||
}
|
||||
this.progress = newProgress;
|
||||
}
|
||||
|
||||
public synchronized void markComplete() {
|
||||
this.isComplete = true;
|
||||
}
|
||||
|
||||
public synchronized void reset() {
|
||||
this.progress = 0.0f;
|
||||
this.isComplete = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.ddf.vodsystem.entities;
|
||||
|
||||
public enum JobStatus {
|
||||
NOT_READY,
|
||||
PENDING,
|
||||
RUNNING,
|
||||
FINISHED,
|
||||
FAILED
|
||||
}
|
||||
@@ -8,7 +8,6 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.ddf.vodsystem.exceptions.NotAuthenticated;
|
||||
import com.ddf.vodsystem.repositories.ClipRepository;
|
||||
@@ -57,26 +56,22 @@ public class ClipService {
|
||||
* @param progress A tracker to monitor the progress of the video processing.
|
||||
* @throws IOException if an I/O error occurs during file processing.
|
||||
* @throws InterruptedException if the thread is interrupted during processing.
|
||||
* @return An Optional containing the created Clip if the user is authenticated, otherwise an empty Optional.
|
||||
*/
|
||||
public Optional<Clip> create(VideoMetadata inputMetadata,
|
||||
public void create(VideoMetadata inputMetadata,
|
||||
VideoMetadata outputMetadata,
|
||||
File inputFile,
|
||||
File outputFile,
|
||||
ProgressTracker progress) throws IOException, InterruptedException {
|
||||
metadataService.normalizeVideoMetadata(inputMetadata, outputMetadata);
|
||||
compressionService.compress(inputFile, outputFile, outputMetadata, progress);
|
||||
|
||||
Float fileSize = metadataService.getVideoMetadata(outputFile).getFileSize();
|
||||
outputMetadata.setFileSize(fileSize);
|
||||
ProgressTracker progress)
|
||||
throws IOException, InterruptedException {
|
||||
|
||||
User user = userService.getUser();
|
||||
|
||||
if (user == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return Optional.of(persistClip(outputMetadata, user, outputFile, inputFile.getName()));
|
||||
metadataService.normalizeVideoMetadata(inputMetadata, outputMetadata);
|
||||
compressionService.compress(inputFile, outputFile, outputMetadata, progress)
|
||||
.thenRun(() -> {
|
||||
if (user != null) {
|
||||
persistClip(outputMetadata, user, outputFile, inputFile.getName());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public List<Clip> getClipsByUser() {
|
||||
@@ -136,7 +131,7 @@ public class ClipService {
|
||||
return user.getId().equals(clip.getUser().getId());
|
||||
}
|
||||
|
||||
private Clip persistClip(VideoMetadata videoMetadata,
|
||||
private void persistClip(VideoMetadata videoMetadata,
|
||||
User user,
|
||||
File tempFile,
|
||||
String fileName) {
|
||||
@@ -166,6 +161,6 @@ public class ClipService {
|
||||
clip.setFileSize(videoMetadata.getFileSize());
|
||||
clip.setVideoPath(clipFile.getPath());
|
||||
clip.setThumbnailPath(thumbnailFile.getPath());
|
||||
return clipRepository.save(clip);
|
||||
clipRepository.save(clip);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package com.ddf.vodsystem.services;
|
||||
|
||||
import com.ddf.vodsystem.entities.Clip;
|
||||
import com.ddf.vodsystem.entities.JobStatus;
|
||||
import com.ddf.vodsystem.exceptions.JobNotFinished;
|
||||
import com.ddf.vodsystem.exceptions.JobNotFound;
|
||||
import com.ddf.vodsystem.entities.Job;
|
||||
import com.ddf.vodsystem.dto.Job;
|
||||
import com.ddf.vodsystem.exceptions.NotAuthenticated;
|
||||
import com.ddf.vodsystem.repositories.ClipRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -21,7 +19,6 @@ public class DownloadService {
|
||||
private final ClipRepository clipRepository;
|
||||
private final ClipService clipService;
|
||||
|
||||
@Autowired
|
||||
public DownloadService(JobService jobService,
|
||||
ClipRepository clipRepository,
|
||||
ClipService clipService) {
|
||||
@@ -48,7 +45,7 @@ public class DownloadService {
|
||||
throw new JobNotFound("Job doesn't exist");
|
||||
}
|
||||
|
||||
if (job.getStatus() != JobStatus.FINISHED) {
|
||||
if (!job.getStatus().getProcess().isComplete()) {
|
||||
throw new JobNotFinished("Job is not finished");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.ddf.vodsystem.services;
|
||||
|
||||
import com.ddf.vodsystem.dto.JobStatus;
|
||||
import com.ddf.vodsystem.dto.VideoMetadata;
|
||||
import com.ddf.vodsystem.entities.*;
|
||||
import com.ddf.vodsystem.dto.Job;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@@ -20,17 +21,18 @@ public class EditService {
|
||||
}
|
||||
|
||||
public void process(String uuid) {
|
||||
jobService.jobReady(uuid);
|
||||
Job job = jobService.getJob(uuid);
|
||||
jobService.processJob(job);
|
||||
}
|
||||
|
||||
public float getProgress(String uuid) {
|
||||
public void convert(String uuid) {
|
||||
Job job = jobService.getJob(uuid);
|
||||
jobService.convertJob(job);
|
||||
}
|
||||
|
||||
if (job.getStatus() == JobStatus.FINISHED) {
|
||||
return 1f;
|
||||
}
|
||||
|
||||
return job.getProgress().getProgress();
|
||||
public JobStatus getProgress(String uuid) {
|
||||
Job job = jobService.getJob(uuid);
|
||||
return job.getStatus();
|
||||
}
|
||||
|
||||
private void validateClipConfig(VideoMetadata videoMetadata) {
|
||||
|
||||
@@ -1,22 +1,17 @@
|
||||
package com.ddf.vodsystem.services;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import com.ddf.vodsystem.dto.Job;
|
||||
import com.ddf.vodsystem.services.media.RemuxService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.ddf.vodsystem.entities.Job;
|
||||
import com.ddf.vodsystem.entities.JobStatus;
|
||||
import com.ddf.vodsystem.exceptions.JobNotFound;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* Service for managing and processing jobs in a background thread.
|
||||
* Uses a blocking queue to avoid busy waiting and ensures jobs are processed sequentially.
|
||||
@@ -25,15 +20,19 @@ import jakarta.annotation.PostConstruct;
|
||||
public class JobService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(JobService.class);
|
||||
private final ConcurrentHashMap<String, Job> jobs = new ConcurrentHashMap<>();
|
||||
private final BlockingQueue<Job> jobQueue = new LinkedBlockingQueue<>();
|
||||
private final ClipService clipService;
|
||||
private final RemuxService remuxService;
|
||||
private final DirectoryService directoryService;
|
||||
|
||||
/**
|
||||
* Constructs a JobService with the given CompressionService.
|
||||
* @param clipService the compression service to use for processing jobs
|
||||
*/
|
||||
public JobService(ClipService clipService) {
|
||||
public JobService(ClipService clipService,
|
||||
RemuxService remuxService, DirectoryService directoryService) {
|
||||
this.clipService = clipService;
|
||||
this.remuxService = remuxService;
|
||||
this.directoryService = directoryService;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,76 +60,49 @@ public class JobService {
|
||||
return job;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks a job as ready and adds it to the processing queue.
|
||||
* @param uuid the UUID of the job to mark as ready
|
||||
*/
|
||||
public void jobReady(String uuid) {
|
||||
Job job = getJob(uuid);
|
||||
public void convertJob(Job job) {
|
||||
logger.info("Converting job: {}", job.getUuid());
|
||||
File tempFile = new File(job.getInputFile().getAbsolutePath() + ".temp");
|
||||
directoryService.copyFile(job.getInputFile(), tempFile);
|
||||
|
||||
SecurityContext context = SecurityContextHolder.getContext();
|
||||
job.setSecurityContext(context);
|
||||
job.getStatus().getConversion().reset();
|
||||
|
||||
try {
|
||||
remuxService.remux(
|
||||
tempFile,
|
||||
job.getInputFile(),
|
||||
job.getStatus().getConversion(),
|
||||
job.getInputVideoMetadata().getEndPoint())
|
||||
.thenRun(() -> {
|
||||
job.getStatus().getConversion().markComplete();
|
||||
directoryService.deleteFile(tempFile);
|
||||
});
|
||||
} catch (IOException | InterruptedException e) {
|
||||
logger.error("Error converting job {}: {}", job.getUuid(), e.getMessage());
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
logger.info("Job ready: {}", job.getUuid());
|
||||
job.setStatus(JobStatus.PENDING);
|
||||
jobQueue.add(job);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a job by running the compression service.
|
||||
* @param job the job to process
|
||||
* Marks a job as ready and adds it to the processing queue.
|
||||
* @param job The job to process
|
||||
*/
|
||||
private void processJob(Job job) {
|
||||
SecurityContext previousContext = SecurityContextHolder.getContext(); // optional, for restoring later
|
||||
try {
|
||||
if (job.getSecurityContext() != null) {
|
||||
SecurityContextHolder.setContext(job.getSecurityContext());
|
||||
}
|
||||
public void processJob(Job job) {
|
||||
logger.info("Job ready: {}", job.getUuid());
|
||||
job.getStatus().getProcess().reset();
|
||||
|
||||
try {
|
||||
clipService.create(
|
||||
job.getInputVideoMetadata(),
|
||||
job.getOutputVideoMetadata(),
|
||||
job.getInputFile(),
|
||||
job.getOutputFile(),
|
||||
job.getProgress()
|
||||
job.getStatus().getProcess()
|
||||
);
|
||||
|
||||
job.setStatus(JobStatus.FINISHED);
|
||||
|
||||
} catch (IOException | InterruptedException e) {
|
||||
logger.error("Error processing job {}: {}", job.getUuid(), e.getMessage());
|
||||
Thread.currentThread().interrupt();
|
||||
logger.error("Error while running job {}", job.getUuid(), e);
|
||||
|
||||
} finally {
|
||||
// 🔄 Restore previous context to avoid leaking across jobs
|
||||
SecurityContextHolder.setContext(previousContext);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the background processing loop in a daemon thread.
|
||||
* The loop blocks until a job is available and then processes it.
|
||||
*/
|
||||
@PostConstruct
|
||||
private void startProcessingLoop() {
|
||||
Thread thread = new Thread(() -> {
|
||||
logger.info("Starting processing loop");
|
||||
while (true) {
|
||||
try {
|
||||
Job job = jobQueue.take(); // Blocks until a job is available
|
||||
|
||||
logger.info("Starting job {}", job.getUuid());
|
||||
job.setStatus(JobStatus.RUNNING);
|
||||
processJob(job);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
logger.error("Processing loop interrupted", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.ddf.vodsystem.services;
|
||||
|
||||
import com.ddf.vodsystem.entities.Job;
|
||||
import com.ddf.vodsystem.dto.Job;
|
||||
import com.ddf.vodsystem.dto.VideoMetadata;
|
||||
import com.ddf.vodsystem.exceptions.FFMPEGException;
|
||||
import com.ddf.vodsystem.services.media.MetadataService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -11,6 +11,9 @@ import java.io.File;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -23,7 +26,6 @@ public class UploadService {
|
||||
private final MetadataService metadataService;
|
||||
private final DirectoryService directoryService;
|
||||
|
||||
@Autowired
|
||||
public UploadService(JobService jobService,
|
||||
MetadataService metadataService,
|
||||
DirectoryService directoryService) {
|
||||
@@ -38,12 +40,19 @@ public class UploadService {
|
||||
String extension = directoryService.getFileExtension(file.getOriginalFilename());
|
||||
|
||||
File inputFile = directoryService.getTempInputFile(uuid + "." + extension);
|
||||
File outputFile = directoryService.getTempOutputFile(uuid + "." + extension);
|
||||
File outputFile = directoryService.getTempOutputFile(uuid + ".mp4");
|
||||
directoryService.saveAtDir(inputFile, file);
|
||||
|
||||
// add job
|
||||
logger.info("Uploaded file and creating job with UUID: {}", uuid);
|
||||
VideoMetadata videoMetadata = metadataService.getVideoMetadata(inputFile);
|
||||
|
||||
VideoMetadata videoMetadata;
|
||||
try {
|
||||
videoMetadata = metadataService.getVideoMetadata(inputFile).get(5, TimeUnit.SECONDS);
|
||||
} catch (ExecutionException | TimeoutException | InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new FFMPEGException(e.getMessage());
|
||||
}
|
||||
Job job = new Job(uuid, inputFile, outputFile, videoMetadata);
|
||||
jobService.add(job);
|
||||
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
package com.ddf.vodsystem.services.media;
|
||||
|
||||
import com.ddf.vodsystem.dto.CommandOutput;
|
||||
import com.ddf.vodsystem.dto.ProgressTracker;
|
||||
import com.ddf.vodsystem.dto.VideoMetadata;
|
||||
import org.slf4j.Logger;
|
||||
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.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -22,12 +25,21 @@ public class CompressionService {
|
||||
private static final float BITRATE_MULTIPLIER = 0.9f;
|
||||
private final Pattern timePattern = Pattern.compile("out_time_ms=(\\d+)");
|
||||
|
||||
public void compress(File inputFile, File outputFile, VideoMetadata videoMetadata, ProgressTracker progress) throws IOException, InterruptedException {
|
||||
@Async("ffmpegTaskExecutor")
|
||||
public CompletableFuture<CommandOutput> compress(File inputFile,
|
||||
File outputFile,
|
||||
VideoMetadata videoMetadata,
|
||||
ProgressTracker progress
|
||||
) throws IOException, InterruptedException {
|
||||
logger.info("Compressing video from {} to {}", inputFile.getAbsolutePath(), outputFile.getAbsolutePath());
|
||||
|
||||
float length = videoMetadata.getEndPoint() - videoMetadata.getStartPoint();
|
||||
List<String> command = buildCommand(inputFile, outputFile, videoMetadata);
|
||||
CommandRunner.run(command, line -> setProgress(line, progress, length));
|
||||
|
||||
CommandOutput result = CommandRunner.run(command, line -> setProgress(line, progress, length));
|
||||
progress.markComplete();
|
||||
|
||||
return CompletableFuture.completedFuture(result);
|
||||
}
|
||||
|
||||
private void setProgress(String line, ProgressTracker progress, float length) {
|
||||
|
||||
@@ -7,17 +7,21 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
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.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
@Service
|
||||
public class MetadataService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(MetadataService.class);
|
||||
|
||||
public VideoMetadata getVideoMetadata(File file) {
|
||||
@Async("ffmpegTaskExecutor")
|
||||
public Future<VideoMetadata> getVideoMetadata(File file) {
|
||||
logger.info("Getting metadata for file {}", file.getAbsolutePath());
|
||||
|
||||
List<String> command = List.of(
|
||||
@@ -40,7 +44,7 @@ public class MetadataService {
|
||||
}
|
||||
|
||||
JsonNode node = mapper.readTree(outputBuilder.toString());
|
||||
return parseVideoMetadata(node);
|
||||
return CompletableFuture.completedFuture(parseVideoMetadata(node));
|
||||
} catch (IOException | InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new FFMPEGException("Error while getting video metadata: " + e);
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.ddf.vodsystem.services.media;
|
||||
|
||||
import com.ddf.vodsystem.dto.CommandOutput;
|
||||
import com.ddf.vodsystem.dto.ProgressTracker;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Service
|
||||
public class RemuxService {
|
||||
private final Pattern timePattern = Pattern.compile("out_time_ms=(\\d+)");
|
||||
|
||||
@Async("ffmpegTaskExecutor")
|
||||
public CompletableFuture<CommandOutput> remux(File inputFile,
|
||||
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()
|
||||
);
|
||||
|
||||
return CompletableFuture.completedFuture(CommandRunner.run(command, line -> setProgress(line, remuxProgress, length)));
|
||||
}
|
||||
|
||||
private void setProgress(String line, ProgressTracker progress, float length) {
|
||||
Matcher matcher = timePattern.matcher(line);
|
||||
if (matcher.find()) {
|
||||
float timeInMs = Float.parseFloat(matcher.group(1)) / 1000000f;
|
||||
progress.setProgress(timeInMs / length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
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;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
@@ -13,7 +13,8 @@ import java.util.List;
|
||||
public class ThumbnailService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ThumbnailService.class);
|
||||
|
||||
public CommandOutput createThumbnail(File inputFile, File outputFile, Float timeInVideo) throws IOException, InterruptedException {
|
||||
@Async("ffmpegTaskExecutor")
|
||||
public void createThumbnail(File inputFile, File outputFile, Float timeInVideo) throws IOException, InterruptedException {
|
||||
logger.info("Creating thumbnail at {} seconds", timeInVideo);
|
||||
|
||||
List<String> command = List.of(
|
||||
@@ -24,6 +25,6 @@ public class ThumbnailService {
|
||||
outputFile.getAbsolutePath()
|
||||
);
|
||||
|
||||
return CommandRunner.run(command);
|
||||
CommandRunner.run(command);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
-- DROP TABLE IF EXISTS clips;
|
||||
-- DROP TABLE IF EXISTS users;
|
||||
DROP TABLE IF EXISTS clips;
|
||||
DROP TABLE IF EXISTS users;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
|
||||
Reference in New Issue
Block a user