Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to execute FFmpeg and FFprobe synchronously #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ interface FFbinaryInterface {
*/
FFtask execute(String[] cmd, FFcommandExecuteResponseHandler ffcommandExecuteResponseHandler);

/**
* Executes a command synchronously
*
* @param environmentVars Environment variables
* @param cmd command to execute
* @return The output of the command
*/
String execute(Map<String, String> environmentVars, String[] cmd);

/**
* Executes a command synchronously
*
* @param cmd command to execute
* @return The output of the command
*/
String execute(String[] cmd);

/**
* Checks if FF binary is supported on this device
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package nl.bravobit.ffmpeg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class FFcommandExecuteSynchronous {

private final String[] cmd;
private Map<String, String> environment;
private final ShellCommand shellCommand;
private final long timeout;
private long startTime;
private Process process;
private String output = "";

FFcommandExecuteSynchronous(String[] cmd, Map<String, String> environment, long timeout) {
this.cmd = cmd;
this.timeout = timeout;
this.environment = environment;
this.shellCommand = new ShellCommand();
}

private CommandResult runCommand() {
startTime = System.currentTimeMillis();
try {
process = shellCommand.run(cmd, environment);
if (process == null) {
return CommandResult.getDummyFailureResponse();
}
Log.d("Running publishing updates method");
checkAndUpdateProcess();
return CommandResult.getOutputFromProcess(process);
} catch (TimeoutException e) {
Log.e("FFmpeg binary timed out", e);
return new CommandResult(false, e.getMessage());
} catch (Exception e) {
Log.e("Error running FFmpeg binary", e);
} finally {
Util.destroyProcess(process);
}
return CommandResult.getDummyFailureResponse();
}

public String execute() {
CommandResult commandResult = runCommand();
output += commandResult.output;
return output;
}

private void checkAndUpdateProcess() throws TimeoutException {
while (!Util.isProcessCompleted(process)) {

// checking if process is completed
if (Util.isProcessCompleted(process)) {
return;
}

// Handling timeout
if (timeout != Long.MAX_VALUE && System.currentTimeMillis() > startTime + timeout) {
throw new TimeoutException("FFmpeg binary timed out");
}

try {
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((line = reader.readLine()) != null) {
output += line + "\n";
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
17 changes: 17 additions & 0 deletions android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FFmpeg.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ public FFtask execute(String[] cmd, FFcommandExecuteResponseHandler ffmpegExecut
return execute(null, cmd, ffmpegExecuteResponseHandler);
}

@Override
public String execute(Map<String, String> environmentVars, String[] cmd) {
if (cmd.length != 0) {
String[] ffmpegBinary = new String[]{FileUtils.getFFmpeg(context.provide()).getAbsolutePath()};
String[] command = concatenate(ffmpegBinary, cmd);
FFcommandExecuteSynchronous synchronous = new FFcommandExecuteSynchronous(command, environmentVars, timeout);
return synchronous.execute();
} else {
throw new IllegalArgumentException("shell command cannot be empty");
}
}

@Override
public String execute(String[] cmd) {
return execute(null, cmd);
}

@Override
public boolean isCommandRunning(FFtask task) {
return task != null && !task.isProcessCompleted();
Expand Down
17 changes: 17 additions & 0 deletions android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FFprobe.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ public FFtask execute(String[] cmd, FFcommandExecuteResponseHandler ffcommandExe
return execute(null, cmd, ffcommandExecuteResponseHandler);
}

@Override
public String execute(Map<String, String> environmentVars, String[] cmd) {
if (cmd.length != 0) {
String[] ffprobeBinary = new String[]{FileUtils.getFFprobe(context.provide()).getAbsolutePath()};
String[] command = concatenate(ffprobeBinary, cmd);
FFcommandExecuteSynchronous synchronous = new FFcommandExecuteSynchronous(command, environmentVars, timeout);
return synchronous.execute();
} else {
throw new IllegalArgumentException("shell command cannot be empty");
}
}

@Override
public String execute(String[] cmd) {
return execute(null, cmd);
}

public boolean isCommandRunning(FFtask task) {
return task != null && !task.isProcessCompleted();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void onCreate(Bundle savedInstanceState) {
// ffmpeg is supported
versionFFmpeg();
//ffmpegTestTaskQuit();
synchronousVersionFFmpeg();
} else {
// ffmpeg is not supported
Timber.e("ffmpeg not supported!");
Expand All @@ -32,6 +33,7 @@ public void onCreate(Bundle savedInstanceState) {
if (FFprobe.getInstance(this).isSupported()) {
// ffprobe is supported
versionFFprobe();
synchronousVersionFFprobe();
} else {
// ffprobe is not supported
Timber.e("ffprobe not supported!");
Expand Down Expand Up @@ -69,6 +71,16 @@ public void onProgress(String message) {
});
}

private void synchronousVersionFFmpeg() {
Timber.d("version ffmpeg synchronous");
Timber.d(FFmpeg.getInstance(this).execute(new String[]{"-version"}));
}

private void synchronousVersionFFprobe() {
Timber.d("version ffprobe synchronous");
Timber.d(FFprobe.getInstance(this).execute(new String[]{"-version"}));
}

private void ffmpegTestTaskQuit() {
String[] command = {"-i", "input.mp4", "output.mov"};

Expand Down