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  public RunProcessFunction setWorkingDirectory(String workingDirectory) {
041    this.workingDirectory = new File(workingDirectory);
042    return this;
043  }
044
045  public RunProcessFunction setWorkingDirectory(File workingDirectory) {
046    this.workingDirectory = workingDirectory;
047    return this;
048  }
049}