From 3abdfe3978f85a51c37024408bf77eded7bc5962 Mon Sep 17 00:00:00 2001 From: ThisBirchWood Date: Thu, 3 Jul 2025 22:40:29 +0200 Subject: [PATCH] ADD file size retrieval and update VideoMetadata object --- .../ddf/vodsystem/services/ClipService.java | 48 +++++++++++++------ .../vodsystem/services/MetadataService.java | 23 ++++++++- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/ddf/vodsystem/services/ClipService.java b/src/main/java/com/ddf/vodsystem/services/ClipService.java index b1aaadc..14e202b 100644 --- a/src/main/java/com/ddf/vodsystem/services/ClipService.java +++ b/src/main/java/com/ddf/vodsystem/services/ClipService.java @@ -30,12 +30,25 @@ public class ClipService { private static final float BITRATE_MULTIPLIER = 0.9f; private final ClipRepository clipRepository; + private final MetadataService metadataService; private final Pattern timePattern = Pattern.compile("out_time_ms=(\\d+)"); - public ClipService(ClipRepository clipRepository) { + public ClipService(ClipRepository clipRepository, MetadataService metadataService) { this.clipRepository = clipRepository; + this.metadataService = metadataService; } + /** + * Runs the FFMPEG command to create a video clip based on the provided job. + * Updates the job status and progress as the command executes. + * This method validates the input and output video metadata, + * Updates the job VideoMetadata with the output file size, + * + * @param job the job containing input and output video metadata + * @throws IOException if an I/O error occurs during command execution + * @throws InterruptedException if the thread is interrupted while waiting for the process to finish + * + */ public void run(Job job) throws IOException, InterruptedException { logger.info("FFMPEG starting..."); @@ -45,6 +58,26 @@ public class ClipService { Process process = pb.start(); job.setStatus(JobStatus.RUNNING); + updateJobProgress(process, job); + + if (process.waitFor() != 0) { + job.setStatus(JobStatus.FAILED); + throw new FFMPEGException("FFMPEG process failed"); + } + + Float fileSize = metadataService.getFileSize(job.getOutputFile()); + job.getOutputVideoMetadata().setFileSize(fileSize); + + User user = getUser(); + if (user != null) { + createClip(job.getOutputVideoMetadata(), user); + } + + job.setStatus(JobStatus.FINISHED); + logger.info("FFMPEG finished"); + } + + private void updateJobProgress(Process process, Job job) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); float length = job.getOutputVideoMetadata().getEndPoint() - job.getOutputVideoMetadata().getStartPoint(); @@ -58,19 +91,6 @@ public class ClipService { job.setProgress(progress); } } - - if (process.waitFor() != 0) { - job.setStatus(JobStatus.FAILED); - throw new FFMPEGException("FFMPEG process failed"); - } - - User user = getUser(); - if (user != null) { - createClip(job.getOutputVideoMetadata(), user); - } - - job.setStatus(JobStatus.FINISHED); - logger.info("FFMPEG finished"); } private User getUser() { diff --git a/src/main/java/com/ddf/vodsystem/services/MetadataService.java b/src/main/java/com/ddf/vodsystem/services/MetadataService.java index f3bf829..3c19a06 100644 --- a/src/main/java/com/ddf/vodsystem/services/MetadataService.java +++ b/src/main/java/com/ddf/vodsystem/services/MetadataService.java @@ -1,6 +1,5 @@ package com.ddf.vodsystem.services; -import com.ddf.vodsystem.entities.Job; import com.ddf.vodsystem.entities.VideoMetadata; import com.ddf.vodsystem.exceptions.FFMPEGException; import com.fasterxml.jackson.databind.JsonNode; @@ -16,7 +15,7 @@ import java.io.InputStreamReader; @Service public class MetadataService { - private static Logger logger = LoggerFactory.getLogger(MetadataService.class); + private static final Logger logger = LoggerFactory.getLogger(MetadataService.class); public VideoMetadata getVideoMetadata(File file) { logger.info("Getting metadata for file {}", file.getAbsolutePath()); @@ -44,6 +43,26 @@ public class MetadataService { } } + public Float getFileSize(File file) { + logger.info("Getting file size for {}", file.getAbsolutePath()); + VideoMetadata metadata = getVideoMetadata(file); + + if (metadata.getFileSize() == null) { + throw new FFMPEGException("File size not found"); + } + + return metadata.getFileSize(); + } + + public Float getVideoDuration(File file) { + logger.info("Getting video duration for {}", file.getAbsolutePath()); + VideoMetadata metadata = getVideoMetadata(file); + if (metadata.getEndPoint() == null) { + throw new FFMPEGException("Video duration not found"); + } + return metadata.getEndPoint(); + } + private JsonNode readStandardOutput(Process process) throws IOException{ // Read the standard output (JSON metadata) BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));