001package net.bramp.ffmpeg;
002
003import com.google.common.base.Joiner;
004import com.google.common.base.Preconditions;
005import java.io.File;
006import java.io.IOException;
007import java.util.List;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Simple function that creates a Process with the arguments, and returns a BufferedReader reading
013 * stdout.
014 *
015 * @author bramp
016 */
017public class RunProcessFunction implements ProcessFunction {
018
019  static final Logger LOG = LoggerFactory.getLogger(RunProcessFunction.class);
020
021  File workingDirectory;
022
023  @Override
024  public Process run(List<String> args) throws IOException {
025    Preconditions.checkNotNull(args, "Arguments must not be null");
026    Preconditions.checkArgument(!args.isEmpty(), "No arguments specified");
027
028    if (LOG.isInfoEnabled()) {
029      LOG.info("{}", Joiner.on(" ").join(args));
030    }
031
032    ProcessBuilder builder = new ProcessBuilder(args);
033    if (workingDirectory != null) {
034      builder.directory(workingDirectory);
035    }
036    builder.redirectErrorStream(true);
037    return builder.start();
038  }
039
040  /** Sets the working directory for the process using a path string. */
041  public RunProcessFunction setWorkingDirectory(String workingDirectory) {
042    this.workingDirectory = new File(workingDirectory);
043    return this;
044  }
045
046  /** Sets the working directory for the process using a File object. */
047  public RunProcessFunction setWorkingDirectory(File workingDirectory) {
048    this.workingDirectory = workingDirectory;
049    return this;
050  }
051}