Skip to content

Commit

Permalink
Merge pull request #38 from mdaubie/feat/35/shift-subtitles-tool
Browse files Browse the repository at this point in the history
feat: add the shift subtitles tool
  • Loading branch information
Matthieu Daubié committed May 7, 2023
2 parents 58a36cc + 4c877b5 commit f035dcf
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.mdaubie.subtitlesparser.toolbox;

import io.github.mdaubie.subtitlesparser.model.Subtitle;
import io.github.mdaubie.subtitlesparser.model.SubtitlesFile;

import java.security.InvalidParameterException;

public class ShiftSubtitles {
/**
* Shifts the subtitle timestamps by a given offset
*
* @param sf The SubtitleFile object for which to shift the subtitles
* @param millisOffset The offset, from which the subtitles are shifted, can be negative
* @throws InvalidParameterException if the offset provided is negative and larger than the first timestamp (resulting in a negative timestamp)
*/
static void shift(SubtitlesFile sf, long millisOffset) {
long nanosOffset = millisOffset * 1000000;
if (millisOffset < 0 && sf.getSubtitles().get(0).start.toNanoOfDay() + nanosOffset < 0)
throw new InvalidParameterException("Too large negative offset provided");
for (int i = 0; i < sf.getSubtitles().size() - 1; i++) {
Subtitle first = sf.getSubtitles().get(i);
first.start = first.start.plusNanos(nanosOffset);
first.end = first.end.plusNanos(nanosOffset);
}
}
}
19 changes: 16 additions & 3 deletions src/test/java/io/github/mdaubie/subtitlesparser/TestObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,24 @@ public static class SubRip {
public static final Supplier<SubRipSubtitle> sub3FixedMerge = () -> getSubRipSubtitle(2,
LocalTime.of(0, 0, 56, 473 * 1000000),
LocalTime.of(0, 0, 59, 701 * 1000000),
"This is the second dialogue"+System.lineSeparator()+"This is the third dialogue");
"This is the second dialogue" + System.lineSeparator() + "This is the third dialogue");
public static final Supplier<SubRipSubtitle> sub1Shifted1000 = () -> getSubRipSubtitle(1,
LocalTime.of(0, 0, 52, 93 * 1000000),
LocalTime.of(0, 0, 53, 635 * 1000000),
"This is the first dialogue");
public static final Supplier<SubRipSubtitle> sub2Shifted1000 = () -> getSubRipSubtitle(2,
LocalTime.of(0, 0, 57, 473 * 1000000),
LocalTime.of(0, 0, 59, 266 * 1000000),
"This is the second dialogue");
public static final Supplier<SubRipSubtitle> sub3Shifted1000 = () -> getSubRipSubtitle(3,
LocalTime.of(0, 0, 58, 908 * 1000000),
LocalTime.of(0, 1, 0, 701 * 1000000),
"This is the third dialogue");
public static final Supplier<SubRipFile> object1 = () -> getSubRipFile(sub1.get(), sub2.get(), sub3.get());
public static final Supplier<SubRipFile> object1FixedDelay = ()->getSubRipFile(sub1.get(), sub2.get(), sub3FixedDelay.get());
public static final Supplier<SubRipFile> object1FixedDelay = () -> getSubRipFile(sub1.get(), sub2.get(), sub3FixedDelay.get());
public static final Supplier<SubRipFile> object2 = () -> getSubRipFile(sub2.get(), sub1.get(), sub3.get());
public static final Supplier<SubRipFile> object2FixedMerge = ()->getSubRipFile(sub1.get(), sub3FixedMerge.get());
public static final Supplier<SubRipFile> object2FixedMerge = () -> getSubRipFile(sub1.get(), sub3FixedMerge.get());
public static final Supplier<SubRipFile> object1Shifted1000 = () -> getSubRipFile(sub1Shifted1000.get(), sub2Shifted1000.get(), sub3Shifted1000.get());
public static final String text1 = """
1
00:00:51,093 --> 00:00:52,635
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.mdaubie.subtitlesparser.toolbox;

import io.github.mdaubie.subtitlesparser.TestObjects;
import io.github.mdaubie.subtitlesparser.model.SubtitlesFile;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.security.InvalidParameterException;
import java.util.function.Supplier;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;

class ShiftSubtitlesTest {
@ParameterizedTest
@MethodSource("shift")
void shift(Supplier<SubtitlesFile> sf, long millisOffset, Supplier<SubtitlesFile> expectedResult) {
ShiftSubtitles.shift(sf.get(), millisOffset);
assertThat(sf).usingRecursiveComparison().isEqualTo(expectedResult.get());
}

static Stream<Arguments> shift() {
return Stream.of(
Arguments.of(TestObjects.SubRip.object1, 1000, TestObjects.SubRip.object1Shifted1000)
);
}

@Test()
void shiftThrows() {
assertThrowsExactly(InvalidParameterException.class,
()-> ShiftSubtitles.shift(TestObjects.SubRip.object1.get(),-52000));
}
}

0 comments on commit f035dcf

Please sign in to comment.