Rework to avoid using preferences object to retrieve options
This commit is contained in:
@@ -15,5 +15,9 @@
|
||||
<groupId>com.github.gtache.autosubtitle</groupId>
|
||||
<artifactId>autosubtitle-whisper-common</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.github.gtache.autosubtitle.modules.subtitle.parser.json.whisper.base;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.github.gtache.autosubtitle.subtitle.converter.SubtitleConverter;
|
||||
import com.github.gtache.autosubtitle.subtitle.parser.json.whisper.base.JSONSubtitleConverter;
|
||||
import com.google.gson.Gson;
|
||||
import dagger.Binds;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
@@ -25,7 +25,7 @@ public abstract class WhisperJsonModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
static Gson providesGson() {
|
||||
return new Gson();
|
||||
static ObjectMapper providesObjectMapper() {
|
||||
return new ObjectMapper();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.github.gtache.autosubtitle.impl.OS;
|
||||
import com.github.gtache.autosubtitle.modules.setup.whisper.WhisperVenvPath;
|
||||
import com.github.gtache.autosubtitle.process.ProcessRunner;
|
||||
import com.github.gtache.autosubtitle.subtitle.converter.SubtitleConverterProvider;
|
||||
import com.github.gtache.autosubtitle.subtitle.extractor.ExtractionModel;
|
||||
import com.github.gtache.autosubtitle.subtitle.extractor.ExtractOptions;
|
||||
import com.github.gtache.autosubtitle.subtitle.extractor.SubtitleExtractor;
|
||||
import com.github.gtache.autosubtitle.subtitle.extractor.whisper.AbstractWhisperSubtitleExtractor;
|
||||
import com.github.gtache.autosubtitle.whisper.WhisperModels;
|
||||
@@ -30,7 +30,9 @@ public class WhisperSubtitleExtractor extends AbstractWhisperSubtitleExtractor {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<String> createArgs(final Path path, final Language language, final ExtractionModel model, final Path outputDir) {
|
||||
protected List<String> createArgs(final Path path, final ExtractOptions options, final Path outputDir) {
|
||||
final var model = options.model();
|
||||
final var language = options.language();
|
||||
final var args = new ArrayList<String>(14);
|
||||
args.add(getPythonPath().toString());
|
||||
args.add("-m");
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
package com.github.gtache.autosubtitle.subtitle.parser.json.whisper.base;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.github.gtache.autosubtitle.Language;
|
||||
import com.github.gtache.autosubtitle.VideoInfo;
|
||||
import com.github.gtache.autosubtitle.subtitle.Subtitle;
|
||||
import com.github.gtache.autosubtitle.subtitle.SubtitleCollection;
|
||||
import com.github.gtache.autosubtitle.subtitle.converter.FormatException;
|
||||
import com.github.gtache.autosubtitle.subtitle.converter.FormatOptions;
|
||||
import com.github.gtache.autosubtitle.subtitle.converter.ParseException;
|
||||
import com.github.gtache.autosubtitle.subtitle.converter.ParseOptions;
|
||||
import com.github.gtache.autosubtitle.subtitle.converter.SubtitleConverter;
|
||||
import com.github.gtache.autosubtitle.subtitle.impl.SubtitleCollectionImpl;
|
||||
import com.github.gtache.autosubtitle.subtitle.impl.SubtitleImpl;
|
||||
import com.google.gson.Gson;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@@ -28,26 +31,30 @@ import java.util.stream.Collectors;
|
||||
public class JSONSubtitleConverter implements SubtitleConverter<SubtitleImpl> {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(JSONSubtitleConverter.class);
|
||||
private final Gson gson;
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
@Inject
|
||||
JSONSubtitleConverter(final Gson gson) {
|
||||
this.gson = Objects.requireNonNull(gson);
|
||||
JSONSubtitleConverter(final ObjectMapper mapper) {
|
||||
this.mapper = Objects.requireNonNull(mapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String format(final SubtitleCollection<?> collection, final VideoInfo videoInfo) {
|
||||
public String format(final SubtitleCollection<?> collection, final FormatOptions options) throws FormatException {
|
||||
final var id = new AtomicInteger(0);
|
||||
final var segments = collection.subtitles().stream().map(s -> new JSONSubtitleSegment(id.incrementAndGet(), 0, s.start() / (double) 1000,
|
||||
s.end() / (double) 1000, s.content(), List.of(), 0, 0, 0, 0)).toList();
|
||||
final var subtitles = new JSONSubtitles(collection.text(), segments, collection.language().iso2());
|
||||
return gson.toJson(subtitles);
|
||||
try {
|
||||
return mapper.writeValueAsString(subtitles);
|
||||
} catch (final JsonProcessingException e) {
|
||||
throw new FormatException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SubtitleCollectionImpl<SubtitleImpl> parse(final String content) throws ParseException {
|
||||
public SubtitleCollectionImpl<SubtitleImpl> parse(final String content, final ParseOptions options) throws ParseException {
|
||||
try {
|
||||
final var json = gson.fromJson(content, JSONSubtitles.class);
|
||||
final var json = mapper.readValue(content, JSONSubtitles.class);
|
||||
final var subtitles = json.segments().stream().map(s -> {
|
||||
final var start = (long) (s.start() * 1000L);
|
||||
final var end = (long) (s.end() * 1000L);
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
*/
|
||||
module com.github.gtache.autosubtitle.whisper.base {
|
||||
requires transitive com.github.gtache.autosubtitle.whisper.common;
|
||||
requires com.fasterxml.jackson.databind;
|
||||
requires com.github.gtache.autosubtitle.core;
|
||||
requires org.apache.logging.log4j;
|
||||
requires com.google.gson;
|
||||
|
||||
exports com.github.gtache.autosubtitle.setup.whisper.base;
|
||||
exports com.github.gtache.autosubtitle.subtitle.extractor.whisper.base;
|
||||
|
||||
Reference in New Issue
Block a user