-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from mdaubie/feat/35/shift-subtitles-tool
feat: add the shift subtitles tool
- Loading branch information
Showing
3 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/io/github/mdaubie/subtitlesparser/toolbox/ShiftSubtitles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/test/java/io/github/mdaubie/subtitlesparser/toolbox/ShiftSubtitlesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |