-
Notifications
You must be signed in to change notification settings - Fork 0
API Usage
The Java API is unchanged from upstream FFmpegKit — same com.arthenica.ffmpegkit package, same classes. This page walks through the parts of it you'll actually use. See Getting Started first if you haven't added the dependency yet.
Every execute/executeAsync call creates a new session object you can inspect — FFmpegSession for FFmpeg commands, FFprobeSession for FFprobe ones.
FFmpegSession session = FFmpegKit.execute("-i file1.mp4 -c:v mpeg4 file2.mp4");
long sessionId = session.getSessionId();
String command = session.getCommand();
String[] arguments = session.getArguments();
SessionState state = session.getState(); // still running or completed
ReturnCode returnCode = session.getReturnCode(); // null until completed
Date startTime = session.getStartTime();
Date endTime = session.getEndTime();
long duration = session.getDuration();
String output = session.getOutput(); // console output for this execution
String failStackTrace = session.getFailStackTrace();
List<com.arthenica.ffmpegkit.Log> logs = session.getLogs();
List<Statistics> statistics = session.getStatistics();FFmpegKit.executeAsync("-i file1.mp4 -c:v mpeg4 file2.mp4",
session -> {
// FFmpegSessionCompleteCallback - called when the session finishes
Log.d(TAG, String.format("FFmpeg process exited with state %s and rc %s.%s",
session.getState(), session.getReturnCode(), session.getFailStackTrace()));
},
log -> {
// LogCallback - called whenever the session prints a log line
},
statistics -> {
// StatisticsCallback - called whenever the session generates statistics
});// Synchronous
FFprobeSession session = FFprobeKit.execute(ffprobeCommand);
if (!ReturnCode.isSuccess(session.getReturnCode())) {
Log.d(TAG, "Command failed. Check output for details.");
}
// Asynchronous
FFprobeKit.executeAsync(ffprobeCommand, session -> {
// called when the session is executed
});MediaInformationSession mediaInformation = FFprobeKit.getMediaInformation("<file path or uri>");
mediaInformation.getMediaInformation();FFmpegKit.cancel(); // stop every running session
FFmpegKit.cancel(sessionId); // stop a specific oneList<Session> sessions = FFmpegKitConfig.getSessions();
for (Session session : sessions) {
Log.d(TAG, String.format("Session id:%d, startTime:%s, duration:%s, state:%s, returnCode:%s.",
session.getSessionId(), session.getStartTime(), session.getDuration(),
session.getState(), session.getReturnCode()));
}Useful if you don't want to attach a callback to every individual executeAsync call.
FFmpegKitConfig.enableFFmpegSessionCompleteCallback(session -> { /* ... */ });
FFmpegKitConfig.enableFFprobeSessionCompleteCallback(session -> { /* ... */ });
FFmpegKitConfig.enableMediaInformationSessionCompleteCallback(session -> { /* ... */ });
FFmpegKitConfig.enableLogCallback(log -> { /* ... */ });
FFmpegKitConfig.enableStatisticsCallback(statistics -> { /* ... */ });ffmpeg needs a fontconfig configuration to render subtitles or use the drawtext filter — Android doesn't ship one by default. Register a font directory before running such a command:
FFmpegKitConfig.setFontDirectoryList(context, Arrays.asList("/system/fonts", "<folder with fonts>"), Collections.EMPTY_MAP);See Tips for the exact errors you'll hit if you skip this.
Required by frameworks that use Mono (e.g. Unity, Xamarin) — not applicable to a plain Android app:
FFmpegKitConfig.ignoreSignal(Signal.SIGXCPU);-
Storage Access Framework — converting
content://Uris for use as input/output paths - Camera Support — capturing from the device camera
- Pipe Support — feeding another process's output into a command
- Concurrency — running multiple sessions without log cross-talk