java-time - Java 从父时间范围中减去时间范围数组
问题描述
时间范围(代表视频长度)采用这种格式的字符串:HH:mm:ss
. 假设我有一个看起来像这样的父时间范围:00:00:59
表示一个 59 秒长的视频。
我现在有一组时间范围对,我必须从视频中删除,因为在这些时间范围内视频中没有发生任何有趣的事情。例如输入数组:
[
00:00:10 to 00:00:20,
00:00:30 to 00:00:35,
00:00:35 to 00:00:40
]
我希望从 59 秒的视频中删除这些不重要的范围,并计算剩余的有效视频片段。在上述情况下,理想的输出将是:
[
00:00:00 to 00:00:10,
00:00:20 to 00:00:30,
00:00:40 to 00:00:59
]
解决方案
Sort, iterate, and include
- Sort the input intervals on their start time(to be excluded)
- Iterate over each of the input interval to excluded
- Include any interval between end of previous excluded interval and start of current excluded interval
- At the end, check for any remaining interval by comparing with total length
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class SequenceTest {
public static void main(String[] args) {
final SequenceTest sequenceTest = new SequenceTest();
sequenceTest.computeIncludeIntervals(59,
Arrays.asList((new int[][]{{10, 20}, {30, 35}, {35, 40}})));
sequenceTest.computeIncludeIntervals(40,
Arrays.asList((new int[][]{{10, 20}, {30, 35}, {35, 40}})));
sequenceTest.computeIncludeIntervals(41,
Arrays.asList((new int[][]{{10, 20}, {30, 35}, {35, 40}})));
sequenceTest.computeIncludeIntervals(41,
Arrays.asList((new int[][]{{0, 20}, {30, 35}, {36, 40}})));
}
protected List<int[]> computeIncludeIntervals(final int totalLength, List<int[]> excludeIntervals) {
// sort the sequence by start time - O(NlogN)
excludeIntervals.sort((a, b) -> a[0] == b[0] ? Integer.compare(a[1], b[1])
: Integer.compare(a[0], b[0]));
int previousEnd = 0; // initial state
final List<int[]> result = new ArrayList<>();
for (int[] exclude : excludeIntervals) {
if (previousEnd < exclude[0]) {
int[] include = new int[]{previousEnd, exclude[0]};
result.add(include);
}
previousEnd = Math.max(previousEnd, exclude[1]);
}
// remaining tail
if (previousEnd < totalLength) {
result.add(new int[]{previousEnd, totalLength});
}
System.out.println("Total Length: " + totalLength + ", Input: " + excludeIntervals.stream()
.map(interval -> interval[0] + ":" + interval[1])
.collect(Collectors.joining(", ")));
System.out.println("Included: " + result.stream().map(interval -> interval[0] + ":" + interval[1])
.collect(Collectors.joining(", ")));
return result;
}
}
Actual solution
- Write a transformer to transform input date time to epoch unix time
- Use the above approach to compute include intervals
- Transform include intervals in epoch time to date time using reverse transformer
推荐阅读
- android - 安卓。输入过滤器无法正常工作
- pyspark - pyspark sql sum vs aggr
- python - 从重复元素列表中创建非重复元素列表。仅使用列表理解
- arrays - 嵌套对象,数组组合
- java - 为什么在存在 HTML5 验证时在 Spring MVC 中使用 @Valid
- ffmpeg - 如何使用 FFmpeg 获取视频前 x 分钟的大小?
- python - Python:使用由类组成的列表时,for循环和输入失败
- python - 使用 Python SSH 实用程序执行 OpenShift 命令失败
- grails - Rundeck SchedulerException:调度程序已关闭
- javascript - 如何通过 Web 共享 API 共享单个 Base64 URL 图像?