• Stars
    star
    1,551
  • Rank 29,049 (Top 0.6 %)
  • Language
    Java
  • License
    BSD 2-Clause "Sim...
  • Created about 10 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Java wrapper around the FFmpeg command line tool

FFmpeg CLI Wrapper for Java

by Andrew Brampton (bramp.net) (c) 2013-2024

"Buy Me A Coffee"

A fluent interface for running FFmpeg from Java.

Java Build Status Coverage Status Maven Libraries.io

GitHub | API docs

Install

We currently support Java 8 and above. Use Maven to install the dependency.

<dependency>
  <groupId>net.bramp.ffmpeg</groupId>
  <artifactId>ffmpeg</artifactId>
  <version>0.8.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")     // Filename, or a FFmpegProbeResult
  .overrideOutputFiles(true) // Override the output if it exists

  .addOutput("output.mp4")   // Filename for the destination
    .setFormat("mp4")        // Format is inferred from filename, or can be set
    .setTargetSize(250_000)  // Aim for a 250KB file

    .disableSubtitle()       // No subtiles

    .setAudioChannels(1)         // Mono audio
    .setAudioCodec("aac")        // using the aac codec
    .setAudioSampleRate(48_000)  // at 48KHz
    .setAudioBitRate(32768)      // at 32 kbit/s

    .setVideoCodec("libx264")     // Video using x264
    .setVideoFrameRate(24, 1)     // at 24 frames per second
    .setVideoResolution(640, 480) // at 640x480 resolution

    .setStrict(FFmpegBuilder.Strict.EXPERIMENTAL) // Allow FFmpeg to use experimental specs
  .done();

FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);

// Run a one-pass encode
executor.createJob(builder).run();

// Or run a two-pass encode (which is better quality at the cost of being slower)
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) // Or filename
 .addOutput("output.mp4")
 .done();

FFmpegJob job = executor.createJob(builder, new ProgressListener() {

 // Using the FFmpegProbeResult determine the duration of the input
 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;

  // Print out interesting information about the progress
  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:

# To build
mvn

# To test
mvn test

# To release (pushing jar to maven central)
# (don't forget to set up your ~/.m2/settings.xml)
mvn release:prepare
mvn release:perform

# To publish javadoc
git checkout ffmpeg-0.x
mvn clean javadoc:aggregate scm-publish:publish-scm

Bumpings Deps

# Update Maven Plugins
mvn versions:display-plugin-updates

# Library Dependencies
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) 2013-2024, 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.

More Repositories

1

js-sequence-diagrams

Draws simple SVG sequence diagrams from textual representation of the diagram
JavaScript
7,764
star
2

prob.js

Generate random numbers from different probability distributions.
JavaScript
112
star
3

unsafe

Assorted java classes that make use of sun.misc.Unsafe
Java
95
star
4

antlr4-grammars

Precompiled Go versions of most of the grammars on https://github.com/antlr/grammars-v4.
Go
94
star
5

libcec-daemon

A Linux daemon for connecting libcec to uinput. That is, using your TV to control your PC!
C++
64
star
6

myip

(A better) What's My IP Address web application
Go
53
star
7

archivemount

FUSE filesystem using libarchive (this is out of date copy of:
C
43
star
8

protoc-gen-php

*DEPRECATED* A PHP Protocol Buffer Generator Plugin for protoc - Please use the now official compiler https://github.com/google/protobuf/tree/master/php
C++
42
star
9

Connected-component-labelling

A javascript library for connected-component labelling (aka blob extraction)
JavaScript
21
star
10

rustdns

DNS Client in Rust
Rust
17
star
11

goredirects

A tool to help host go packages with vanity domains on GitHub
Go
17
star
12

goadvent-antlr

Examples from my goadvent blog post
Go
17
star
13

dsector

API for parsing binary files using a predefined grammar
Go
14
star
14

smart-health-card-scanner

Scan Smart Health Cards
JavaScript
14
star
15

dataflow-art

Go
12
star
16

morebeam

Additional functions useful for building Apache Beam pipelines in Go.
Go
11
star
17

node-ioctl

Simple nodejs addon to add ioctl
C++
10
star
18

objectgraph

Simple library to traverse Java object graph
Java
10
star
19

ndsfs

A FUSE application to mount Nintendo DS roms
C
9
star
20

hessian.js

Node.js support for the Hessian binary web service protocol
JavaScript
8
star
21

funcs

Google Cloud Functions
JavaScript
7
star
22

mysqldump2csv

Convert mysqldump files to CSVs
Go
6
star
23

p2psim

A peer-to-peer DHT simulator
Java
5
star
24

curriculum-vitae

Andrew Brampton's Curriculum Vitae
Makefile
5
star
25

jambox

Tool to interact with the Jawbone Jambox
C++
4
star
26

libarchive

C library and command-line tools for reading and writing tar, cpio, zip, ISO, and other archive formats
C
4
star
27

pmtiles-dart

pmtiles decoder for dart
Dart
4
star
28

flvtool-plus-plus

flvtool++
C++
4
star
29

js-scan

Attempt to scan the Internet and determine what libraries everyone is using
JavaScript
3
star
30

libphonenumber-php

PHP Bindings for libphonenumber
C++
3
star
31

jackson-datatype-commons-lang3

Simple Jackson library for some datatypes in Apache Commons Lang3
Java
3
star
32

db-patterns

Some simple DB patterns implemented onto of MySQL
Java
3
star
33

dissector

Dissects various file formats
Java
3
star
34

snow.js

A simple snow demo (using device orientation)
JavaScript
2
star
35

nodewii-talk

A talk I gave on nodewii at NodeDC Nov 2012 and later at NationJS 2012
JavaScript
2
star
36

ssltester

Tests how well a SSL service is set up, and alerts if it's below required threshold
Go
2
star
37

php5-spotify

This is a simple PHP extension that wraps some of the functionality of libspotify.
C
2
star
38

ByteTorrent

Some old code I wrote to speak BitTorrent
C++
2
star
39

us-census-tools

A collection of scripts I've written to handle census data.
PHP
2
star
40

jPsyScript

PsyScript (written in Java)
Java
2
star
41

threadnetperf

Threadnetperf is a highly customisable high performance multi-threaded network benchmarking tool
C
2
star
42

sms-rpc

Simple Android app to explose a RESTful API to control your device, such as for sending SMS.
Java
2
star
43

libmintchip

Unofficial C library for interfacing with MintChips - Unfinished, and mostly an exercise in reverse engineering a protocol
C
2
star
44

bithacks

A collection of bit twiddling hacks in dart
Dart
2
star
45

php-sdk

PHP SDK for the Facebook API
PHP
1
star
46

blog

My blog (work in progress)
HTML
1
star
47

khan-index

Index of Khan Academy
Go
1
star
48

ultimate-tic-tac-toe

Rust
1
star
49

bounce

Bouncing Ball Game (unfinished)
1
star
50

aoc

Advent Of Code
Rust
1
star
51

godot-test

Test Repo for Godot
HTML
1
star
52

nine_patch

Dart
1
star
53

solvers

Solvers for sudoku and more
Go
1
star
54

par-rs

Rust PAR File implementation
Rust
1
star
55

is_x86_feature_detected

Rust
1
star
56

lusernet

LUSerNet - a peer-to-peer network that runs over a LAN.
Visual Basic
1
star
57

handy-tools

A collection of helpful cli tools written in python
Python
1
star
58

publication

A selection of my publications (with source tex/resources/etc)
TeX
1
star
59

yii-bootstrap

A clone of the mercurial yii-bootstrap - https://bitbucket.org/Crisu83/yii-bootstrap
PHP
1
star
60

js-sudoku

Sudoku solver written in javascript which uses computer vision to extract the sudoku from a image
CSS
1
star
61

phd-thesis

Andrew Brampton's PhD Thesis
TeX
1
star
62

categories

Randomly generates categories
JavaScript
1
star
63

sms-count

Simple app to count the length of a SMS message using different encodings
CSS
1
star
64

node-usb

node.js bindings for libusb-1.0
JavaScript
1
star