Class FFmpeg

java.lang.Object
com.github.kokorin.jaffree.ffmpeg.FFmpeg

public class FFmpeg extends Object
FFmpeg provides an ability to start & stop ffmpeg process and keep track of encoding progress.
  • Constructor Details

    • FFmpeg

      public FFmpeg(Path executable)
      Creates FFmpeg.
      Parameters:
      executable - path to ffmpeg binary
  • Method Details

    • addInput

      public FFmpeg addInput(Input input)
      Adds arguments (provided by input parameter) to ffmpeg arguments list.

      Note: the order matters.

      Parameters:
      input - input
      Returns:
      this
      See Also:
    • addOutput

      public FFmpeg addOutput(Output output)
      Adds arguments (provided by output parameter) to ffmpeg arguments list.

      Note: the order matters.

      Parameters:
      output - output
      Returns:
      this
      See Also:
    • addArgument

      public FFmpeg addArgument(String argument)
      Adds custom global argument to ffmpeg arguments list.

      Note: if value contains spaces it should not be wrapped with quotes. Also spaces should not be escaped with backslash

      Parameters:
      argument - argument
      Returns:
      this
    • addArguments

      public FFmpeg addArguments(String key, String value)
      Adds custom global arguments to ffmpeg arguments list.

      Note: if value contains spaces it should not be wrapped with quotes. Also spaces should not be escaped with backslash

      Parameters:
      key - key argument
      value - value argument
      Returns:
      this
    • setComplexFilter

      public FFmpeg setComplexFilter(FilterGraph complexFilter)
      Adds complex filter graph to ffmpeg arguments list.

      Complex filtergraphs are those which cannot be described as simply a linear processing chain applied to one stream. This is the case, for example, when the graph has more than one input and/or output, or when output stream type is different from input.

      Parameters:
      complexFilter - complex filter graph
      Returns:
      this
      See Also:
    • setComplexFilter

      public FFmpeg setComplexFilter(String complexFilter)
      Adds complex filter graph to ffmpeg arguments list.

      Complex filtergraphs are those which cannot be described as simply a linear processing chain applied to one stream. This is the case, for example, when the graph has more than one input and/or output, or when output stream type is different from input.

      Parameters:
      complexFilter - complex filter graph
      Returns:
      this
      See Also:
    • setFilter

      public FFmpeg setFilter(Filter filter)
      Sets the 'generic' filter value (equivalent to the "-filter" command-line parameter).
      Parameters:
      filter - a filter to apply
      Returns:
      this
      See Also:
    • setFilter

      public FFmpeg setFilter(FilterChain filterChain)
      Sets the 'generic' filter value (equivalent to the "-filter" command-line parameter).
      Parameters:
      filterChain - a filter chain to apply
      Returns:
      this
      See Also:
    • setFilter

      public FFmpeg setFilter(String filter)
      Sets the 'generic' filter value (equivalent to the "-filter" command-line parameter).
      Parameters:
      filter - a String describing the filter to apply
      Returns:
      this
      See Also:
    • setFilter

      public FFmpeg setFilter(StreamType streamType, Filter filter)
      Sets a 'stream specific' filter value (equivalent to the "-av" / "-filter:a" or "-fv" / "-filter:v" command-line parameters).
      Parameters:
      streamType - the stream type to apply this filter to
      filter - a filter to apply
      Returns:
      this
      See Also:
    • setFilter

      public FFmpeg setFilter(StreamType streamType, FilterChain filterChain)
      Sets a 'stream specific' filter value (equivalent to the "-av" / "-filter:a" or "-fv" / "-filter:v" command-line parameters).
      Parameters:
      streamType - the stream type to apply this filter to
      filterChain - a filter chain to apply
      Returns:
      this
      See Also:
    • setFilter

      public FFmpeg setFilter(StreamType streamType, String filter)
      Sets a 'stream specific' filter value (equivalent to the "-av" / "-filter:a" or "-fv" / "-filter:v" command-line parameters).
      Parameters:
      streamType - the stream type to apply this filter to
      filter - a String describing the filter to apply
      Returns:
      this
      See Also:
    • setFilter

      public FFmpeg setFilter(String streamSpecifier, Filter filter)
      Sets a 'stream specific' filter value (equivalent to the "-av" / "-filter:a" or "-fv" / "-filter:v" / "-filter" command-line parameters).
      Parameters:
      streamSpecifier - a String specifying to which stream this filter must be applied ("a" for audio, "v" "for video, or "" for generic 'filter')
      filter - a filter
      Returns:
      this
      See Also:
    • setFilter

      public FFmpeg setFilter(String streamSpecifier, FilterChain filterChain)
      Sets a 'stream specific' filter value (equivalent to the "-av" / "-filter:a" or "-fv" / "-filter:v" / "-filter" command-line parameters).
      Parameters:
      streamSpecifier - a String specifying to which stream this filter must be applied ("a" for audio, "v" "for video, or "" for generic 'filter')
      filterChain - a filter chain describing the filters to apply
      Returns:
      this
      See Also:
    • setFilter

      public FFmpeg setFilter(String streamSpecifier, String filter)
      Sets a 'stream specific' filter value (equivalent to the "-av" / "-filter:a" or "-fv" / "-filter:v" / "-filter" command-line parameters).
      Parameters:
      streamSpecifier - a String specifying to which stream this filter must be applied ("a" for audio, "v" "for video, or "" for generic 'filter')
      filter - a String describing the filter to apply
      Returns:
      this
      See Also:
    • setOverwriteOutput

      public FFmpeg setOverwriteOutput(boolean overwriteOutput)
      Whether to overwrite output. False by default.

      If overwriteOutput is false, ffmpeg will stop with an error if output file exists.

      Parameters:
      overwriteOutput - true to overwrite output
      Returns:
      this
    • setProgressListener

      public FFmpeg setProgressListener(ProgressListener progressListener)
      Supply custom ProgressListener to receive progress events.

      Usually ffmpeg reports encoding progress every second.

      Parameters:
      progressListener - progress listener
      Returns:
      this
    • setOutputListener

      public FFmpeg setOutputListener(OutputListener outputListener)
      Supply custom OutputListener to receive ffmpeg output.

      Some ffmpeg filters cause extra output. Any line in ffmpeg output that doesn't represent encoding progress or encoding result will be passed to OutputListener

      Parameters:
      outputListener - output listener
      Returns:
      this
      See Also:
    • setProgress

      protected void setProgress(String progress)
      Send program-friendly progress information to url.

      Progress information is written periodically and at the end of the encoding process. It is made of "key=value" lines. key consists of only alphanumeric characters. The last key of a sequence of progress information is always "progress".

      This method is protected intentionally. One should use ProgressListener to get periodic progress reports.

      Parameters:
      progress - progress url
      See Also:
    • setLogLevel

      public FFmpeg setLogLevel(LogLevel logLevel)
      Sets ffmpeg logging level.

      Note: for message to appear in SLF4J logging it's required to configure appropriate log level for SLF4J.

      Parameters:
      logLevel - log level
      Returns:
      this
    • setContextName

      public FFmpeg setContextName(String contextName)
      Set context name to prepend all log messages.

      Makes logs more clear in case of multiple ffmpeg processes running simultaneously

      Parameters:
      contextName - context name
      Returns:
      this
    • setExecutorTimeoutMillis

      public FFmpeg setExecutorTimeoutMillis(int executorTimeoutMillis)
      Overrides the default Executor timeout.

      Most normal use cases will easily complete within the default timeout. It is not recommended to set an explicit timeout value unless you have actually experienced unwanted timeouts.

      A value of 0 will disable the timeout. That is, Jaffree will wait indefinitely for the Executor to complete.

      Parameters:
      executorTimeoutMillis - the custom executor timeout in milliseconds
      Returns:
      this
    • execute

      public FFmpegResult execute()
      Starts synchronous ffmpeg execution.

      Current thread is blocked until ffmpeg is finished.

      Returns:
      ffmpeg result
    • executeAsync

      public FFmpegResultFuture executeAsync()
      Starts asynchronous ffmpeg execution.
      Returns:
      ffmpeg result future
    • executeAsync

      public FFmpegResultFuture executeAsync(Executor executor)
      Starts asynchronous ffmpeg execution, executed using the supplied Executor.
      Parameters:
      executor - the executor to use for asynchronous execution
      Returns:
      ffmpeg result future
    • createProcessHandler

      protected ProcessHandler<FFmpegResult> createProcessHandler()
      Creates ProcessHandler which executes ffmpeg command and starts specified ProcessHelpers.
      Returns:
      ProcessHandler
    • createStopper

      protected Stopper createStopper()
      Creates Stopper which is used to stop ffmpeg gracefully and forcefully.
      Returns:
      Stopper
    • createStdErrReader

      protected StdReader<FFmpegResult> createStdErrReader(OutputListener listener)
      Creates StdReader which is used to read ffmpeg stderr.

      Note: should be overridden wisely: otherwise FFmpeg may produce wrong result or even produce an error.

      Parameters:
      listener - output listener for non-progress-related ffmpeg output
      Returns:
      this
    • createStdOutReader

      protected StdReader<FFmpegResult> createStdOutReader()
      Creates StdReader which is used to read ffmpeg stderr.

      Note: default implementation simply logs everything with SLF4J.

      Returns:
      this
    • createProgressHelper

      protected ProcessHelper createProgressHelper(ProgressListener listener)
      Creates ProcessHelper if required. It receives ffmpeg progress report, parses it and passes to listener.
      Parameters:
      listener - progress listener
      Returns:
      ProcessHelper, or null
    • buildArguments

      protected List<String> buildArguments()
      Constructs ffmpeg command line.

      Arguments order is as follows:

      1. arguments for each Input
      2. global arguments
      3. arguments for each Output
      Returns:
      arguments list
    • atPath

      public static FFmpeg atPath()
      Creates FFmpeg.

      Note: directory with ffmpeg binaries must be in PATH environment variable.

      Returns:
      FFmpeg
    • atPath

      public static FFmpeg atPath(Path pathToDir)
      Creates FFmpeg.
      Parameters:
      pathToDir - path to ffmpeg directory
      Returns:
      FFmpeg