Refactored some code from the Transcoder class to a separate Repository class
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
b049581632
commit
a977ff8cfe
@ -14,13 +14,16 @@ public class App {
|
||||
|
||||
private static final String configFile = "~/.config/dragoon.toml";
|
||||
|
||||
private static final int msToMinutes = 60 * 1000;
|
||||
|
||||
public static void main(String[] args) {
|
||||
// read our config file
|
||||
Config c = new Config(configFile);
|
||||
// create new Transcoder object and start the service
|
||||
Transcoder t = new Transcoder(c);
|
||||
Timer timer = new Timer();
|
||||
timer.scheduleAtFixedRate(new RunTranscoderTask(t), 2500, 120 * 1000);
|
||||
timer.scheduleAtFixedRate(new RunTranscoderTask(t), 2500, ((long) c.getInt("transcoder.interval") * msToMinutes));
|
||||
System.out.printf("Starting transcoder, running in %d minute intervals.%n", c.getInt("transcoder.interval"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ public class Config {
|
||||
}
|
||||
|
||||
public int getInt(String key) {
|
||||
return Objects.requireNonNull(this.result.getDouble(key)).intValue();
|
||||
return Objects.requireNonNull(this.result.getLong(key)).intValue();
|
||||
}
|
||||
|
||||
public boolean contains(String key) {
|
||||
|
79
src/main/java/tech/bitgoblin/transcoder/Repository.java
Normal file
79
src/main/java/tech/bitgoblin/transcoder/Repository.java
Normal file
@ -0,0 +1,79 @@
|
||||
package tech.bitgoblin.transcoder;
|
||||
|
||||
import tech.bitgoblin.io.IOUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
public class Repository {
|
||||
|
||||
private String repoPath;
|
||||
private String ingestPath;
|
||||
private String outputPath;
|
||||
private String archivePath;
|
||||
|
||||
public Repository(String repoPath) {
|
||||
this.repoPath = IOUtils.resolveTilda(repoPath);
|
||||
this.ingestPath = Paths.get(this.repoPath, "ingest").toString();
|
||||
this.outputPath = Paths.get(this.repoPath, "output").toString();
|
||||
this.archivePath = Paths.get(this.repoPath, "archive").toString();
|
||||
}
|
||||
|
||||
// initializes the video repository
|
||||
public void init() {
|
||||
String[] dirs = {this.repoPath, this.ingestPath, this.outputPath, this.archivePath};
|
||||
for (String p : dirs) {
|
||||
IOUtils.createDirectory(p);
|
||||
}
|
||||
}
|
||||
|
||||
// searches this ingest directory for video files
|
||||
public File[] searchIngest() {
|
||||
System.out.println("Searching for files to transcode in " + this.ingestPath);
|
||||
File repo = new File(this.ingestPath);
|
||||
return repo.listFiles();
|
||||
}
|
||||
|
||||
// archives files in the ingest directory
|
||||
public void archiveIngest() {
|
||||
File repo = new File(this.archivePath);
|
||||
File[] sourceFiles = repo.listFiles();
|
||||
|
||||
for (File f : sourceFiles) {
|
||||
Path filePath = Path.of(f.toString());
|
||||
String filename = filePath.getFileName().toString();
|
||||
String archivePath = Paths.get(this.archivePath, 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 cleanupIngest() {
|
||||
File repo = new File(this.ingestPath);
|
||||
File[] sourceFiles = repo.listFiles();
|
||||
|
||||
for (File f : sourceFiles) {
|
||||
f.delete();
|
||||
}
|
||||
}
|
||||
|
||||
// returns the repository's path
|
||||
public String getPath() {
|
||||
return this.repoPath;
|
||||
}
|
||||
|
||||
// return the repository's output path
|
||||
public String getOutputPath() {
|
||||
return this.outputPath;
|
||||
}
|
||||
|
||||
}
|
@ -13,11 +13,7 @@ public class RunTranscoderTask extends TimerTask {
|
||||
@Override
|
||||
public void run() {
|
||||
// archive the files
|
||||
transcoder.archive();
|
||||
// run the transcoder
|
||||
transcoder.transcode();
|
||||
// clean up ingest
|
||||
transcoder.cleanup();
|
||||
transcoder.run();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,44 +15,47 @@ import tech.bitgoblin.io.IOUtils;
|
||||
|
||||
public class Transcoder {
|
||||
|
||||
private String repo_dir;
|
||||
private Repository repo;
|
||||
private Config config;
|
||||
private String ffmpeg_path = "/usr/bin/ffmpeg";
|
||||
|
||||
// only define the working directory; use default FFMPEG path
|
||||
public Transcoder(Config config) {
|
||||
this.config = config;
|
||||
this.repo_dir = IOUtils.resolveTilda(this.config.getString("transcoder.repo_path"));
|
||||
this.repo = new Repository(this.config.getString("transcoder.repo_path"));
|
||||
if (this.config.contains("transcoder.ffmpeg_path")) {
|
||||
this.ffmpeg_path = this.config.getString("transcoder.ffmpeg_path");
|
||||
}
|
||||
this.initDirectory();
|
||||
this.repo.init();
|
||||
}
|
||||
|
||||
// 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);
|
||||
public void run() {
|
||||
// save source video files
|
||||
this.repo.archiveIngest();
|
||||
// transcode video files to new format
|
||||
this.transcode();
|
||||
// cleanup old video files in ingest
|
||||
this.repo.cleanupIngest();
|
||||
}
|
||||
|
||||
// transcode files in the working directory
|
||||
public void transcode() {
|
||||
// search for files
|
||||
System.out.println("Searching for files to transcode in " + this.repo_dir);
|
||||
File repo = new File(Paths.get(this.repo_dir, "ingest").toString());
|
||||
File[] sourceFiles = repo.listFiles();
|
||||
File[] sourceFiles = this.repo.searchIngest();
|
||||
|
||||
// check if the ingest directory is empty
|
||||
if (sourceFiles.length == 0) {
|
||||
System.out.println("There is nothing to transcode in " + this.repo_dir + "/ingest.");
|
||||
System.out.println("There is nothing to transcode in ingest.");
|
||||
return;
|
||||
}
|
||||
|
||||
// transcode
|
||||
System.out.println("Transcoding files in " + this.repo_dir + "/ingest...");
|
||||
System.out.println("Transcoding video files ingest...");
|
||||
for (File f : sourceFiles) {
|
||||
String filePath = f.toString().substring(0, f.toString().lastIndexOf("."));
|
||||
String filename = Paths.get(filePath).getFileName().toString();
|
||||
String outputPath = Paths.get(this.repo_dir, "output", String.format("%s.mov", filename)).toString();
|
||||
String outputPath = Paths.get(this.repo.getOutputPath(), String.format("%s.mov", filename)).toString();
|
||||
|
||||
String cmd = String.format("%s -i %s -y -f %s -c:v %s -vf %s -profile:v %s -c:a %s %s",
|
||||
this.ffmpeg_path, // FFMPEG binary path
|
||||
@ -71,7 +74,6 @@ public class Transcoder {
|
||||
Process process = pb.start();
|
||||
int ret = process.waitFor();
|
||||
System.out.printf("Program exited with code: %d\n", ret);
|
||||
System.out.println(String.join(" ", pb.command()));
|
||||
System.out.println();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
@ -83,42 +85,4 @@ public class Transcoder {
|
||||
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
|
||||
private void initDirectory() {
|
||||
// create transcoder directory
|
||||
IOUtils.createDirectory(this.repo_dir);
|
||||
// create the sub-directories
|
||||
IOUtils.createDirectory(Paths.get(this.repo_dir, "ingest").toString());
|
||||
IOUtils.createDirectory(Paths.get(this.repo_dir, "archive").toString());
|
||||
IOUtils.createDirectory(Paths.get(this.repo_dir, "output").toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user