diff --git a/common/src/main/java/ctbrec/recorder/download/dash/DashDownload.java b/common/src/main/java/ctbrec/recorder/download/dash/DashDownload.java index a9055d33..d5e72046 100644 --- a/common/src/main/java/ctbrec/recorder/download/dash/DashDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/dash/DashDownload.java @@ -26,6 +26,8 @@ import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +import org.jcodec.containers.mp4.MP4Util; +import org.jcodec.containers.mp4.boxes.MovieBox; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,6 +65,7 @@ public class DashDownload extends AbstractDownload { private ZonedDateTime splitRecStartTime; private File targetFile; + private File finalFile; public DashDownload(HttpClient httpClient, String manifestUrl) { this.httpClient = httpClient; @@ -225,7 +228,7 @@ public class DashDownload extends AbstractDownload { this.config = config; this.model = model; this.startTime = startTime; - File finalFile = Config.getInstance().getFileForRecording(model, "mp4", startTime); + finalFile = Config.getInstance().getFileForRecording(model, "mp4", startTime); targetFile = new File(finalFile.getParentFile(), finalFile.getName() + ".part"); downloadDir = targetFile.toPath(); } @@ -363,7 +366,14 @@ public class DashDownload extends AbstractDownload { @Override public Duration getLength() { - return Duration.between(startTime, Optional.ofNullable(endTime).orElse(Instant.now())); + try { + MovieBox movieBox = MP4Util.parseMovie(finalFile); + double lengthInSeconds = (double) movieBox.getDuration() / movieBox.getTimescale(); + return Duration.ofSeconds((long) Math.ceil(lengthInSeconds)); + } catch (IOException e) { + LOG.error("Couldn't determine length of MP4 file {}", getTarget(), e); + return Duration.between(startTime, Optional.ofNullable(endTime).orElse(Instant.now())); + } } @Override