diff --git a/common/src/main/java/ctbrec/recorder/download/hls/MergedHlsDownload.java b/common/src/main/java/ctbrec/recorder/download/hls/MergedHlsDownload.java index 8257d60b..773c8089 100644 --- a/common/src/main/java/ctbrec/recorder/download/hls/MergedHlsDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/hls/MergedHlsDownload.java @@ -8,8 +8,11 @@ import java.net.URL; import java.nio.file.Files; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; +import java.time.Duration; import java.util.Arrays; +import org.jcodec.containers.mp4.MP4Util; +import org.jcodec.containers.mp4.boxes.MovieBox; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -151,4 +154,16 @@ public class MergedHlsDownload extends HlsDownload { response.close(); } } + + @Override + public Duration getLength() { + 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.ofSeconds(0); + } + } }