FFmpeg Java
by Andrew Brampton (bramp.net) (c) 2013-2014,2016
A fluent interface to running FFmpeg from Java.
GitHub | API docs
Install
Maven:
<dependency>
<groupId>net.bramp.ffmpeg</groupId>
<artifactId>ffmpeg</artifactId>
<version>0.7.0</version>
</dependency>
Usage
Video Encoding
Code:
FFmpeg ffmpeg = new FFmpeg("/path/to/ffmpeg");
FFprobe ffprobe = new FFprobe("/path/to/ffprobe");
FFmpegBuilder builder = new FFmpegBuilder()
.setInput("input.mp4")
.overrideOutputFiles(true)
.addOutput("output.mp4")
.setFormat("mp4")
.setTargetSize(250_000)
.disableSubtitle()
.setAudioChannels(1)
.setAudioCodec("aac")
.setAudioSampleRate(48_000)
.setAudioBitRate(32768)
.setVideoCodec("libx264")
.setVideoFrameRate(24, 1)
.setVideoResolution(640, 480)
.setStrict(FFmpegBuilder.Strict.EXPERIMENTAL);
FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
executor.createJob(builder).run();
executor.createTwoPassJob(builder).run();
Get Media Information
Code:
FFprobe ffprobe = new FFprobe("/path/to/ffprobe");
FFmpegProbeResult probeResult = ffprobe.probe("input.mp4");
FFmpegFormat format = probeResult.getFormat();
System.out.format("%nFile: '%s' ; Format: '%s' ; Duration: %.3fs",
format.filename,
format.format_long_name,
format.duration
);
FFmpegStream stream = probeResult.getStreams().get(0);
System.out.format("%nCodec: '%s' ; Width: %dpx ; Height: %dpx",
stream.codec_long_name,
stream.width,
stream.height
);
Get progress while encoding
FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
FFmpegProbeResult in = ffprobe.probe("input.flv");
FFmpegBuilder builder = new FFmpegBuilder()
.setInput(in)
.addOutput("output.mp4")
.done();
FFmpegJob job = executor.createJob(builder, new ProgressListener() {
final double duration_ns = in.getFormat().duration * TimeUnit.SECONDS.toNanos(1);
@Override
public void progress(Progress progress) {
double percentage = progress.out_time_ns / duration_ns;
System.out.println(String.format(
"[%.0f%%] status:%s frame:%d time:%s ms fps:%.0f speed:%.2fx",
percentage * 100,
progress.status,
progress.frame,
FFmpegUtils.toTimecode(progress.out_time_ns, TimeUnit.NANOSECONDS),
progress.fps.doubleValue(),
progress.speed
));
}
});
job.run();
Building & Releasing
If you wish to make changes, then building and releasing is simple:
mvn
mvn test
mvn release:prepare
mvn release:perform
git checkout ffmpeg-0.x
mvn clean javadoc:aggregate scm-publish:publish-scm
Bumpings Deps
mvn versions:display-plugin-updates
mvn versions:display-dependency-updates
Install FFmpeg on Ubuntu
We only the support the original FFmpeg, not the libav version. Before Ubuntu 12.04, and in 15.04
and later the original FFmpeg is shipped. If you have to run on a version with libav, you can install
FFmpeg from a PPA, or using the static build. More information here
Get involved!
We welcome contributions. Please check the issue tracker.
If you see something you wish to work on, please either comment on the issue, or just send a pull
request. Want to work on something else, then just open a issue, and we can discuss! We appreciate
documentation improvements, code cleanup, or new features. Please be mindful that all work is done
on a volunteer basis, thus we can be slow to reply.
Licence (Simplified BSD License)
Copyright (c) 2016-2022, Andrew Brampton
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.