From e8fccb327a97fffc4cc683cb62bffa061dd9dd7c Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Fri, 27 Dec 2019 14:21:09 +0100 Subject: [PATCH] Determine the movie's length from the MP4 file Use the time spent recording the stream as a fallback --- .../recorder/download/dash/DashDownload.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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