Initial commit

This commit is contained in:
Guillaume Tâche
2024-07-27 17:45:46 +02:00
commit 75829244b9
66 changed files with 2906 additions and 0 deletions

29
core/pom.xml Normal file
View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.gtache.autosubtitle</groupId>
<artifactId>autosubtitle</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>autosubtitle-core</artifactId>
<dependencies>
<dependency>
<groupId>com.github.gtache.autosubtitle</groupId>
<artifactId>autosubtitle-api</artifactId>
</dependency>
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.23.1</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,20 @@
package com.github.gtache.autosubtitle.impl;
import com.github.gtache.autosubtitle.AudioInfo;
import java.util.Objects;
/**
* Implementation of {@link AudioInfo}
*/
public record AudioInfoImpl(String audioFormat) implements AudioInfo {
public AudioInfoImpl {
Objects.requireNonNull(audioFormat);
}
@Override
public String videoFormat() {
return audioFormat;
}
}

View File

@@ -0,0 +1,27 @@
package com.github.gtache.autosubtitle.impl;
import com.github.gtache.autosubtitle.Audio;
import com.github.gtache.autosubtitle.AudioInfo;
import com.github.gtache.autosubtitle.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import static java.util.Objects.requireNonNull;
/**
* Implementation of {@link Audio} with a {@link File}
*/
public record FileAudioImpl(Path path, AudioInfo info) implements Audio, File {
public FileAudioImpl {
requireNonNull(path);
requireNonNull(info);
}
@Override
public InputStream getInputStream() throws IOException {
return File.super.getInputStream();
}
}

View File

@@ -0,0 +1,27 @@
package com.github.gtache.autosubtitle.impl;
import com.github.gtache.autosubtitle.File;
import com.github.gtache.autosubtitle.Video;
import com.github.gtache.autosubtitle.VideoInfo;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import static java.util.Objects.requireNonNull;
/**
* Implementation of {@link Video} with a {@link File}
*/
public record FileVideoImpl(Path path, VideoInfo info) implements Video, File {
public FileVideoImpl {
requireNonNull(path);
requireNonNull(info);
}
@Override
public InputStream getInputStream() throws IOException {
return File.super.getInputStream();
}
}

View File

@@ -0,0 +1,25 @@
package com.github.gtache.autosubtitle.impl;
import com.github.gtache.autosubtitle.Audio;
import com.github.gtache.autosubtitle.AudioInfo;
import java.io.InputStream;
import java.util.function.Supplier;
import static java.util.Objects.requireNonNull;
/**
* In-memory implementation of {@link Audio}
*/
public record MemoryAudioImpl(Supplier<InputStream> inputStreamSupplier, AudioInfo info) implements Audio {
public MemoryAudioImpl {
requireNonNull(inputStreamSupplier);
requireNonNull(info);
}
@Override
public InputStream getInputStream() {
return inputStreamSupplier.get();
}
}

View File

@@ -0,0 +1,25 @@
package com.github.gtache.autosubtitle.impl;
import com.github.gtache.autosubtitle.Video;
import com.github.gtache.autosubtitle.VideoInfo;
import java.io.InputStream;
import java.util.function.Supplier;
import static java.util.Objects.requireNonNull;
/**
* In-memory implementation of {@link Video}
*/
public record MemoryVideoImpl(Supplier<InputStream> inputStreamSupplier, VideoInfo info) implements Video {
public MemoryVideoImpl {
requireNonNull(inputStreamSupplier);
requireNonNull(info);
}
@Override
public InputStream getInputStream() {
return inputStreamSupplier.get();
}
}

View File

@@ -0,0 +1,20 @@
package com.github.gtache.autosubtitle.impl;
import com.github.gtache.autosubtitle.VideoInfo;
import java.util.Objects;
/**
* Implementation of {@link VideoInfo}
*/
public record VideoInfoImpl(String videoFormat) implements VideoInfo {
public VideoInfoImpl {
Objects.requireNonNull(videoFormat);
}
@Override
public String videoFormat() {
return videoFormat;
}
}

View File

@@ -0,0 +1,48 @@
package com.github.gtache.autosubtitle.process.impl;
import com.github.gtache.autosubtitle.process.ProcessResult;
import com.github.gtache.autosubtitle.process.ProcessRunner;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Base implementation of {@link ProcessRunner}
*/
public abstract class AbstractProcessRunner implements ProcessRunner {
private static final Logger logger = LogManager.getLogger(AbstractProcessRunner.class);
@Override
public ProcessResult run(final List<String> args) throws IOException {
final var builder = new ProcessBuilder(args);
builder.inheritIO();
builder.redirectErrorStream(true);
final var process = builder.start();
final var output = new ArrayList<String>();
new Thread(() -> {
try (final var in = new BufferedReader(new InputStreamReader(new BufferedInputStream(process.getInputStream()), StandardCharsets.UTF_8))) {
while (in.ready()) {
output.add(in.readLine());
}
} catch (final IOException e) {
logger.error("Error listening to process output of {}", args, e);
}
}).start();
try {
process.waitFor(1, TimeUnit.HOURS);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
process.destroy();
}
return new ProcessResultImpl(process.exitValue(), output);
}
}

View File

@@ -0,0 +1,15 @@
package com.github.gtache.autosubtitle.process.impl;
import com.github.gtache.autosubtitle.process.ProcessResult;
import java.util.List;
/**
* Implementation of {@link ProcessResult}
*/
public record ProcessResultImpl(int exitCode, List<String> output) implements ProcessResult {
public ProcessResultImpl {
output = List.copyOf(output);
}
}

View File

@@ -0,0 +1,16 @@
package com.github.gtache.autosubtitle.setup.modules.impl;
import javax.inject.Qualifier;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Qualifier
@Documented
@Retention(RUNTIME)
@Target({ElementType.TYPE_USE, ElementType.METHOD, ElementType.FIELD})
public @interface SubtitleExtractor {
}

View File

@@ -0,0 +1,16 @@
package com.github.gtache.autosubtitle.setup.modules.impl;
import javax.inject.Qualifier;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Qualifier
@Documented
@Retention(RUNTIME)
@Target({ElementType.TYPE_USE, ElementType.METHOD, ElementType.FIELD})
public @interface Translator {
}

View File

@@ -0,0 +1,16 @@
package com.github.gtache.autosubtitle.setup.modules.impl;
import javax.inject.Qualifier;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Qualifier
@Documented
@Retention(RUNTIME)
@Target({ElementType.TYPE_USE, ElementType.METHOD, ElementType.FIELD})
public @interface VideoConverter {
}

View File

@@ -0,0 +1,24 @@
package com.github.gtache.autosubtitle.subtitle.impl;
import com.github.gtache.autosubtitle.subtitle.Bounds;
/**
* Implementation of {@link Bounds}
*/
public record BoundsImpl(double x, double y, double width, double height) implements Bounds {
public BoundsImpl {
if (x < 0) {
throw new IllegalArgumentException("x must be >= 0 : " + x);
}
if (y < 0) {
throw new IllegalArgumentException("y must be >= 0 : " + y);
}
if (width < 0) {
throw new IllegalArgumentException("width must be >= 0 : " + width);
}
if (height < 0) {
throw new IllegalArgumentException("height must be >= 0 : " + height);
}
}
}

View File

@@ -0,0 +1,20 @@
package com.github.gtache.autosubtitle.subtitle.impl;
import com.github.gtache.autosubtitle.subtitle.Font;
import java.util.Objects;
/**
* Implementation of {@link Font}
* @param name
* @param size
*/
public record FontImpl(String name, int size) implements Font {
public FontImpl {
Objects.requireNonNull(name);
if (size <= 0) {
throw new IllegalArgumentException("Size must be greater than 0 : " + size);
}
}
}

View File

@@ -0,0 +1,19 @@
package com.github.gtache.autosubtitle.subtitle.impl;
import com.github.gtache.autosubtitle.subtitle.SubtitleCollection;
import com.github.gtache.autosubtitle.subtitle.SubtitleConverter;
/**
* Converts subtitles to SRT format
*/
public class SRTSubtitleConverter implements SubtitleConverter {
public String convert(final SubtitleCollection collection) {
}
@Override
public String formatName() {
return "srt";
}
}

View File

@@ -0,0 +1,21 @@
package com.github.gtache.autosubtitle.subtitle.impl;
import com.github.gtache.autosubtitle.subtitle.Subtitle;
import com.github.gtache.autosubtitle.subtitle.SubtitleCollection;
import java.util.Collection;
import java.util.List;
import static java.util.Objects.requireNonNull;
/**
* Implementation of {@link SubtitleCollection}
*/
public record SubtitleCollectionImpl(Collection<? extends Subtitle> subtitles,
String language) implements SubtitleCollection {
public SubtitleCollectionImpl {
subtitles = List.copyOf(subtitles);
requireNonNull(language);
}
}