Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
6bce649458 | |||
d017cb19f3 | |||
5bc2acac1d | |||
fe29664a83 | |||
5e8da26257 | |||
763c27ca95 | |||
a8302c38e0 | |||
a977ff8cfe |
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@
|
||||
.settings/
|
||||
target/
|
||||
.idea/
|
||||
logs/
|
||||
|
@ -2,6 +2,7 @@ pipeline:
|
||||
test:
|
||||
image: maven:3-jdk-11
|
||||
commands:
|
||||
- apt update && apt install -y lsof
|
||||
- mvn test
|
||||
|
||||
build:
|
||||
|
12
pom.xml
12
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>tech.bitgoblin</groupId>
|
||||
<artifactId>Dragoon</artifactId>
|
||||
<version>0.2.0</version>
|
||||
<version>0.3.1</version>
|
||||
|
||||
<name>Dragoon</name>
|
||||
<url>https://www.bitgoblin.tech</url>
|
||||
@ -23,6 +23,16 @@
|
||||
<artifactId>tomlj</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<version>2.17.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.17.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
@ -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));
|
||||
Logger.logger.info(String.format("Starting transcoder, running in %d minute intervals.", c.getInt("transcoder.interval")));
|
||||
}
|
||||
|
||||
}
|
||||
|
9
src/main/java/tech/bitgoblin/Logger.java
Normal file
9
src/main/java/tech/bitgoblin/Logger.java
Normal file
@ -0,0 +1,9 @@
|
||||
package tech.bitgoblin;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
||||
public class Logger {
|
||||
|
||||
public static org.apache.logging.log4j.Logger logger = LogManager.getLogger();
|
||||
|
||||
}
|
@ -8,6 +8,7 @@ import java.util.Objects;
|
||||
|
||||
import org.tomlj.Toml;
|
||||
import org.tomlj.TomlParseResult;
|
||||
import tech.bitgoblin.Logger;
|
||||
import tech.bitgoblin.io.IOUtils;
|
||||
|
||||
public class Config {
|
||||
@ -21,7 +22,7 @@ public class Config {
|
||||
try {
|
||||
this.parseConfig();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Unable to read config file; please check that " + this.configPath + " is available.");
|
||||
Logger.logger.info("Unable to read config file; please check that " + this.configPath + " is available.");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
@ -31,7 +32,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) {
|
||||
|
@ -1,6 +1,8 @@
|
||||
package tech.bitgoblin.io;
|
||||
|
||||
import java.io.File;
|
||||
import tech.bitgoblin.Logger;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class IOUtils {
|
||||
|
||||
@ -24,4 +26,23 @@ public class IOUtils {
|
||||
return path;
|
||||
}
|
||||
|
||||
// checks to see if a file is currently locked/being written to.
|
||||
public static boolean isFileLocked(File file) throws IOException {
|
||||
String[] cmd = {"lsof", file.toString()};
|
||||
Process process = Runtime.getRuntime().exec(cmd);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(
|
||||
process.getInputStream()));
|
||||
|
||||
boolean isOpen = false; // we'll change this if lsof returns that the file is open
|
||||
|
||||
String s;
|
||||
while ((s = reader.readLine()) != null) {
|
||||
if (s.endsWith(file.toString())) {
|
||||
isOpen = true;
|
||||
}
|
||||
}
|
||||
|
||||
return isOpen;
|
||||
}
|
||||
|
||||
}
|
||||
|
70
src/main/java/tech/bitgoblin/transcoder/Repository.java
Normal file
70
src/main/java/tech/bitgoblin/transcoder/Repository.java
Normal file
@ -0,0 +1,70 @@
|
||||
package tech.bitgoblin.transcoder;
|
||||
|
||||
import tech.bitgoblin.Logger;
|
||||
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() {
|
||||
Logger.logger.info("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 archiveFile(File sourceFile) {
|
||||
Path filePath = Path.of(sourceFile.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 cleanupFile(File sourceFile) {
|
||||
sourceFile.delete();
|
||||
}
|
||||
|
||||
// returns the repository's path
|
||||
public String getPath() {
|
||||
return this.repoPath;
|
||||
}
|
||||
|
||||
// return the repository's output path
|
||||
public String getOutputPath() {
|
||||
return this.outputPath;
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package tech.bitgoblin.transcoder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.TimerTask;
|
||||
|
||||
public class RunTranscoderTask extends TimerTask {
|
||||
@ -13,11 +14,11 @@ public class RunTranscoderTask extends TimerTask {
|
||||
@Override
|
||||
public void run() {
|
||||
// archive the files
|
||||
transcoder.archive();
|
||||
// run the transcoder
|
||||
transcoder.transcode();
|
||||
// clean up ingest
|
||||
transcoder.cleanup();
|
||||
try {
|
||||
transcoder.run();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,115 +10,84 @@ import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Timer;
|
||||
|
||||
import tech.bitgoblin.Logger;
|
||||
import tech.bitgoblin.config.Config;
|
||||
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);
|
||||
}
|
||||
|
||||
// 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();
|
||||
public void run() throws IOException {
|
||||
// pull list of files to transcode
|
||||
File[] sourceFiles = this.repo.searchIngest();
|
||||
|
||||
// check if we have anything to process
|
||||
if (sourceFiles.length == 0) {
|
||||
System.out.println("There is nothing to transcode in " + this.repo_dir + "/ingest.");
|
||||
return;
|
||||
Logger.logger.info("There is nothing in ingest, skipping transcode run.");
|
||||
}
|
||||
|
||||
// transcode
|
||||
System.out.println("Transcoding files in " + this.repo_dir + "/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 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
|
||||
f.toString(), // input file
|
||||
this.config.getString("transcoder.video_format"), // video container format
|
||||
this.config.getString("transcoder.video_codec"), // video codec
|
||||
this.config.getString("transcoder.video_parameters"), // video format
|
||||
this.config.getString("transcoder.video_profile"), // video profile
|
||||
this.config.getString("transcoder.audio_codec"), // audio codec
|
||||
outputPath // output file path
|
||||
);
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder(cmd.split("\\s+"));
|
||||
pb.inheritIO(); // use the java processes' input and output streams
|
||||
try {
|
||||
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);
|
||||
// loop through each file
|
||||
for (File sf : sourceFiles) {
|
||||
// check if the file is open before attempting to transcode it
|
||||
if (IOUtils.isFileLocked(sf)) {
|
||||
Logger.logger.info(String.format("Skipping %s because it is open in another program.", sf.toString()));
|
||||
continue;
|
||||
}
|
||||
|
||||
// archive files
|
||||
this.repo.archiveFile(sf);
|
||||
// transcode files
|
||||
this.transcodeFile(sf);
|
||||
// cleanup old files
|
||||
this.repo.cleanupFile(sf);
|
||||
}
|
||||
|
||||
// end output
|
||||
System.out.println("------------ End of transcoding ------------");
|
||||
System.out.println();
|
||||
Logger.logger.info("------------ End of transcoding ------------");
|
||||
Logger.logger.info("");
|
||||
}
|
||||
|
||||
// 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();
|
||||
// transcode files in the working directory
|
||||
public void transcodeFile(File sourceFile) throws IOException {
|
||||
String filePath = sourceFile.toString().substring(0, sourceFile.toString().lastIndexOf("."));
|
||||
String filename = Paths.get(filePath).getFileName().toString();
|
||||
String outputPath = Paths.get(this.repo.getOutputPath(), String.format("%s.mov", filename)).toString();
|
||||
|
||||
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();
|
||||
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
|
||||
sourceFile.toString(), // input file
|
||||
this.config.getString("transcoder.video_format"), // video container format
|
||||
this.config.getString("transcoder.video_codec"), // video codec
|
||||
this.config.getString("transcoder.video_parameters"), // video format
|
||||
this.config.getString("transcoder.video_profile"), // video profile
|
||||
this.config.getString("transcoder.audio_codec"), // audio codec
|
||||
outputPath // output file path
|
||||
);
|
||||
|
||||
try {
|
||||
Files.copy(filePath, Paths.get(archivePath), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
ProcessBuilder pb = new ProcessBuilder(cmd.split("\\s+"));
|
||||
pb.inheritIO(); // use the java processes' input and output streams
|
||||
try {
|
||||
Process process = pb.start();
|
||||
int ret = process.waitFor();
|
||||
Logger.logger.info(String.format("Program exited with code: %d", ret));
|
||||
Logger.logger.info("");
|
||||
} catch (IOException | InterruptedException 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());
|
||||
}
|
||||
|
||||
}
|
||||
|
20
src/main/resources/log4j2.xml
Normal file
20
src/main/resources/log4j2.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="info" name="Dragoon" packages="">
|
||||
<Appenders>
|
||||
<File name="DragoonLog" fileName="logs/dragoon.log">
|
||||
<PatternLayout>
|
||||
<Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
|
||||
</PatternLayout>
|
||||
</File>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout
|
||||
pattern="%highlight{%d [%t] %-5level: %msg%n%throwable}" />
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="DragoonLog"/>
|
||||
<AppenderRef ref="Console"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
@ -1,15 +1,21 @@
|
||||
package tech.bitgoblin.io;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
|
||||
public class IOUtilsTest {
|
||||
|
||||
private static String testDir = "test-temp";
|
||||
private static String testFile = "test.txt";
|
||||
|
||||
@Test
|
||||
public void shouldCreateDirectory() {
|
||||
@ -28,9 +34,30 @@ public class IOUtilsTest {
|
||||
IOUtils.resolveTilda("~test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fileShouldBeLocked() throws IOException {
|
||||
RandomAccessFile raf = new RandomAccessFile(testFile, "rw");
|
||||
assertTrue(IOUtils.isFileLocked(new File(testFile)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fileShouldNotBeLocked() throws IOException {
|
||||
RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");
|
||||
raf.close();
|
||||
assertFalse(IOUtils.isFileLocked(new File(testFile)));
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
// ensure that test files are created before running tests
|
||||
public static void setup() throws IOException {
|
||||
new File(testFile).createNewFile();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
// ensure test files are cleaned up from the environment
|
||||
public static void cleanup() {
|
||||
new File(testDir).delete();
|
||||
new File(testFile).delete();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user