Made the transcoder run as a periodic task, with the interval defined in the config TOML file
This commit is contained in:
parent
347211d566
commit
5bde5bfee0
@ -1,7 +1,10 @@
|
|||||||
package tech.bitgoblin;
|
package tech.bitgoblin;
|
||||||
|
|
||||||
import tech.bitgoblin.config.Config;
|
import tech.bitgoblin.config.Config;
|
||||||
import tech.bitgoblin.video.Transcoder;
|
import tech.bitgoblin.transcoder.RunTranscoderTask;
|
||||||
|
import tech.bitgoblin.transcoder.Transcoder;
|
||||||
|
|
||||||
|
import java.util.Timer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Bit Goblin video transcoder service.
|
* The Bit Goblin video transcoder service.
|
||||||
@ -16,7 +19,8 @@ public class App {
|
|||||||
Config c = new Config(configFile);
|
Config c = new Config(configFile);
|
||||||
// create new Transcoder object and start the service
|
// create new Transcoder object and start the service
|
||||||
Transcoder t = new Transcoder(c);
|
Transcoder t = new Transcoder(c);
|
||||||
t.transcode();
|
Timer timer = new Timer();
|
||||||
|
timer.scheduleAtFixedRate(new RunTranscoderTask(t), 2500, 120 * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import org.tomlj.Toml;
|
import org.tomlj.Toml;
|
||||||
import org.tomlj.TomlParseResult;
|
import org.tomlj.TomlParseResult;
|
||||||
import tech.bitgoblin.io.IOUtils;
|
import tech.bitgoblin.io.IOUtils;
|
||||||
@ -28,6 +30,10 @@ public class Config {
|
|||||||
return this.result.getString(key);
|
return this.result.getString(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getInt(String key) {
|
||||||
|
return Objects.requireNonNull(this.result.getDouble(key)).intValue();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean contains(String key) {
|
public boolean contains(String key) {
|
||||||
return this.result.contains(key);
|
return this.result.contains(key);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
package tech.bitgoblin.transcoder;
|
||||||
|
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
public class RunTranscoderTask extends TimerTask {
|
||||||
|
|
||||||
|
private Transcoder transcoder;
|
||||||
|
|
||||||
|
public RunTranscoderTask(Transcoder t) {
|
||||||
|
this.transcoder = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// archive the files
|
||||||
|
transcoder.archive();
|
||||||
|
// run the transcoder
|
||||||
|
transcoder.transcode();
|
||||||
|
// clean up ingest
|
||||||
|
transcoder.cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,16 +1,14 @@
|
|||||||
package tech.bitgoblin.video;
|
package tech.bitgoblin.transcoder;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.InterruptedException;
|
import java.lang.InterruptedException;
|
||||||
import java.lang.Process;
|
import java.lang.Process;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.function.Consumer;
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.Timer;
|
||||||
|
|
||||||
import tech.bitgoblin.config.Config;
|
import tech.bitgoblin.config.Config;
|
||||||
import tech.bitgoblin.io.IOUtils;
|
import tech.bitgoblin.io.IOUtils;
|
||||||
@ -31,6 +29,12 @@ public class Transcoder {
|
|||||||
this.initDirectory();
|
this.initDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create a periodic timer task and start it
|
||||||
|
public void start() {
|
||||||
|
Timer timer = new Timer();
|
||||||
|
timer.scheduleAtFixedRate(new RunTranscoderTask(this), 10000, this.config.getInt("transcoder.interval") * 60 * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
// transcode files in the working directory
|
// transcode files in the working directory
|
||||||
public void transcode() {
|
public void transcode() {
|
||||||
// search for files
|
// search for files
|
||||||
@ -38,6 +42,11 @@ public class Transcoder {
|
|||||||
File repo = new File(Paths.get(this.repo_dir, "ingest").toString());
|
File repo = new File(Paths.get(this.repo_dir, "ingest").toString());
|
||||||
File[] sourceFiles = repo.listFiles();
|
File[] sourceFiles = repo.listFiles();
|
||||||
|
|
||||||
|
if (sourceFiles.length == 0) {
|
||||||
|
System.out.println("There is nothing to transcode in " + this.repo_dir + "/ingest.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// transcode
|
// transcode
|
||||||
System.out.println("Transcoding files in " + this.repo_dir + "/ingest...");
|
System.out.println("Transcoding files in " + this.repo_dir + "/ingest...");
|
||||||
for (File f : sourceFiles) {
|
for (File f : sourceFiles) {
|
||||||
@ -74,6 +83,34 @@ public class Transcoder {
|
|||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copies sources files to the archive directory
|
||||||
|
public void archive() {
|
||||||
|
File repo = new File(Paths.get(this.repo_dir, "ingest").toString());
|
||||||
|
File[] sourceFiles = repo.listFiles();
|
||||||
|
|
||||||
|
for (File f : sourceFiles) {
|
||||||
|
Path filePath = Path.of(f.toString());
|
||||||
|
String filename = filePath.getFileName().toString();
|
||||||
|
String archivePath = Paths.get(this.repo_dir, "archive", filename).toString();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Files.copy(filePath, Paths.get(archivePath), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// clean up the ingest directory once we're done
|
||||||
|
public void cleanup() {
|
||||||
|
File repo = new File(Paths.get(this.repo_dir, "ingest").toString());
|
||||||
|
File[] sourceFiles = repo.listFiles();
|
||||||
|
|
||||||
|
for (File f : sourceFiles) {
|
||||||
|
f.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ensures the transcoder's working directory is available
|
// ensures the transcoder's working directory is available
|
||||||
private void initDirectory() {
|
private void initDirectory() {
|
||||||
// create transcoder directory
|
// create transcoder directory
|
Loading…
Reference in New Issue
Block a user