Search in sources :

Example 1 with StreamGobbler

use of maspack.util.StreamGobbler in project artisynth_core by artisynth.

the class MovieMaker method render.

/**
 * Renders captured data to a movie with the specified file name.
 */
public void render(String fn) throws Exception {
    if (lastFrameCount == 0) {
        Main.getMain().getLogger().info("No frames grabbed, no movie to make");
        return;
    }
    // wait for all frames to be done writing
    myViewer.awaitScreenShotCompletion();
    Method method = myMethodMap.get(myMethodName);
    if (myMethodName.equals(INTERNAL_METHOD)) {
        String[] frameFileNames = new String[lastFrameCount];
        for (int i = 1; i <= lastFrameCount; i++) {
            frameFileNames[i - 1] = getFrameFileName(i);
        }
        new MakeMovieFromData(frameFileNames, dataPath, fn + ".mov");
    } else if (myMethodName.equals(ANIMATED_GIF_METHOD)) {
        String opts = method.command;
        opts = opts.replaceAll("\\$FPS", "" + frameRate);
        String[] frameFileNames = new String[lastFrameCount];
        for (int i = 1; i <= lastFrameCount; i++) {
            frameFileNames[i - 1] = getFrameFileName(i);
        }
        File outFile = new File(dataPath + File.separator + fn + ".gif");
        DoubleHolder delayHolder = new DoubleHolder(0);
        IntHolder loopHolder = new IntHolder(0);
        AnimatedGifWriter.parseArgs(opts, delayHolder, loopHolder);
        AnimatedGifWriter.write(outFile, frameFileNames, delayHolder.value, loopHolder.value);
    } else {
        // Custom method, execute the specified command in data directory.
        String cmd = method.command;
        cmd = cmd.replaceAll("\\$FPS", "" + frameRate);
        cmd = cmd.replaceAll("\\$FMT", myFormat);
        String[] cmdArray = cmd.split("\\s+");
        // Substitute $OUT later because file name might contain white space
        String finalCmd = "";
        for (int i = 0; i < cmdArray.length; i++) {
            cmdArray[i] = cmdArray[i].replaceAll("\\$OUT", fn);
            finalCmd = finalCmd + " " + cmdArray[i];
        }
        // Map<String, String> env = System.getenv();
        // File dataFile = new File(dataPath);
        // Process proc = Runtime.getRuntime().exec (
        // cmdArray, /*env=*/null, null);
        Main.getMain().getLogger().info("Executing " + finalCmd);
        ProcessBuilder procBuild = new ProcessBuilder(cmdArray);
        procBuild.directory(new File(dataPath));
        Process proc = procBuild.start();
        StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), System.err, ">>");
        // any output?
        StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), System.out, ">>");
        // start gobblers
        outputGobbler.start();
        errorGobbler.start();
        int exitVal = proc.waitFor();
        if (exitVal != 0) {
            Main.getMain().getLogger().error("\nMovie creation failed with exit value: " + exitVal + "\n");
        }
    }
}
Also used : DoubleHolder(argparser.DoubleHolder) StreamGobbler(maspack.util.StreamGobbler) IntHolder(argparser.IntHolder) File(java.io.File)

Aggregations

DoubleHolder (argparser.DoubleHolder)1 IntHolder (argparser.IntHolder)1 File (java.io.File)1 StreamGobbler (maspack.util.StreamGobbler)1