Added ability for the transcoder to determine if a video file is open in another program to avoid trying to transcode/remove a partially written file; reworked the main transcode loop to handle one file at a time instead of archiving everything, then transcoding, then cleanup
This commit is contained in:
@ -31,54 +31,29 @@ public class Transcoder {
|
||||
}
|
||||
|
||||
// create a periodic timer task and start it
|
||||
public void run() {
|
||||
public void run() throws IOException {
|
||||
// pull list of files to transcode
|
||||
File[] sourceFiles = this.repo.searchIngest();
|
||||
|
||||
// archive files
|
||||
this.repo.archiveIngest(sourceFiles);
|
||||
// transcode files
|
||||
this.transcode(sourceFiles);
|
||||
// cleanup old files
|
||||
this.repo.cleanupIngest(sourceFiles);
|
||||
}
|
||||
|
||||
// transcode files in the working directory
|
||||
public void transcode(File[] sourceFiles) {
|
||||
// check if the ingest directory is empty
|
||||
// check if we have anything to process
|
||||
if (sourceFiles.length == 0) {
|
||||
Logger.logger.info("There is nothing to transcode in ingest.");
|
||||
return;
|
||||
Logger.logger.info("There is nothing in ingest, skipping transcode run.");
|
||||
}
|
||||
|
||||
// transcode
|
||||
Logger.logger.info("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.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
|
||||
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();
|
||||
Logger.logger.info("Program exited with code: %d", ret);
|
||||
Logger.logger.info("");
|
||||
} 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
|
||||
@ -86,4 +61,33 @@ public class Transcoder {
|
||||
Logger.logger.info("");
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user