From b2d1d41abc6c14f445f831178c421b15dfebf65d Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Fri, 14 Dec 2018 23:53:10 +0100 Subject: [PATCH] Remove ordering by sequence This was used for Chaturbate, because the filename format was known. With several camsites the filename format can differ and this is not a good solution anymore. Instead we now just sort filename. To make sure, the files have the right order, HlsDownload now creates a prefix for each segment. --- .../java/ctbrec/recorder/PlaylistGenerator.java | 16 +--------------- .../ctbrec/recorder/download/HlsDownload.java | 15 +++++++++++---- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/common/src/main/java/ctbrec/recorder/PlaylistGenerator.java b/common/src/main/java/ctbrec/recorder/PlaylistGenerator.java index a4180765..1c02a614 100644 --- a/common/src/main/java/ctbrec/recorder/PlaylistGenerator.java +++ b/common/src/main/java/ctbrec/recorder/PlaylistGenerator.java @@ -9,8 +9,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,10 +46,8 @@ public class PlaylistGenerator { Arrays.sort(files, (f1, f2) -> { String n1 = f1.getName(); - int seq1 = getSequence(n1); String n2 = f2.getName(); - int seq2 = getSequence(n2); - return seq1 - seq2; + return n1.compareTo(n2); }); // create a track containing all files @@ -102,16 +98,6 @@ public class PlaylistGenerator { return output; } - private int getSequence(String filename) { - filename = filename.substring(0, filename.lastIndexOf('.')); // cut off file suffix - Matcher matcher = Pattern.compile(".*?(\\d+)").matcher(filename); - if(matcher.matches()) { - return Integer.parseInt(matcher.group(1)); - } else { - return -1; - } - } - private void updateProgressListeners(double percentage) { int p = (int) (percentage*100); if(p > lastPercentage) { diff --git a/common/src/main/java/ctbrec/recorder/download/HlsDownload.java b/common/src/main/java/ctbrec/recorder/download/HlsDownload.java index 94aa5a4d..b99f3be7 100644 --- a/common/src/main/java/ctbrec/recorder/download/HlsDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/HlsDownload.java @@ -13,6 +13,8 @@ import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; +import java.text.DecimalFormat; +import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.time.Instant; import java.util.Date; @@ -39,6 +41,9 @@ public class HlsDownload extends AbstractHlsDownload { protected Path downloadDir; + private int segmentCounter = 1; + private NumberFormat nf = new DecimalFormat("000000"); + public HlsDownload(HttpClient client) { super(client); } @@ -78,7 +83,8 @@ public class HlsDownload extends AbstractHlsDownload { for (int i = nextSegment; i < lsp.seq; i++) { URL segmentUrl = new URL(first.replaceAll(Integer.toString(seq), Integer.toString(i))); LOG.debug("Reloading segment {} for model {}", i, model.getName()); - downloadThreadPool.submit(new SegmentDownload(segmentUrl, downloadDir, client)); + String prefix = nf.format(segmentCounter++); + downloadThreadPool.submit(new SegmentDownload(segmentUrl, downloadDir, client, prefix)); } // TODO switch to a lower bitrate/resolution ?!? } @@ -88,7 +94,8 @@ public class HlsDownload extends AbstractHlsDownload { skip--; } else { URL segmentUrl = new URL(segment); - downloadThreadPool.submit(new SegmentDownload(segmentUrl, downloadDir, client)); + String prefix = nf.format(segmentCounter++); + downloadThreadPool.submit(new SegmentDownload(segmentUrl, downloadDir, client, prefix)); //new SegmentDownload(segment, downloadDir).call(); } } @@ -150,11 +157,11 @@ public class HlsDownload extends AbstractHlsDownload { private Path file; private HttpClient client; - public SegmentDownload(URL url, Path dir, HttpClient client) { + public SegmentDownload(URL url, Path dir, HttpClient client, String prefix) { this.url = url; this.client = client; File path = new File(url.getPath()); - file = FileSystems.getDefault().getPath(dir.toString(), path.getName()); + file = FileSystems.getDefault().getPath(dir.toString(), prefix + '_' + path.getName()); } @Override