Skip to content

Commit

Permalink
basic cli run implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
olir committed Apr 12, 2018
1 parent d3f0cc6 commit d6a10e1
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 45 deletions.
39 changes: 27 additions & 12 deletions app/src/main/java/de/serviceflow/frankenstein/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public class Configuration implements ConfigManager {

private static final Configuration configuration = new Configuration(null);

public void init() {
getPluginManager().load(configuration);

// todo ...


}

public static ConfigManager getInstance() {
return configuration;
}
Expand All @@ -95,27 +103,26 @@ public static Configuration cliCreateConfiguration(String[] args) {
Properties optionProperties;

usage.setLength(0);
usage.append("frankenstein");
usage.append("Usage:\n\nfrankenstein");

optionKeyIndex = getOptionIndex(args, "visual");
configuration.setVisual(optionKeyIndex>=0);
optionKeyIndex = getOptionIndex(args, "cli", false, false);
configuration.setVisual(optionKeyIndex<0);

optionKeyIndex = getOptionIndex(args, "source");
optionValue = getOptionValue(args, "source", "test", optionKeyIndex, "file", "test", "slides", "camera", "stream");
optionKeyIndex = getOptionIndex(args, "source", true, false);
optionValue = getOptionValue(args, "source", "test", optionKeyIndex, false, "file", "test", "slides", "camera", "stream");
optionProperties = getOptionProperties(args, optionKeyIndex);
cliConfigureSource(configuration, optionValue, optionProperties);

configuration.getPluginManager().load(configuration);

// todo ...

if (getOptionIndex(args, "?")>=0) {
usage.append("\n\nfrankenstein");
if (getOptionIndex(args, "?", false, true)>=0) {
return null;
}

return configuration;
}



private static void cliConfigureSource(Configuration configuration, String optionValue,
Properties optionProperties) {
switch (optionValue) {
Expand All @@ -135,9 +142,14 @@ private static void cliConfigureSource(Configuration configuration, String optio

}

private static int getOptionIndex(String[] args, String optionKey) {
usage.append(" -");
private static int getOptionIndex(String[] args, String optionKey, boolean hasValues, boolean mandatory) {
usage.append(' ');
if (!mandatory)
usage.append('[');
usage.append('-');
usage.append(optionKey);
if (!mandatory && !hasValues)
usage.append(']');

for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-" + optionKey + "=") || args[i].equals("-" + optionKey)) {
Expand All @@ -148,13 +160,16 @@ private static int getOptionIndex(String[] args, String optionKey) {
}

private static String getOptionValue(String[] args, String optionKey, String defaultValue, int optionKeyIndex,
boolean mandatory,
String... optionValues) {
usage.append("=");
for (String opt : optionValues) {
usage.append(opt);
usage.append('|');
}
usage.setLength(usage.length()-1);
if (!mandatory)
usage.append(']');

if (optionKeyIndex < 0)
return defaultValue;
Expand Down
34 changes: 21 additions & 13 deletions app/src/main/java/de/serviceflow/frankenstein/ExecutorThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.Executor;

public class ExecutorThread implements Executor {
final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
Runnable active;
boolean isAlive = true;
public class ExecutorThread {
final Queue<Runnable[]> tasks = new ArrayDeque<Runnable[]>();
Runnable[] active;
private boolean isAlive = true;
final Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Expand All @@ -29,30 +28,39 @@ private ExecutorThread() {
}

private void stop() {
isAlive=false;
isAlive = false;
}

private static ExecutorThread instance = null;

public static synchronized ExecutorThread getInstance() {
if (instance==null)
if (instance == null)
instance = new ExecutorThread();
return instance;
}

public static void shutdown() {
if (instance!=null)
if (instance != null)
instance.stop();
}

public synchronized void execute(final Runnable r) {
tasks.offer(r);

public synchronized void execute(final Runnable... r) {
// System.out.println("ExecutorThread execute "+r.length);
Runnable[] taskSequence = r;
tasks.offer(taskSequence);
}

protected synchronized void scheduleNext() {
if ((active = tasks.poll()) != null) {
active.run();
// System.out.println("ExecutorThread scheduleNext active");
for (Runnable r : active) {
// System.out.println("ExecutorThread scheduleNext running...");
r.run();
}
}
}

public boolean isAlive() {
return isAlive;
}
}
127 changes: 110 additions & 17 deletions app/src/main/java/de/serviceflow/frankenstein/Main.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,126 @@
package de.serviceflow.frankenstein;

import java.util.Arrays;
import org.opencv.core.CvException;
import org.opencv.core.Mat;

import de.serviceflow.frankenstein.fxml.FxMain;
import de.serviceflow.frankenstein.fxml.ProcessingSceneController;
import de.serviceflow.frankenstein.plugin.api.DefaultSegmentConfigController;
import de.serviceflow.frankenstein.plugin.api.SegmentVideoFilter;
import de.serviceflow.frankenstein.vf.VideoStreamSource;
import javafx.application.Platform;

public class Main {
public static void main(String[] args) {

System.out.println("Args: " + Arrays.toString(args));

Configuration c = Configuration.cliCreateConfiguration(args);
if (c == null) {
System.out.println(Configuration.getUsage());
System.exit(0);
}

if (args.length == 0 || c.isVisual()) {
c.init();

if (c.isVisual()) {
FxMain.fxmain(c);
} else {
new Main().runcli(c);
}
}

private void runcli(Configuration configuration) {
CliProcessingListener l = new CliProcessingListener();
MovieProcessor processor = new MovieProcessor(configuration);
try {
processor.init(l);
runProcessing(processor, l);
} catch (CvException e) {
// taskError(e.toString());
}
}

private void runProcessing(MovieProcessor processor, CliProcessingListener l) {
Runnable r = new Runnable() {
public void run() {

long seconds = System.currentTimeMillis() / 1000;

if (processor.processVideo(l)) {

seconds = System.currentTimeMillis() / 1000 - seconds;

System.out.println("Done in " + seconds + "s.");
} else {
// taskError("Task failed");
}
}
};
ExecutorThread.getInstance().execute(r, new Runnable() {
@Override
public void run() {
ExecutorThread.shutdown();
System.exit(0);
}
});

}

private class CliProcessingListener implements ProcessingListener {

@Override
public void configChanged(DefaultSegmentConfigController segmentConfigController,
SegmentVideoFilter selectedFilter) {
// TODO Auto-generated method stub

}

@Override
public void videoStarted(int frames, double fps) {
// TODO Auto-generated method stub

}

@Override
public void nextFrameLoaded(VideoStreamSource s) {
// TODO Auto-generated method stub

}

@Override
public void nextFrameProcessed(Mat frame, int frameId) {
// TODO Auto-generated method stub

}

@Override
public void seekDone(int frameId) {
// TODO Auto-generated method stub

}

@Override
public void seeking(int i) {
// TODO Auto-generated method stub

de.serviceflow.frankenstein.fxml.FxMain.fxmain(c);
/*
* Class<?> fxMain; try { fxMain =
* Class.forName("de.serviceflow.frankenstein.fxml.FxMain");
* Class<?> parameterTypes[] =
* {de.serviceflow.frankenstein.Configuration.class}; Method main =
* fxMain.getDeclaredMethod("fxmain", parameterTypes); Object[]
* invokeArgs = {c}; main.invoke(fxMain, invokeArgs); } catch
* (ClassNotFoundException | IllegalAccessException |
* IllegalArgumentException | InvocationTargetException |
* NoSuchMethodException | SecurityException e) {
* e.printStackTrace(); }
*/
}

@Override
public void prematureEnd(int realFrameCount) {
// TODO Auto-generated method stub

}

@Override
public void taskUpdate(String timeStamp, String message) {
// TODO Auto-generated method stub

}

@Override
public void taskError(String errorMessage) {
// TODO Auto-generated method stub

}

}
}
26 changes: 23 additions & 3 deletions app/src/main/java/de/serviceflow/frankenstein/MovieProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
package de.serviceflow.frankenstein;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -33,6 +36,7 @@
import de.serviceflow.frankenstein.vf.FilterElement;
import de.serviceflow.frankenstein.vf.VideoFilter;
import de.serviceflow.frankenstein.vf.VideoStreamSource;
import de.serviceflow.frankenstein.vf.input.TestImageInput;

public class MovieProcessor {

Expand Down Expand Up @@ -174,7 +178,7 @@ public boolean processVideo(ProcessingListener l) {

// 1. Detach Audio and Metadata from orginal video and store
// temporarily
if (configuration.doOutput && configuration.doInput) {
if (configuration.doOutput && configuration.doInput && !(configuration.getSource() instanceof TestImageInput)) {
if (!new Task(this,
ffmpeg.getAbsolutePath() + " -y -i \"" + configuration.getInputVideo() + "\""
+ " -f ffmetadata " + tempMetadataFile.getAbsolutePath()
Expand All @@ -195,6 +199,13 @@ public boolean processVideo(ProcessingListener l) {
+ tempAudioFile.getAbsolutePath(),
new TimeTaskHandler(l, "Creating Silent Audio Audio")).run())
return false;

configuration.metadata.clear();
PrintWriter w = new PrintWriter(new FileWriter(tempMetadataFile));
w.println(";FFMETADATA1");
w.close();
configuration.metadata.load(tempMetadataFile);

}
}

Expand Down Expand Up @@ -273,26 +284,32 @@ public boolean processVideo(ProcessingListener l) {
if (configuration.doOutput) {
File of = findFreeFile(new File(configuration.outputVideo));

TaskHandler handler = null;

if (!configuration.doInput || !(configuration.getSource() instanceof VideoStreamSource)) {
if (configuration.doInput) {
handler = new TimeTaskHandler(l, "Assembling Output");
if (!new Task(this, ffmpeg.getAbsolutePath() + " -y -i \"" + tempVideoFile.getAbsolutePath()
+ "\" -i " + tempAudioFile.getAbsolutePath() + " -i " + tempMetadataFile.getAbsolutePath()
+ " -map_metadata 2" + " -c:a aac -c:v libx264 -q 17 \"" + of.getAbsolutePath() + '"',
new TimeTaskHandler(l, "Assembling Output")).run())
handler).run())
return false;
} else {
handler = new TimeTaskHandler(l, "Processing Output");
if (!new Task(this,
ffmpeg.getAbsolutePath() + " -y -i \"" + tempVideoFile.getAbsolutePath() + "\" -i "
+ tempAudioFile.getAbsolutePath() + " -c:a aac -c:v libx264 -q 17 \""
+ of.getAbsolutePath()+ '"',
new TimeTaskHandler(l, "Processing Output")).run())
handler).run())
System.out.println("Warning: Task failed");
}
} else {
System.out.println("Renaming temp file " + tempVideoFile.getAbsolutePath());
tempVideoFile.renameTo(of);
}
if (!of.exists()) {
if (handler!=null)
System.err.println(handler.getLogBuffer().toString());
System.err.println("Missing output " + of.getAbsolutePath());
return false;
} else {
Expand All @@ -302,6 +319,9 @@ public boolean processVideo(ProcessingListener l) {
tempAudioFile.delete();
tempMetadataFile.delete();
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
// if (!configuration.doInput || !(configuration.getSource()
// instanceof VideoStreamSource))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ public void configure(FxMain main, Stage stage) {
this.stage = stage;
}


@FXML
public void doneButtonPressed(ActionEvent event) {
List<VideoFilter> filters = configuration.getFilters();
Expand Down

0 comments on commit d6a10e1

Please sign in to comment.