/avconv4java

Java interface to avconv tool

Primary LanguageJava

avconv4java

Build Status Coverage Status License Maven Codacy Badge

Introduction

avconv tool is a part of the Libav project (originates from the FFmpeg codebase). It is a fast and powerful video and audio converter. Libav supports most common instruction set architectures (including IA-32, x86-64, PowerPC, ARM, etc.) with great performance.

avconv4java is a simple pure-java interface to the avconv command-line tool. API was designed with KISS principle in mind to be as simple as possible.

Online documentation:

Features

  • Most avconv commands are supported (and tested in real projects).
  • Parallel processing is supported out of box.
  • It has a very simple API with fluent interfaces and method chaining.
  • Options and operators are transformed into similar method-names, e.g.
    • -vcodec libx264 -> .videoCodec(AVVideoCodecType.H264)
    • -vcodec libtheora -> .videoCodec(AVVideoCodecType.THEORA)
    • -vcodec libtheora -> .videoCodec("libtheora")

Setup

Gradle:

repositories {
    maven {
        url "https://jitpack.io"
    }
}

dependencies {
    compile 'com.github.vbauer:avconv4java:1.2.3'
}

Maven:

<repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
</repository>

<dependency>
    <groupId>com.github.vbauer</groupId>
    <artifactId>avconv4java</artifactId>
    <version>1.2.3</version>
</dependency>

Example

First of all you need to configure options for avconv command. Builder pattern allows to do it as simple as possible:

final AVRootOptions options = AVRootOptions.create("input.avi", "output.mp4")
    .builders(
        AVMainOptions.create()
            .overwriteOutput(),
        AVVideoOptions.create()
            .proportionalResizeUsingWidth(800)
            .videoCodec(AVVideoCodecType.H264)
            .movFlags(AVMovFlagsType.FAST_START),
        AVAudioOptions.create()
            .audioCodec(AVAudioCodecType.VISUAL_ON_AAC)
            .audioBitRate(128)
            .audioChannelsCount(2)
            .sampleRate(11025),
        AVCodecOptions.create()
            .bitRate(1000)
    )

To execute avconv command with needed options you should use the class com.avconv4java.core.AVCommnad:

// It'll be better to configure timeout always. Debug is useful sometimes.
final AVCommand command = AVCommand.create()
    .setDebug(true)
    .setTimeout(timeout);

final int returnCode = command.run(options);
final String outputFile = options.getOutputFile();

Logger.getGlobal().info(
    String.format("Output file: %s, return code: %d", outputFile, returnCode)
);

This command is equivalent to the following:

/usr/bin/avconv -i input.avi -y -vf scale=w=800:h=trunc(ow/a/2)*2 -vcodec libx264 -movflags faststart -acodec libvo_aacenc -ab 128k -b 1000k -ac 2 -ar 11025 output.mp4

FAQ

What is the default avconv path value?
Environment variable "AVCONV4JAVA_TOOLPATH" will be checked in priority, otherwise it will be "/usr/bin/avconv".

Might also like

  • jconditions - Extra conditional annotations for JUnit.
  • jackdaw - Java Annotation Processor which allows to simplify development.
  • houdini - Type conversion system for Spring framework.
  • herald - Logging annotation for Spring framework.
  • caesar - Library that allows to create async beans from sync beans.
  • commons-vfs2-cifs - SMB/CIFS provider for Commons VFS.

License

Copyright 2014 Vladislav Bauer

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.