All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ws.schild.jave.VideoProcessor Maven / Gradle / Ivy

Go to download

The JAVE (Java Audio Video Encoder) library is Java wrapper on the ffmpeg project. Developers can take take advantage of JAVE2 to transcode audio and video files from a format to another. In example you can transcode an AVI file to a MPEG one, you can change a DivX video stream into a (youtube like) Flash FLV one, you can convert a WAV audio file to a MP3 or a Ogg Vorbis one, you can separate and transcode audio and video tracks, you can resize videos, changing their sizes and proportions and so on. Many other formats, containers and operations are supported by JAVE2.

There is a newer version: 3.5.0
Show newest version
package ws.schild.jave;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;
import ws.schild.jave.progress.EncoderProgressAdapter;
import ws.schild.jave.progress.VideoProgressListener;
import ws.schild.jave.process.ProcessLocator;
import ws.schild.jave.process.ffmpeg.DefaultFFMPEGLocator;
import ws.schild.jave.utils.AutoRemoveableFile;

/**
 * A high-level class meant to perform higher level operations on video files. Will use the Encoder
 * heavily, but presents a simpler interface for someone new to JAVE. For real customization, use of
 * Encoder is encouraged.
 *
 * @author mressler
 */
public class VideoProcessor {

  private static final Logger logger = LoggerFactory.getLogger(VideoProcessor.class);

  private static boolean enabled = false;

  private Encoder encoder;
  private ProcessLocator locator;

  public VideoProcessor() {
    try {
      locator = new DefaultFFMPEGLocator();
      encoder = new Encoder(locator);
      enabled = true;
    } catch (IllegalStateException ise) {
      logger.error("Error while starting the VideoService", ise);
    }
  }

  public boolean isEnabled() {
    return enabled;
  }

  /**
   * Concatenate input video files to a destination file.Destination file and parent directory must
   * be writeable.
   *
   * @see FFMPEG documentation for
   *     concatenate
   * @param videos The list of videos on the local filesystem that are readable by this process that
   *     will be concatenated together
   * @param destination The target file to write to. The target file must be unique to this process
   *     and writeable.
   * @param progress Track progress of processing
   * @throws FileNotFoundException If the destination cannot be created
   * @throws EncoderException error in encoding
   * @throws InputFormatException error in input arguments
   * @throws IllegalArgumentException thrown when parameters don't match
   */
  public void catClipsTogether(List videos, File destination, VideoProgressListener progress)
      throws FileNotFoundException, IllegalArgumentException, InputFormatException,
          EncoderException {
    assert (enabled);
    progress.onBegin();

    try (AutoRemoveableFile mergeFile = prepareMergeInstructions(videos, destination)) {
      MultimediaObject toMerge = fromFile(mergeFile);

      EncodingAttributes attributes = new EncodingAttributes();
      attributes.setInputFormat("concat");
      attributes.setSafe(0);

      VideoAttributes videoAttributes = new VideoAttributes();
      videoAttributes.setCodec("copy");
      attributes.setVideoAttributes(videoAttributes);

      encoder.encode(toMerge, destination, attributes, new EncoderProgressAdapter(progress));
    }

    progress.onComplete();
  }

  private MultimediaObject fromFile(File source) {
    return new MultimediaObject(source, locator);
  }

  private AutoRemoveableFile prepareMergeInstructions(List videos, File destination)
      throws FileNotFoundException {
    // Create the merge instruction in the same directory as the destination
    AutoRemoveableFile mergeFile =
        new AutoRemoveableFile(destination.getParentFile(), destination.getName() + ".merge.txt");

    try (PrintStream fout = new PrintStream(mergeFile)) {
      fout.println(
          videos
              .stream()
              .map(File::getAbsolutePath)
              .collect(Collectors.joining("'\nfile '", "file '", "'")));
    }

    return mergeFile;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy